From 053872effb71380f2d765892601c07f90ea5b90b Mon Sep 17 00:00:00 2001 From: Jonny Bergdahl Date: Sat, 9 Dec 2023 22:03:29 +0100 Subject: [PATCH] Add sensors state binary sensors --- custom_components/growcube/binary_sensor.py | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/custom_components/growcube/binary_sensor.py b/custom_components/growcube/binary_sensor.py index 521b0e8..0e2c163 100644 --- a/custom_components/growcube/binary_sensor.py +++ b/custom_components/growcube/binary_sensor.py @@ -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), @@ -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))