ha(H2): компонент fglair — manifest, config flow (облако/ручной/импорт), пробная сессия
- custom_components/fglair: manifest (config_flow, local_push, requirements),
const, trial (start -> ONLINE <=10 c -> GET базовых свойств), FglairClient
(сессия pyfglair в executor, колбэки в loop, --runtime_data),
__init__ (setup/unload, рефкаунт глобального лог-пампа)
- config flow: меню cloud/manual/import_json + import-source; discover через
pyfglair.provision; пробное подключение с диагностикой (cannot_connect,
no_slots, key_mismatch, timeout, invalid_auth, invalid_import); unique_id=dsn
- strings.json + translations/{en,ru}.json
- тесты HA (pytest-homeassistant-custom-component): меню/шаги/ошибки/импорт/
облако (discover замокан), setup/unload, занятый порт; trial против
mock_ac.py (online/key_mismatch/unreachable/no_slots)
- pytest.ini (общий), tests/conftest перенесён на уровень tests/,
scripts/py-ci.sh: --setup-ha и прогон tests/components
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
"""FGLair (Fujitsu General) — локальное управление через LAN-протокол Ayla."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
import pyfglair
|
||||
|
||||
from .const import DOMAIN
|
||||
from .fglair_client import FglairClient
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS: list[str] = []
|
||||
|
||||
_LOG_LEVELS = {
|
||||
0: logging.DEBUG,
|
||||
1: logging.INFO,
|
||||
2: logging.WARNING,
|
||||
3: logging.ERROR,
|
||||
}
|
||||
|
||||
DATA_CLIENTS = f"{DOMAIN}_clients"
|
||||
|
||||
|
||||
def _log_handler(level: int, message: str) -> None:
|
||||
_LOGGER.log(_LOG_LEVELS.get(level, logging.DEBUG), "pyfglair: %s", message)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: dict) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
clients: dict[str, FglairClient] = hass.data.setdefault(DATA_CLIENTS, {})
|
||||
if not clients:
|
||||
pyfglair.set_log_handler(_log_handler)
|
||||
pyfglair.set_log_level(1)
|
||||
|
||||
client = FglairClient(hass, entry.data)
|
||||
try:
|
||||
await client.async_start()
|
||||
except Exception:
|
||||
if not clients:
|
||||
pyfglair.set_log_handler(None)
|
||||
raise
|
||||
clients[entry.entry_id] = client
|
||||
entry.runtime_data = client
|
||||
|
||||
if PLATFORMS:
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
client: FglairClient = entry.runtime_data
|
||||
await client.async_stop()
|
||||
|
||||
clients: dict[str, FglairClient] = hass.data.get(DATA_CLIENTS, {})
|
||||
clients.pop(entry.entry_id, None)
|
||||
if not clients:
|
||||
pyfglair.set_log_handler(None)
|
||||
return True
|
||||
Reference in New Issue
Block a user