Skip to content

Commit

Permalink
Refactoring of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bergdahl committed Jun 29, 2024
1 parent 1b2d9b9 commit c0a5831
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
40 changes: 11 additions & 29 deletions custom_components/growcube/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from homeassistant.helpers.device_registry import DeviceInfo
from .coordinator import GrowcubeDataCoordinator
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass
from .const import DOMAIN
from .const import DOMAIN, CHANNEL_NAME, CHANNEL_ID
import logging

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -98,21 +98,15 @@ def update(self) -> None:
self._attr_native_value = self._coordinator.data.water_warning
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))


class PumpOpenStateSensor(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.data.device_id}_pump_" + self._channel_id[channel] + "_open"
self._attr_unique_id = f"{coordinator.data.device_id}_pump_" + 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_name = f"Pump " + CHANNEL_NAME[channel] + " open"
self._attr_device_class = BinarySensorDeviceClass.OPENING
self._attr_native_value = coordinator.data.pump_open[self._channel]
self._attr_entity_registry_enabled_default = False
Expand Down Expand Up @@ -144,16 +138,13 @@ def update(self) -> None:


class OutletLockedSensor(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.data.device_id}_outlet_" + self._channel_id[channel] + "_locked"
self._attr_unique_id = f"{coordinator.data.device_id}_outlet_" + CHANNEL_ID[channel] + "_locked"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Outlet " + self._channel_name[channel] + " locked"
self._attr_name = f"Outlet " + CHANNEL_NAME[channel] + " locked"
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.data.outlet_locked_state[self._channel]
Expand Down Expand Up @@ -185,16 +176,13 @@ def update(self) -> None:


class OutletBlockedSensor(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.data.device_id}_outlet_" + self._channel_id[channel] + "_blocked"
self._attr_unique_id = f"{coordinator.data.device_id}_outlet_" + self.CHANNEL_ID[channel] + "_blocked"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Outlet " + self._channel_name[channel] + " blocked"
self._attr_name = f"Outlet " + CHANNEL_NAME[channel] + " blocked"
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.data.outlet_blocked_state[self._channel]
Expand Down Expand Up @@ -226,16 +214,13 @@ def update(self) -> None:


class SensorFaultSensor(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.data.device_id}_sensor_" + self._channel_id[channel] + "_fault"
self._attr_unique_id = f"{coordinator.data.device_id}_sensor_" + CHANNEL_ID[channel] + "_fault"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Sensor " + self._channel_name[channel] + " fault"
self._attr_name = f"Sensor " + CHANNEL_NAME[channel] + " fault"
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.data.sensor_abnormal[self._channel]
Expand Down Expand Up @@ -267,16 +252,13 @@ def update(self) -> None:


class SensorDisconnectedSensor(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.data.device_id}_sensor_" + self._channel_id[channel] + "_disconnected"
self._attr_unique_id = f"{coordinator.data.device_id}_sensor_" + CHANNEL_ID[channel] + "_disconnected"
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
self._attr_name = f"Sensor " + self._channel_name[channel] + " disconnected"
self._attr_name = f"Sensor " + CHANNEL_NAME[channel] + " disconnected"
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_native_value = coordinator.data.sensor_disconnected[self._channel]
Expand Down
2 changes: 2 additions & 0 deletions custom_components/growcube/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Constants for the Growcube integration."""

DOMAIN = "growcube"
CHANNEL_NAME = ['A', 'B', 'C', 'D']
CHANNEL_ID = ['a', 'b', 'c', 'd']

0 comments on commit c0a5831

Please sign in to comment.