Skip to content

Commit

Permalink
Merge branch 'fix-status-3000' into add-reboot-button
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHabenicht committed Feb 22, 2024
2 parents 36f36eb + 3f0d4ed commit cb3a821
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 38 deletions.
3 changes: 0 additions & 3 deletions custom_components/livisi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
VARIABLE_DEVICE_TYPES: Final = ["VariableActuator"]
VRCC_DEVICE_TYPES: Final = ["VRCC"]
WDS_DEVICE_TYPES: Final = ["WDS", "BT-WDS"]
CONTROLLER_DEVICE_TYPES: Final = ["SHC", "SHC2"] # TODO: Is this the right type?

BATTERY_POWERED_DEVICES = [
"BRC8",
Expand Down Expand Up @@ -95,5 +94,3 @@
EVENT_BUTTON_PRESSED = "button_pressed"
EVENT_BUTTON_LONG_PRESSED = "button_long_pressed"
EVENT_MOTION_DETECTED = "motion_detected"

COMMAND_RESTART = "Restart"
49 changes: 18 additions & 31 deletions custom_components/livisi/livisi_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from aiohttp.client import ClientSession, ClientError, TCPConnector
from dateutil.parser import parse as parse_timestamp

from custom_components.livisi.const import COMMAND_RESTART, CONTROLLER_DEVICE_TYPES

from .livisi_device import LivisiDevice

from .livisi_json_util import parse_dataclass
Expand All @@ -28,11 +26,12 @@
from .livisi_websocket import LivisiWebsocket

from .livisi_const import (
COMMAND_RESTART,
CONTROLLER_DEVICE_TYPES,
V1_NAME,
V2_NAME,
LOGGER,
REQUEST_TIMEOUT,
SHC2_ID,
WEBSERVICE_PORT,
)

Expand Down Expand Up @@ -232,20 +231,6 @@ async def async_get_devices(
return_exceptions=True,
)

if self.controller.is_v2:
shc_state = await self.async_send_authorized_request(
"get", path=f"device/{SHC2_ID}/state"
)

if self.controller.is_v1:
controller_device = next((x for x in devices if x["type"] == "SHC"), None)
if controller_device:
shc_state = (
await self.async_send_authorized_request(
"get", path=f"device/{controller_device['id']}/state"
)
)["state"]

for result, path in zip(
(devices, capabilities, rooms),
("device", "capability", "location"),
Expand All @@ -254,6 +239,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 @@ -293,20 +292,8 @@ async def async_get_devices(
roomid = device["location"].removeprefix("/location/")
device["room"] = room_map.get(roomid)

# TODO: Can we remove this if the controller Device types match?
# For SHC2
if device_id == SHC2_ID:
if isinstance(shc_state, Exception):
device["state"] = {}
else:
device["state"] = shc_state

# For SHC1
if device["type"] in CONTROLLER_DEVICE_TYPES:
if isinstance(shc_state, Exception):
device["state"] = {}
else:
device["state"] = shc_state
device["state"] = shc_state

devicelist.append(parse_dataclass(device, LivisiDevice))

Expand Down Expand Up @@ -336,7 +323,7 @@ def parse_messages(self, messages):
d.removeprefix("/device/") for d in message.get("devices", [])
]
if len(device_ids) == 0:
source = message.get("source", SHC2_ID)
source = message.get("source", "")
device_ids = [source.replace("/device/", "")]
if msgtype == "DeviceLowBattery":
for device_id in device_ids:
Expand Down
4 changes: 3 additions & 1 deletion custom_components/livisi/livisi_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WEBSERVICE_PORT: Final = 8080
REQUEST_TIMEOUT: Final = 2000

SHC2_ID: Final = "00000000000000000000000000000000"
CONTROLLER_DEVICE_TYPES: Final = ["SHC", "SHCA"]

BATTERY_LOW: Final = "batteryLow"
UPDATE_AVAILABLE: Final = "DeviceUpdateAvailable"
Expand All @@ -26,3 +26,5 @@
EVENT_BUTTON_PRESSED = "button_pressed"
EVENT_BUTTON_LONG_PRESSED = "button_long_pressed"
EVENT_MOTION_DETECTED = "motion_detected"

COMMAND_RESTART = "Restart"
1 change: 1 addition & 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 Down
4 changes: 1 addition & 3 deletions custom_components/livisi/livisi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

from dataclasses import dataclass

from custom_components.livisi.const import CONTROLLER_DEVICE_TYPES

from .livisi_const import CONTROLLER_DEVICE_TYPES


@dataclass
Expand Down Expand Up @@ -51,5 +50,4 @@ def tag_type(self) -> str:
@property
def is_shc(self) -> bool:
"""Indicate whether this device is the controller."""
# TODO: Does this work for V2?
return self.type in CONTROLLER_DEVICE_TYPES

0 comments on commit cb3a821

Please sign in to comment.