Skip to content

Commit

Permalink
Merge pull request #88 from DanielHabenicht/fix-status-3000
Browse files Browse the repository at this point in the history
fix error 3000 on SHC1
  • Loading branch information
planbnet authored Feb 22, 2024
2 parents 3c61679 + 3f0d4ed commit f8286b8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
30 changes: 21 additions & 9 deletions custom_components/livisi/livisi_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -201,6 +202,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(
Expand All @@ -219,11 +221,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,
)

Expand All @@ -235,6 +236,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 = {}

Expand Down Expand Up @@ -274,11 +289,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))

Expand Down Expand Up @@ -308,7 +320,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:
Expand Down
3 changes: 2 additions & 1 deletion custom_components/livisi/livisi_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions custom_components/livisi/livisi_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Code to represent a livisi device."""

from __future__ import annotations

from dataclasses import dataclass
Expand All @@ -13,3 +14,4 @@ class LivisiController:
os_version: str

is_v2: bool
is_v1: bool
4 changes: 2 additions & 2 deletions custom_components/livisi/livisi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from dataclasses import dataclass

from .livisi_const import SHC_ID
from .livisi_const import CONTROLLER_DEVICE_TYPES


@dataclass
Expand Down Expand Up @@ -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

0 comments on commit f8286b8

Please sign in to comment.