Bump screenlogicpy to v0.9.0 (#92475)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
8de3945bd4
commit
092580a3ed
28 changed files with 3821 additions and 652 deletions
|
@ -1,52 +1,65 @@
|
|||
"""Base ScreenLogicEntity definitions."""
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from screenlogicpy import ScreenLogicGateway
|
||||
from screenlogicpy.const import CODE, DATA as SL_DATA, EQUIPMENT, ON_OFF
|
||||
from screenlogicpy.const.common import ON_OFF
|
||||
from screenlogicpy.const.data import ATTR
|
||||
from screenlogicpy.const.msg import CODE
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import ScreenlogicDataUpdateCoordinator
|
||||
from .const import ScreenLogicDataPath
|
||||
from .coordinator import ScreenlogicDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScreenLogicEntityRequiredKeyMixin:
|
||||
"""Mixin for required ScreenLogic entity key."""
|
||||
|
||||
data_path: ScreenLogicDataPath
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScreenLogicEntityDescription(
|
||||
EntityDescription, ScreenLogicEntityRequiredKeyMixin
|
||||
):
|
||||
"""Base class for a ScreenLogic entity description."""
|
||||
|
||||
|
||||
class ScreenlogicEntity(CoordinatorEntity[ScreenlogicDataUpdateCoordinator]):
|
||||
"""Base class for all ScreenLogic entities."""
|
||||
|
||||
entity_description: ScreenLogicEntityDescription
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: ScreenlogicDataUpdateCoordinator,
|
||||
data_key: str,
|
||||
enabled: bool = True,
|
||||
entity_description: ScreenLogicEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize of the entity."""
|
||||
super().__init__(coordinator)
|
||||
self._data_key = data_key
|
||||
self._attr_entity_registry_enabled_default = enabled
|
||||
self._attr_unique_id = f"{self.mac}_{self._data_key}"
|
||||
|
||||
controller_type = self.config_data["controller_type"]
|
||||
hardware_type = self.config_data["hardware_type"]
|
||||
try:
|
||||
equipment_model = EQUIPMENT.CONTROLLER_HARDWARE[controller_type][
|
||||
hardware_type
|
||||
]
|
||||
except KeyError:
|
||||
equipment_model = f"Unknown Model C:{controller_type} H:{hardware_type}"
|
||||
self.entity_description = entity_description
|
||||
self._data_path = self.entity_description.data_path
|
||||
self._data_key = self._data_path[-1]
|
||||
self._attr_unique_id = f"{self.mac}_{self.entity_description.key}"
|
||||
mac = self.mac
|
||||
assert mac is not None
|
||||
self._attr_device_info = DeviceInfo(
|
||||
connections={(dr.CONNECTION_NETWORK_MAC, mac)},
|
||||
manufacturer="Pentair",
|
||||
model=equipment_model,
|
||||
name=self.gateway_name,
|
||||
model=self.gateway.controller_model,
|
||||
name=self.gateway.name,
|
||||
sw_version=self.gateway.version,
|
||||
)
|
||||
|
||||
|
@ -56,26 +69,11 @@ class ScreenlogicEntity(CoordinatorEntity[ScreenlogicDataUpdateCoordinator]):
|
|||
assert self.coordinator.config_entry is not None
|
||||
return self.coordinator.config_entry.unique_id
|
||||
|
||||
@property
|
||||
def config_data(self) -> dict[str | int, Any]:
|
||||
"""Shortcut for config data."""
|
||||
return self.gateway_data[SL_DATA.KEY_CONFIG]
|
||||
|
||||
@property
|
||||
def gateway(self) -> ScreenLogicGateway:
|
||||
"""Return the gateway."""
|
||||
return self.coordinator.gateway
|
||||
|
||||
@property
|
||||
def gateway_data(self) -> dict[str | int, Any]:
|
||||
"""Return the gateway data."""
|
||||
return self.gateway.get_data()
|
||||
|
||||
@property
|
||||
def gateway_name(self) -> str:
|
||||
"""Return the configured name of the gateway."""
|
||||
return self.gateway.name
|
||||
|
||||
async def _async_refresh(self) -> None:
|
||||
"""Refresh the data from the gateway."""
|
||||
await self.coordinator.async_refresh()
|
||||
|
@ -87,20 +85,41 @@ class ScreenlogicEntity(CoordinatorEntity[ScreenlogicDataUpdateCoordinator]):
|
|||
"""Refresh from a timed called."""
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@property
|
||||
def entity_data(self) -> dict:
|
||||
"""Shortcut to the data for this entity."""
|
||||
if (data := self.gateway.get_data(*self._data_path)) is None:
|
||||
raise KeyError(f"Data not found: {self._data_path}")
|
||||
return data
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScreenLogicPushEntityRequiredKeyMixin:
|
||||
"""Mixin for required key for ScreenLogic push entities."""
|
||||
|
||||
subscription_code: CODE
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScreenLogicPushEntityDescription(
|
||||
ScreenLogicEntityDescription,
|
||||
ScreenLogicPushEntityRequiredKeyMixin,
|
||||
):
|
||||
"""Base class for a ScreenLogic push entity description."""
|
||||
|
||||
|
||||
class ScreenLogicPushEntity(ScreenlogicEntity):
|
||||
"""Base class for all ScreenLogic push entities."""
|
||||
|
||||
entity_description: ScreenLogicPushEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: ScreenlogicDataUpdateCoordinator,
|
||||
data_key: str,
|
||||
message_code: CODE,
|
||||
enabled: bool = True,
|
||||
entity_description: ScreenLogicPushEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the entity."""
|
||||
super().__init__(coordinator, data_key, enabled)
|
||||
self._update_message_code = message_code
|
||||
"""Initialize of the entity."""
|
||||
super().__init__(coordinator, entity_description)
|
||||
self._last_update_success = True
|
||||
|
||||
@callback
|
||||
|
@ -114,7 +133,8 @@ class ScreenLogicPushEntity(ScreenlogicEntity):
|
|||
await super().async_added_to_hass()
|
||||
self.async_on_remove(
|
||||
await self.gateway.async_subscribe_client(
|
||||
self._async_data_updated, self._update_message_code
|
||||
self._async_data_updated,
|
||||
self.entity_description.subscription_code,
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -129,17 +149,10 @@ class ScreenLogicPushEntity(ScreenlogicEntity):
|
|||
class ScreenLogicCircuitEntity(ScreenLogicPushEntity):
|
||||
"""Base class for all ScreenLogic switch and light entities."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Get the name of the switch."""
|
||||
return self.circuit["name"]
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Get whether the switch is in on state."""
|
||||
return self.circuit["value"] == ON_OFF.ON
|
||||
return self.entity_data[ATTR.VALUE] == ON_OFF.ON
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Send the ON command."""
|
||||
|
@ -149,14 +162,9 @@ class ScreenLogicCircuitEntity(ScreenLogicPushEntity):
|
|||
"""Send the OFF command."""
|
||||
await self._async_set_circuit(ON_OFF.OFF)
|
||||
|
||||
async def _async_set_circuit(self, circuit_value: int) -> None:
|
||||
if not await self.gateway.async_set_circuit(self._data_key, circuit_value):
|
||||
async def _async_set_circuit(self, state: ON_OFF) -> None:
|
||||
if not await self.gateway.async_set_circuit(self._data_key, state.value):
|
||||
raise HomeAssistantError(
|
||||
f"Failed to set_circuit {self._data_key} {circuit_value}"
|
||||
f"Failed to set_circuit {self._data_key} {state.value}"
|
||||
)
|
||||
_LOGGER.debug("Turn %s %s", self._data_key, circuit_value)
|
||||
|
||||
@property
|
||||
def circuit(self) -> dict[str | int, Any]:
|
||||
"""Shortcut to access the circuit."""
|
||||
return self.gateway_data[SL_DATA.KEY_CIRCUITS][self._data_key]
|
||||
_LOGGER.debug("Set circuit %s %s", self._data_key, state.value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue