Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error 3000 on SHC1 #88

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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(
Expand All @@ -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,
)

Expand All @@ -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 = {}

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

Expand Down Expand Up @@ -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:
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
3 changes: 3 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 @@ -12,4 +13,6 @@ class LivisiController:
serial_number: str
os_version: str

device_id: str
is_v2: bool
is_v1: bool
5 changes: 3 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,5 @@ def tag_type(self) -> str:
@property
def is_shc(self) -> bool:
"""Indicate whether this device is the controller."""
return self.id == SHC_ID
# TODO: Does this work for V2?
return self.type in CONTROLLER_DEVICE_TYPES
Loading