diff --git a/custom_components/livisi/livisi_connector.py b/custom_components/livisi/livisi_connector.py index add507b..644ce2a 100644 --- a/custom_components/livisi/livisi_connector.py +++ b/custom_components/livisi/livisi_connector.py @@ -25,10 +25,11 @@ from .livisi_websocket import LivisiWebsocket from .livisi_const import ( + CONTROLLER_DEVICE_TYPES, + V1_NAME, V2_NAME, LOGGER, REQUEST_TIMEOUT, - SHC_ID, WEBSERVICE_PORT, ) @@ -202,6 +203,7 @@ async def _async_get_controller(self) -> LivisiController: shc_info = await self.async_send_authorized_request("get", path="status") controller = parse_dataclass(shc_info, LivisiController) controller.is_v2 = shc_info.get("controllerType") == V2_NAME + controller.is_v1 = shc_info.get("controllerType") == V1_NAME return controller async def async_get_devices( @@ -220,11 +222,10 @@ async def async_get_devices( updated_devices, ) = self.parse_messages(messages) - devices, capabilities, rooms, shc_state = await asyncio.gather( + devices, capabilities, rooms = await asyncio.gather( self.async_send_authorized_request("get", path="device"), self.async_send_authorized_request("get", path="capability"), self.async_send_authorized_request("get", path="location"), - self.async_send_authorized_request("get", path=f"device/{SHC_ID}/state"), return_exceptions=True, ) @@ -236,6 +237,20 @@ async def async_get_devices( LOGGER.warn(f"Error loading {path}") raise result # Re-raise the exception immediately + controller_id = next( + (x.get("id") for x in devices if x.get("type") in CONTROLLER_DEVICE_TYPES), + None, + ) + if controller_id is not None: + try: + shc_state = await self.async_send_authorized_request( + "get", path=f"device/{controller_id}/state" + ) + if self.controller.is_v1: + shc_state = shc_state["state"] + except Exception: + LOGGER.warning("Error getting shc state", exc_info=True) + capability_map = {} capability_config = {} @@ -275,11 +290,8 @@ async def async_get_devices( roomid = device["location"].removeprefix("/location/") device["room"] = room_map.get(roomid) - if device_id == SHC_ID: - if isinstance(shc_state, Exception): - device["state"] = {} - else: - device["state"] = shc_state + if device["type"] in CONTROLLER_DEVICE_TYPES: + device["state"] = shc_state devicelist.append(parse_dataclass(device, LivisiDevice)) @@ -309,7 +321,7 @@ def parse_messages(self, messages): d.removeprefix("/device/") for d in message.get("devices", []) ] if len(device_ids) == 0: - source = message.get("source", SHC_ID) + source = message.get("source", "") device_ids = [source.replace("/device/", "")] if msgtype == "DeviceLowBattery": for device_id in device_ids: diff --git a/custom_components/livisi/livisi_const.py b/custom_components/livisi/livisi_const.py index d4fed20..6340c6f 100644 --- a/custom_components/livisi/livisi_const.py +++ b/custom_components/livisi/livisi_const.py @@ -6,12 +6,13 @@ LOGGER = logging.getLogger(__package__) V2_NAME = "Avatar" +V1_NAME = "Classic" V2_WEBSOCKET_PORT: Final = 9090 CLASSIC_WEBSOCKET_PORT: Final = 8080 WEBSERVICE_PORT: Final = 8080 REQUEST_TIMEOUT: Final = 2000 -SHC_ID: Final = "00000000000000000000000000000000" +CONTROLLER_DEVICE_TYPES: Final = ["SHC", "SHCA"] BATTERY_LOW: Final = "batteryLow" UPDATE_AVAILABLE: Final = "DeviceUpdateAvailable" diff --git a/custom_components/livisi/livisi_controller.py b/custom_components/livisi/livisi_controller.py index cc11385..001d350 100644 --- a/custom_components/livisi/livisi_controller.py +++ b/custom_components/livisi/livisi_controller.py @@ -1,4 +1,5 @@ """Code to represent a livisi device.""" + from __future__ import annotations from dataclasses import dataclass @@ -13,3 +14,4 @@ class LivisiController: os_version: str is_v2: bool + is_v1: bool diff --git a/custom_components/livisi/livisi_device.py b/custom_components/livisi/livisi_device.py index 11b7a54..16d2ad2 100644 --- a/custom_components/livisi/livisi_device.py +++ b/custom_components/livisi/livisi_device.py @@ -5,7 +5,7 @@ from dataclasses import dataclass -from .livisi_const import SHC_ID +from .livisi_const import CONTROLLER_DEVICE_TYPES @dataclass @@ -50,4 +50,4 @@ def tag_type(self) -> str: @property def is_shc(self) -> bool: """Indicate whether this device is the controller.""" - return self.id == SHC_ID + return self.type in CONTROLLER_DEVICE_TYPES