| 1 | """YoLink Local Hub Client.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | import logging |
| 5 | from typing import Any |
| 6 | from .client import YoLinkClient |
| 7 | from .mqtt_client import YoLinkMqttClient |
| 8 | from .local_auth_mgr import YoLinkLocalAuthMgr |
| 9 | from .const import ATTR_DEVICE_WATER_DEPTH_SENSOR |
| 10 | from .device import YoLinkDevice, YoLinkDeviceMode |
| 11 | from .exception import YoLinkClientError, YoLinkUnSupportedMethodError |
| 12 | from .message_listener import MessageListener |
| 13 | from .model import BRDP |
| 14 | from .endpoint import Endpoint |
| 15 | from aiohttp import ClientSession |
| 16 | |
| 17 | _LOGGER = logging.getLogger(__name__) |
| 18 | |
| 19 | has_external_data_devices = [ATTR_DEVICE_WATER_DEPTH_SENSOR] |
| 20 | |
| 21 | |
| 22 | class YoLinkLocalHubClient: |
| 23 | """YoLink Local Hub client.""" |
| 24 | |
| 25 | def __init__( |
| 26 | self, |
| 27 | session: ClientSession, |
| 28 | host: str, |
| 29 | net_id: str, |
| 30 | client_id: str, |
| 31 | client_secret: str, |
| 32 | ) -> None: |
| 33 | """Init YoLink Local Hub Client.""" |
| 34 | self._session = session |
| 35 | self._net_id: str = net_id |
| 36 | self._client_id = client_id |
| 37 | self._client_secret = client_secret |
| 38 | self._endpoint = Endpoint( |
| 39 | name="Local", |
| 40 | host=host, |
| 41 | url=f"http://{host}:1080/open/yolink/v2/api", |
| 42 | mqtt_host=host, |
| 43 | mqtt_port=18080, |
| 44 | ) |
| 45 | self._auth_mgr = YoLinkLocalAuthMgr( |
| 46 | session=session, |
| 47 | token_url=f"http://{host}:1080/open/yolink/token", |
| 48 | client_id=client_id, |
| 49 | client_secret=client_secret, |
| 50 | ) |
| 51 | self._devices: dict[str, YoLinkDevice] = {} |
| 52 | self._http_client: YoLinkClient | None = None |
| 53 | self._mqtt_client = None |
| 54 | self._message_listener: MessageListener | None = None |
| 55 | |
| 56 | async def authenticate(self) -> bool: |
| 57 | """Authenticate to Local Hub.""" |
| 58 | return await self._auth_mgr.check_and_refresh_token() is not None |
| 59 | |
| 60 | async def async_setup(self, listener: MessageListener) -> None: |
| 61 | """Init YoLink Local Hub Client.""" |
| 62 | if not listener: |
| 63 | raise YoLinkClientError( |
| 64 | "-1002", "setup failed, message listener is required!" |
| 65 | ) |
| 66 | self._http_client = YoLinkClient(self._auth_mgr) |
| 67 | await self.async_load_devices() |
| 68 | self._message_listener = listener |
| 69 | self._mqtt_client = YoLinkMqttClient( |
| 70 | auth_manager=self._auth_mgr, |
| 71 | endpoint=self._endpoint, |
| 72 | broker_host=self._endpoint.mqtt_broker_host, |
| 73 | broker_port=self._endpoint.mqtt_broker_port, |
| 74 | devices=self._devices, |
| 75 | ) |
| 76 | await self._mqtt_client.connect( |
| 77 | f"ylsubnet/{self._net_id}/+/report", self._message_listener |
| 78 | ) |
| 79 | |
| 80 | async def async_unload(self) -> None: |
| 81 | """Unload YoLink home.""" |
| 82 | self._devices = {} |
| 83 | self._http_client = None |
| 84 | if self._mqtt_client is not None: |
| 85 | await self._mqtt_client.disconnect() |
| 86 | _LOGGER.info( |
| 87 | "Local Hub mqtt client disconnected.", |
| 88 | ) |
| 89 | self._mqtt_client = None |
| 90 | self._message_listener = None |
| 91 | |
| 92 | async def async_load_devices(self, **kwargs: Any) -> dict[str, YoLinkDevice]: |
| 93 | """Get sub-net devices.""" |
| 94 | if self._http_client is None: |
| 95 | raise YoLinkClientError( |
| 96 | "-1004", "load devices failed, http client is not initialized!" |
| 97 | ) |
| 98 | response: BRDP = await self._http_client.execute( |
| 99 | self._endpoint.url, bsdp={"method": "Home.getDeviceList"}, **kwargs |
| 100 | ) |
| 101 | for device_data in response.data["devices"]: |
| 102 | device = YoLinkDevice(YoLinkDeviceMode(**device_data), self._http_client) |
| 103 | device.device_endpoint = self._endpoint |
| 104 | if device.device_type in has_external_data_devices: |
| 105 | try: |
| 106 | dev_external_data_resp = await device.get_external_data() |
| 107 | device.device_attrs = dev_external_data_resp.data.get("extData") |
| 108 | except YoLinkUnSupportedMethodError: |
| 109 | _LOGGER.debug( |
| 110 | "getExternalData is not supported for: %s", |
| 111 | device.device_type, |
| 112 | ) |
| 113 | self._devices[device.device_id] = device |
| 114 | return self._devices |
| 115 | |
| 116 | def get_devices(self) -> list[YoLinkDevice]: |
| 117 | """Get Local Hub sub-net devices.""" |
| 118 | return list(self._devices.values()) |
| 119 | |
| 120 | def get_device(self, device_id: str) -> YoLinkDevice | None: |
| 121 | """Get Local Hub sub-net device via device id.""" |
| 122 | return self._devices.get(device_id) |