Skip to content

Commit

Permalink
Transformer fix (#98)
Browse files Browse the repository at this point in the history
Fix issues for landscape-transformer

 * Fix an issue where a sensor could return the wrong type which would
    produce a UHE in HA
 * Update the correct state for the switch
 * Fix switch duplicate issue

Sem-Ver: bugfix
  • Loading branch information
Expl0dingBanana authored Aug 2, 2024
1 parent c7f0e37 commit dfaf409
Show file tree
Hide file tree
Showing 5 changed files with 644 additions and 19 deletions.
10 changes: 9 additions & 1 deletion custom_components/hubspace/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.components.sensor import const as sensor_const
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -23,11 +24,13 @@ def __init__(
coordinator: HubSpaceDataUpdateCoordinator,
description: SensorEntityDescription,
device: HubSpaceDevice,
is_numeric: bool,
) -> None:
super().__init__(coordinator, context=device.id)
self.coordinator = coordinator
self.entity_description = description
self._device = device
self._is_numeric: bool = is_numeric
self._sensor_value = None

@callback
Expand All @@ -47,6 +50,8 @@ def update_states(self) -> None:
)
for state in states:
if state.functionClass == self.entity_description.key:
if self._is_numeric and isinstance(state.value, str):
state.value = int("".join(i for i in state.value if i.isdigit()))
self._sensor_value = state.value

@property
Expand Down Expand Up @@ -96,6 +101,9 @@ async def async_setup_entry(
dev.id,
sensor.key,
)
ha_entity = HubSpaceSensor(coordinator_hubspace, sensor, dev)
is_numeric = (
sensor.device_class not in sensor_const.NON_NUMERIC_DEVICE_CLASSES
)
ha_entity = HubSpaceSensor(coordinator_hubspace, sensor, dev, is_numeric)
entities.append(ha_entity)
async_add_entities(entities)
21 changes: 10 additions & 11 deletions custom_components/hubspace/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def __init__(
hs: HubSpaceDataUpdateCoordinator,
friendly_name: str,
instance: Optional[str],
device_class: str,
child_id: Optional[str] = None,
model: Optional[str] = None,
device_id: Optional[str] = None,
Expand All @@ -53,7 +52,6 @@ def __init__(
}
self._availability: Optional[bool] = None
# Entity-specific
self._device_class = device_class
self._instance = instance

@callback
Expand All @@ -75,11 +73,11 @@ def update_states(self) -> None:
for state in states:
if state.functionClass == "available":
self._availability = state.value
elif state.functionClass != "power":
elif state.functionClass != self.primary_class:
continue
if not self._instance:
self._state = state.value
elif state.functionInstance == self._instance:
elif self._instance and state.functionInstance != self._instance:
continue
else:
self._state = state.value

@property
Expand Down Expand Up @@ -131,12 +129,16 @@ def device_info(self) -> DeviceInfo:
model=model,
)

@property
def primary_class(self) -> str:
return "toggle" if self._instance else "power"

async def async_turn_on(self, **kwargs) -> None:
_LOGGER.debug("Enabling %s on %s", self._instance, self._child_id)
self._state = "on"
states_to_set = [
HubSpaceState(
functionClass="toggle" if self._instance else "power",
functionClass=self.primary_class,
functionInstance=self._instance,
value=self._state,
)
Expand Down Expand Up @@ -172,7 +174,6 @@ async def setup_entry_toggled(
coordinator_hubspace,
entity.friendly_name,
instance,
entity.device_class,
child_id=entity.id,
model=entity.model,
device_id=entity.device_id,
Expand All @@ -190,7 +191,6 @@ async def setup_basic_switch(
coordinator_hubspace,
entity.friendly_name,
None,
entity.device_class,
child_id=entity.id,
model=entity.model,
device_id=entity.device_id,
Expand All @@ -203,7 +203,7 @@ async def async_setup_entry(
entry: HubSpaceConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add Fan entities from a config_entry."""
"""Add Switch entities from a config_entry."""
coordinator_hubspace: HubSpaceDataUpdateCoordinator = (
entry.runtime_data.coordinator_hubspace
)
Expand Down Expand Up @@ -231,5 +231,4 @@ async def async_setup_entry(
model=entity.model,
manufacturer=entity.manufacturerName,
)
entities.extend(new_devs)
async_add_entities(entities)
Loading

0 comments on commit dfaf409

Please sign in to comment.