Convert from MQTT bridge architecture to a proper Home Assistant integration (custom_components/localthings/): - Config flow: IP-only entry, probes ports [49154,49155], cert/key reused from first device on subsequent adds - Coordinator: DataUpdateCoordinator wrapping blocking DTLS session via async_add_executor_job; discovery on first poll; dynamic 5s/30s interval based on active_when - Entity platforms: sensor, binary_sensor, switch, number, select - Library cleanup: delete bridge.py, main.py, config.py, logger.py, keepalive.py, poll_scheduler.py, sensors.py, state_cache.py - adapter.py stripped to flatten(), is_active(), _key() - _parse_device0_batch moved to samsung_appliance/batch.py
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
"""Adapter: BoundEntity list → flat state dict and command dispatch."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from .discovery import BoundEntity
|
|
|
|
|
|
def _key(b: BoundEntity) -> str:
|
|
return f"{b.key_override or b.desc.key}{b.instance}"
|
|
|
|
|
|
def flatten(bound: list[BoundEntity], resources: dict) -> dict[str, Any]:
|
|
"""Map bound entities to their current scalar values."""
|
|
out: dict[str, Any] = {}
|
|
for b in bound:
|
|
if not b.desc.field:
|
|
continue
|
|
rep = resources.get(b.href) or {}
|
|
if b.desc.exists_fn is not None and not b.desc.exists_fn(rep):
|
|
continue
|
|
out[_key(b)] = b.desc.value_fn(rep.get(b.desc.field))
|
|
return out
|
|
|
|
|
|
def is_active(bound: list[BoundEntity], resources: dict) -> bool:
|
|
"""Return True if any capability's active_when fires."""
|
|
for b in bound:
|
|
if b.capability.active_when:
|
|
rep = resources.get(b.href) or {}
|
|
if b.capability.active_when(rep):
|
|
return True
|
|
return False
|