work: Отладил переподключение к Tion. Раньше при разрыве соединения в библиотеке btle соединение зависало. Сейчас при сбросе устройства явно вызывается disconnect библиотеки, чтобы сессия Bluetooth освобождалась.

This commit is contained in:
Fedorov Dmitriy
2026-09-13 21:35:37 +03:00
parent 5cc125db3f
commit fdae9dec72
7 changed files with 557 additions and 18 deletions
+37
View File
@@ -0,0 +1,37 @@
import asyncio
import logging
from app.my_dataclasses import TION_MAC
from app.tion import TionController, TionService
# Убираем служебное логирование библиотек
async def main():
logging.getLogger("asyncio").setLevel(logging.WARNING)
logging.getLogger("bleak").setLevel(logging.WARNING)
logging.getLogger("tion_btle").setLevel(logging.WARNING)
controller = TionController(TION_MAC)
service = TionService(
controller,
poll_interval=3,
)
async with service:
for seconds in range(0, 91, 3):
print(
f"{seconds:02d}s | "
f"online={service.online} | "
f"last_seen={service.last_seen} | "
f"error={service.last_error}"
)
await asyncio.sleep(3)
if __name__ == "__main__":
asyncio.run(main())
+30
View File
@@ -0,0 +1,30 @@
import asyncio
import logging
from bleak import BleakScanner
from app.my_dataclasses import TION_MAC
logging.disable(logging.CRITICAL)
async def main():
print("Ищу Tion...")
device = await BleakScanner.find_device_by_address(
TION_MAC,
timeout=10,
)
if device is None:
print("Tion НЕ найден сканером")
else:
print("Tion найден:")
print(f" name: {device.name}")
print(f" address: {device.address}")
print(f" details: {device.details}")
if __name__ == "__main__":
asyncio.run(main())
+22
View File
@@ -55,6 +55,28 @@ async def main():
state = await tion.set_speed(3)
print_state("После SPEED 3", state)
state = await tion.set_target_temperature(20)
print_state("TARGET TEMP 20°C", state)
state = await tion.set_air_mode("recirculation")
print_state("RECIRCULATION", state)
await asyncio.sleep(3)
state = await tion.set_air_mode("outside")
print_state("RECIRCULATION", state)
await asyncio.sleep(3)
state = await tion.sound_off()
print_state("SOUND OFF", state)
state = await tion.sound_on()
print_state("SOUND ON", state)
state = await tion.light_off()
print_state("LIGHT OFF", state)
state = await tion.light_on()
print_state("LIGHT ON", state)
print()
print(f"Connected after exit: {tion.connected}")
+84
View File
@@ -0,0 +1,84 @@
import asyncio
import json
import logging
from app.tion import (
TionController,
TionService,
)
from app.my_dataclasses import *
def print_service(service: TionService) -> None:
print()
print("=" * 60)
print(f"Running: {service.running}")
print(f"Online: {service.online}")
print(f"Last seen: {service.last_seen}")
print(f"Last error: {service.last_error}")
if service.state is not None:
print()
print(json.dumps(
service.state.to_dict(),
indent=2,
ensure_ascii=False,
))
async def main():
# Убираем лишнее логирование
logging.getLogger("asyncio").setLevel(logging.WARNING)
logging.getLogger("bleak").setLevel(logging.WARNING)
logging.getLogger("tion_btle").setLevel(logging.WARNING)
controller = TionController(TION_MAC)
service = TionService(
controller,
poll_interval=3,
)
async with service:
print("=== После запуска ===")
print_service(service)
print()
print("Ждём несколько циклов polling...")
await asyncio.sleep(60)
print_service(service)
print()
print("=== Устанавливаем скорость 2 ===")
state = await service.execute(
lambda tion: tion.set_speed(2)
)
print(json.dumps(
state.to_dict(),
indent=2,
ensure_ascii=False,
))
print()
print("=== Устанавливаем температуру 20°C ===")
await service.execute(
lambda tion: tion.set_target_temperature(20)
)
print_service(service)
print()
print("=== После stop ===")
print_service(service)
if __name__ == "__main__":
asyncio.run(main())