1 """The yolink integration."""
3 from __future__
import annotations
6 from dataclasses
import dataclass
7 from datetime
import timedelta
10 from yolink.const
import ATTR_DEVICE_SMART_REMOTER
11 from yolink.device
import YoLinkDevice
12 from yolink.exception
import YoLinkAuthFailError, YoLinkClientError
13 from yolink.home_manager
import YoLinkHome
14 from yolink.message_listener
import MessageListener
22 config_entry_oauth2_flow,
23 config_validation
as cv,
24 device_registry
as dr,
29 from .const
import DOMAIN, YOLINK_EVENT
30 from .coordinator
import YoLinkCoordinator
31 from .device_trigger
import CONF_LONG_PRESS, CONF_SHORT_PRESS
32 from .services
import async_register_services
36 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
40 Platform.BINARY_SENSOR,
54 """YoLink home message listener."""
56 def __init__(self, hass: HomeAssistant, entry: ConfigEntry) ->
None:
57 """Init YoLink home message listener."""
61 def on_message(self, device: YoLinkDevice, msg_data: dict[str, Any]) ->
None:
62 """On YoLink home message received."""
63 entry_data = self.
_hass_hass.data[DOMAIN].
get(self.
_entry_entry.entry_id)
66 device_coordinators = entry_data.device_coordinators
67 if not device_coordinators:
69 device_coordinator: YoLinkCoordinator = device_coordinators.get(
72 if device_coordinator
is None:
74 device_coordinator.dev_online =
True
75 device_coordinator.async_set_updated_data(msg_data)
78 device_coordinator.device.device_type == ATTR_DEVICE_SMART_REMOTER
79 and msg_data.get(
"event")
is not None
81 device_registry = dr.async_get(self.
_hass_hass)
82 device_entry = device_registry.async_get_device(
83 identifiers={(DOMAIN, device_coordinator.device.device_id)}
85 if device_entry
is None:
88 if msg_data[
"event"][
"type"] ==
"Press":
89 key_press_type = CONF_SHORT_PRESS
91 key_press_type = CONF_LONG_PRESS
92 button_idx = msg_data[
"event"][
"keyMask"]
94 "type": f
"button_{button_idx}_{key_press_type}",
95 "device_id": device_entry.id,
97 self.
_hass_hass.bus.async_fire(YOLINK_EVENT, event_data)
102 """YoLink home store."""
104 home_instance: YoLinkHome
105 device_coordinators: dict[str, YoLinkCoordinator]
108 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
117 """Set up yolink from a config entry."""
118 hass.data.setdefault(DOMAIN, {})
120 await config_entry_oauth2_flow.async_get_config_entry_implementation(
125 session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
128 hass, aiohttp_client.async_get_clientsession(hass), session
130 yolink_home = YoLinkHome()
132 async
with asyncio.timeout(10):
133 await yolink_home.async_setup(
136 except YoLinkAuthFailError
as yl_auth_err:
137 raise ConfigEntryAuthFailed
from yl_auth_err
138 except (YoLinkClientError, TimeoutError)
as err:
139 raise ConfigEntryNotReady
from err
141 device_coordinators = {}
144 device_pairing_mapping = {}
145 for device
in yolink_home.get_devices():
146 if (parent_id := device.get_paired_device_id())
is not None:
147 device_pairing_mapping[parent_id] = device.device_id
149 for device
in yolink_home.get_devices():
150 paried_device: YoLinkDevice |
None =
None
152 paried_device_id := device_pairing_mapping.get(device.device_id)
154 paried_device = yolink_home.get_device(paried_device_id)
157 await device_coordinator.async_config_entry_first_refresh()
158 except ConfigEntryNotReady:
160 device_coordinator.data = {}
161 device_coordinators[device.device_id] = device_coordinator
163 yolink_home, device_coordinators
165 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
167 async
def async_yolink_unload(event) -> None:
169 await yolink_home.async_unload()
171 entry.async_on_unload(
172 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_yolink_unload)
179 """Unload a config entry."""
180 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
181 await hass.data[DOMAIN][entry.entry_id].home_instance.async_unload()
182 hass.data[DOMAIN].pop(entry.entry_id)
None on_message(self, YoLinkDevice device, dict[str, Any] msg_data)
None __init__(self, HomeAssistant hass, ConfigEntry entry)
web.Response get(self, web.Request request, str config_key)
None async_register_services(HomeAssistant hass)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)