Skip to content

Commit

Permalink
Add updated files
Browse files Browse the repository at this point in the history
  • Loading branch information
bergdahl committed May 31, 2024
1 parent 302abcd commit 20ecb05
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 183 deletions.
4 changes: 2 additions & 2 deletions custom_components/growcube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: dict):
registry = device_registry.async_get(hass)
device_entry = registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, data_coordinator.model.device_id)},
identifiers={(DOMAIN, data_coordinator.data.device_id)},
name=f"GrowCube " + data_coordinator.device_id,
manufacturer="Elecrow",
model="GrowCube",
sw_version=data_coordinator.model.version
sw_version=data_coordinator.data.version
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand Down
115 changes: 62 additions & 53 deletions custom_components/growcube/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,65 @@
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the Growcube sensors."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(coordinator.binary_sensors, True)
async_add_entities([LockedStateSensor(coordinator),
WaterStateSensor(coordinator),
PumpOpenStateSensor(coordinator, 0),
PumpOpenStateSensor(coordinator, 1),
PumpOpenStateSensor(coordinator, 2),
PumpOpenStateSensor(coordinator, 3),
PumpLockedStateSensor(coordinator, 0),
PumpLockedStateSensor(coordinator, 1),
PumpLockedStateSensor(coordinator, 2),
PumpLockedStateSensor(coordinator, 3),
SensorFaultStateSensor(coordinator, 0),
SensorFaultStateSensor(coordinator, 1),
SensorFaultStateSensor(coordinator, 2),
SensorFaultStateSensor(coordinator, 3)
], True)


class LockedStateSensor(BinarySensorEntity):
def __init__(self, coordinator: GrowcubeDataCoordinator):
self._coordinator = coordinator
self._coordinator.entities.append(self)
self._attr_unique_id = f"{coordinator.model.device_id}_locked"
self._attr_unique_id = f"{coordinator.data.device_id}_locked"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Device locked"
self._attr_device_class = BinarySensorDeviceClass.LOCK
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.model.device_lock_state
self._attr_native_value = coordinator.data.device_lock_state

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

@property
def is_on(self):
"""Return True if the binary sensor is on."""
return not self._coordinator.model.device_lock_state
return not self._coordinator.data.device_lock_state

@callback
def update(self, state: bool) -> None:
_LOGGER.debug("Update device_lock_state %s", state)
if self._coordinator.model.device_lock_state != state:
self._coordinator.model.device_lock_state = state
self._attr_native_value = state
self.async_write_ha_state()
def update(self) -> None:
_LOGGER.debug("Update device_lock_state %s", self._coordinator.data.device_lock_state)
if self._coordinator.data.device_lock_state != self._attr_native_value:
self._attr_native_value = self._coordinator.data.device_lock_state
self.schedule_update_ha_state()


class WaterStateSensor(BinarySensorEntity):
def __init__(self, coordinator: GrowcubeDataCoordinator):
self._coordinator = coordinator
self._coordinator.entities.append(self)
self._attr_unique_id = f"{coordinator.model.device_id}_water_level"
self._attr_unique_id = f"{coordinator.data.device_id}_water_level"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Water level"
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.model.device_lock_state
self._attr_native_value = coordinator.data.device_lock_state

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

@property
def icon(self):
Expand All @@ -68,18 +81,17 @@ def icon(self):

@property
def is_on(self):
return self._coordinator.model.water_state
return self._coordinator.data.water_state

@callback
def update(self, state: bool) -> None:
_LOGGER.debug("Update water_state %s", state)
if self._coordinator.model.water_state != state:
self._coordinator.model.water_state = state
self._attr_native_value = state
self.async_write_ha_state()
def update(self) -> None:
_LOGGER.debug("Update water_state %s", self._coordinator.data.water_state)
if self._coordinator.data.water_state != self._attr_native_value:
self._attr_native_value = self._coordinator.data.water_state
self.schedule_update_ha_state()

async def async_added_to_hass(self):
self.async_on_remove(self._coordinator.async_add_listener(self._handle_coordinator_update))
#async def async_added_to_hass(self):
# self.async_on_remove(self._coordinator.async_add_listener(self._handle_coordinator_update))


class PumpOpenStateSensor(BinarySensorEntity):
Expand All @@ -90,16 +102,16 @@ 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}_pump_" + self._channel_id[channel] + "_open"
self._attr_unique_id = f"{coordinator.data.device_id}_pump_" + self._channel_id[channel] + "_open"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Pump " + self._channel_name[channel] + " open"
self._attr_device_class = BinarySensorDeviceClass.OPENING
self._attr_native_value = coordinator.model.pump_open[self._channel]
self._attr_native_value = coordinator.data.pump_open[self._channel]
self._attr_entity_registry_enabled_default = False

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

@property
def icon(self):
Expand All @@ -111,17 +123,16 @@ def icon(self):
@property
def is_on(self):
"""Return True if the binary sensor is on."""
return self._coordinator.model.pump_open[self._channel]
return self._coordinator.data.pump_open[self._channel]

@callback
def update(self, state: bool) -> None:
def update(self) -> None:
_LOGGER.debug("Update pump_state[%s] %s",
self._channel,
state)
if self._coordinator.model.pump_open[self._channel] != state:
self._coordinator.model.pump_open[self._channel] = state
self._attr_native_value = state
self.async_write_ha_state()
self._coordinator.data.pump_open[self._channel])
if self._coordinator.data.pump_open[self._channel] != self._attr_native_value:
self._attr_native_value = self._coordinator.data.pump_open[self._channel]
self.schedule_update_ha_state()


class PumpLockedStateSensor(BinarySensorEntity):
Expand All @@ -132,16 +143,16 @@ 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}_pump_" + self._channel_id[channel] + "_locked"
self._attr_unique_id = f"{coordinator.data.device_id}_pump_" + self._channel_id[channel] + "_locked"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Pump " + self._channel_name[channel] + "lock state"
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.model.pump_lock_state[self._channel]
self._attr_native_value = coordinator.data.pump_lock_state[self._channel]

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

@property
def icon(self):
Expand All @@ -153,17 +164,16 @@ def icon(self):
@property
def is_on(self):
"""Return True if the binary sensor is on."""
return self._coordinator.model.pump_lock_state[self._channel]
return self._coordinator.data.pump_lock_state[self._channel]

@callback
def update(self, state: bool) -> None:
def update(self) -> None:
_LOGGER.debug("Update pump_lock_state[%s] %s",
self._channel,
self._coordinator.model.pump_lock_state[self._channel])
if self._coordinator.model.pump_lock_state[self._channel] != state:
self._coordinator.model.pump_lock_state[self._channel] = state
self._attr_native_value = state
self.async_write_ha_state()
self._coordinator.data.pump_lock_state[self._channel])
if self._coordinator.data.pump_lock_state[self._channel] != self._attr_native_value:
self._attr_native_value = self._coordinator.data.pump_lock_state[self._channel]
self.schedule_update_ha_state()


class SensorFaultStateSensor(BinarySensorEntity):
Expand All @@ -174,16 +184,16 @@ 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._attr_unique_id = f"{coordinator.data.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]
self._attr_native_value = coordinator.data.sensor_state[self._channel]

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

@property
def icon(self):
Expand All @@ -195,15 +205,14 @@ def icon(self):
@property
def is_on(self):
"""Return True if the binary sensor is on."""
return self._coordinator.model.sensor_state[self._channel]
return self._coordinator.data.sensor_state[self._channel]

@callback
def update(self, state: bool) -> None:
def update(self) -> None:
_LOGGER.debug("Update sensor_state[%s] %s",
self._channel,
self._coordinator.model.sensor_state[self._channel])
if self._coordinator.model.sensor_state[self._channel] != state:
self._coordinator.model.sensor_state[self._channel] = state
self._attr_native_value = state
self.async_write_ha_state()
self._coordinator.data.sensor_state[self._channel])
if self._coordinator.data.sensor_state[self._channel] != self._attr_native_value:
self._attr_native_value = self._coordinator.data.sensor_state[self._channel]
self.schedule_update_ha_state()

4 changes: 2 additions & 2 deletions custom_components/growcube/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def __init__(self, coordinator, channel: int):
self._coordinator = coordinator
self._channel = channel
self._attr_name = "Water plant " + self._channel_name[channel]
self._attr_unique_id = f"{coordinator.model.device_id}_water_plant_" + self._channel_id[channel]
self._attr_unique_id = f"{coordinator.data.device_id}_water_plant_" + self._channel_id[channel]
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"

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

@property
def icon(self):
Expand Down
Loading

0 comments on commit 20ecb05

Please sign in to comment.