1 """Coordinator for the nuki component."""
3 from __future__
import annotations
6 from collections
import defaultdict
7 from datetime
import timedelta
10 from pynuki
import NukiBridge, NukiLock, NukiOpener
11 from pynuki.bridge
import InvalidCredentialsException
12 from pynuki.device
import NukiDevice
13 from requests.exceptions
import RequestException
20 from .const
import DOMAIN, ERROR_STATES
21 from .helpers
import parse_id
23 _LOGGER = logging.getLogger(__name__)
29 """Data Update Coordinator for the Nuki integration."""
35 locks: list[NukiLock],
36 openers: list[NukiOpener],
38 """Initialize my coordinator."""
45 update_interval=UPDATE_INTERVAL,
53 """Return the parsed id of the Nuki bridge."""
57 """Fetch data from Nuki bridge."""
61 async
with asyncio.timeout(10):
62 events = await self.
hasshass.async_add_executor_job(
65 except InvalidCredentialsException
as err:
66 raise UpdateFailed(f
"Invalid credentials for Bridge: {err}")
from err
67 except RequestException
as err:
68 raise UpdateFailed(f
"Error communicating with Bridge: {err}")
from err
70 ent_reg = er.async_get(self.
hasshass)
71 for event, device_ids
in events.items():
72 for device_id
in device_ids:
73 entity_id = ent_reg.async_get_entity_id(
74 Platform.LOCK, DOMAIN, device_id
77 "entity_id": entity_id,
80 self.
hasshass.bus.async_fire(
"nuki_event", event_data)
83 """Update the Nuki devices.
86 A dict with the events to be fired. The event type is the key and the device ids are the value
90 events: dict[str, set[str]] = defaultdict(set)
92 for device
in devices:
93 for level
in (
False,
True):
95 if isinstance(device, NukiOpener):
96 last_ring_action_state = device.ring_action_state
100 if not last_ring_action_state
and device.ring_action_state:
101 events[
"ring"].
add(device.nuki_id)
104 except RequestException:
107 if device.state
not in ERROR_STATES:
None _async_update_data(self)
None __init__(self, HomeAssistant hass, NukiBridge bridge, list[NukiLock] locks, list[NukiOpener] openers)
dict[str, set[str]] update_devices(self, list[NukiDevice] devices)
bool add(self, _T matcher)
def parse_id(hardware_id)