Skip to content

Commit

Permalink
Add sensors state binary sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
bergdahl committed Dec 9, 2023
1 parent 6339015 commit 053872e
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions custom_components/growcube/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ async def async_setup_entry(hass, entry, async_add_entities):
sensors = [
LockedStateSensor(coordinator),
WaterStateSensor(coordinator),
SensorFaultStateSensor(coordinator, 0),
SensorFaultStateSensor(coordinator, 1),
SensorFaultStateSensor(coordinator, 2),
SensorFaultStateSensor(coordinator, 3),
PumpLockedStateSensor(coordinator, 0),
PumpLockedStateSensor(coordinator, 1),
PumpLockedStateSensor(coordinator, 2),
Expand Down Expand Up @@ -134,3 +138,47 @@ def _handle_coordinator_update(self) -> None:
async def async_added_to_hass(self):
"""When the entity is added to hass."""
self.async_on_remove(self._coordinator.async_add_listener(self._handle_coordinator_update))


class SensorFaultStateSensor(BinarySensorEntity):
_channel_name = ['A', 'B', 'C', 'D']
_channel_id = ['a', 'b', 'c', 'd']

def __init__(self, coordinator: GrowcubeDataCoordinator, channel: int) -> None:
self._coordinator = coordinator
self._coordinator.entities.append(self)
self._channel = channel
self._attr_unique_id = f"{coordinator.model.device_id}_sensor_" + self._channel_id[channel] + "_locked"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Sensor " + self._channel_name[channel] + " state"
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.model.sensor_state[self._channel]

@property
def device_info(self) -> DeviceInfo | None:
return self._coordinator.model.device_info

@property
def icon(self):
if self.is_on:
return "mdi:pump-off"
else:
return "mdi:pump"

@property
def is_on(self):
"""Return True if the binary sensor is on."""
return self._coordinator.model.sensor_state[self._channel]

@callback
def _handle_coordinator_update(self) -> None:
_LOGGER.debug("Update sensor_state[%s] %s",
self._channel,
self._coordinator.model.sensor_state[self._channel])
self._attr_native_value = self._coordinator.model.sensor_state[self._channel]
self.async_write_ha_state()

async def async_added_to_hass(self):
"""When the entity is added to hass."""
self.async_on_remove(self._coordinator.async_add_listener(self._handle_coordinator_update))

0 comments on commit 053872e

Please sign in to comment.