Skip to content

Commit c0a5831

Browse files
committed
Refactoring of constants
1 parent 1b2d9b9 commit c0a5831

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

custom_components/growcube/binary_sensor.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from homeassistant.helpers.device_registry import DeviceInfo
44
from .coordinator import GrowcubeDataCoordinator
55
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass
6-
from .const import DOMAIN
6+
from .const import DOMAIN, CHANNEL_NAME, CHANNEL_ID
77
import logging
88

99
_LOGGER = logging.getLogger(__name__)
@@ -98,21 +98,15 @@ def update(self) -> None:
9898
self._attr_native_value = self._coordinator.data.water_warning
9999
self.schedule_update_ha_state()
100100

101-
#async def async_added_to_hass(self):
102-
# self.async_on_remove(self._coordinator.async_add_listener(self._handle_coordinator_update))
103-
104101

105102
class PumpOpenStateSensor(BinarySensorEntity):
106-
_channel_name = ['A', 'B', 'C', 'D']
107-
_channel_id = ['a', 'b', 'c', 'd']
108-
109103
def __init__(self, coordinator: GrowcubeDataCoordinator, channel: int) -> None:
110104
self._coordinator = coordinator
111105
self._coordinator.entities.append(self)
112106
self._channel = channel
113-
self._attr_unique_id = f"{coordinator.data.device_id}_pump_" + self._channel_id[channel] + "_open"
107+
self._attr_unique_id = f"{coordinator.data.device_id}_pump_" + CHANNEL_ID[channel] + "_open"
114108
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
115-
self._attr_name = f"Pump " + self._channel_name[channel] + " open"
109+
self._attr_name = f"Pump " + CHANNEL_NAME[channel] + " open"
116110
self._attr_device_class = BinarySensorDeviceClass.OPENING
117111
self._attr_native_value = coordinator.data.pump_open[self._channel]
118112
self._attr_entity_registry_enabled_default = False
@@ -144,16 +138,13 @@ def update(self) -> None:
144138

145139

146140
class OutletLockedSensor(BinarySensorEntity):
147-
_channel_name = ['A', 'B', 'C', 'D']
148-
_channel_id = ['a', 'b', 'c', 'd']
149-
150141
def __init__(self, coordinator: GrowcubeDataCoordinator, channel: int) -> None:
151142
self._coordinator = coordinator
152143
self._coordinator.entities.append(self)
153144
self._channel = channel
154-
self._attr_unique_id = f"{coordinator.data.device_id}_outlet_" + self._channel_id[channel] + "_locked"
145+
self._attr_unique_id = f"{coordinator.data.device_id}_outlet_" + CHANNEL_ID[channel] + "_locked"
155146
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
156-
self._attr_name = f"Outlet " + self._channel_name[channel] + " locked"
147+
self._attr_name = f"Outlet " + CHANNEL_NAME[channel] + " locked"
157148
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
158149
self._attr_entity_category = EntityCategory.DIAGNOSTIC
159150
self._attr_native_value = coordinator.data.outlet_locked_state[self._channel]
@@ -185,16 +176,13 @@ def update(self) -> None:
185176

186177

187178
class OutletBlockedSensor(BinarySensorEntity):
188-
_channel_name = ['A', 'B', 'C', 'D']
189-
_channel_id = ['a', 'b', 'c', 'd']
190-
191179
def __init__(self, coordinator: GrowcubeDataCoordinator, channel: int) -> None:
192180
self._coordinator = coordinator
193181
self._coordinator.entities.append(self)
194182
self._channel = channel
195-
self._attr_unique_id = f"{coordinator.data.device_id}_outlet_" + self._channel_id[channel] + "_blocked"
183+
self._attr_unique_id = f"{coordinator.data.device_id}_outlet_" + self.CHANNEL_ID[channel] + "_blocked"
196184
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
197-
self._attr_name = f"Outlet " + self._channel_name[channel] + " blocked"
185+
self._attr_name = f"Outlet " + CHANNEL_NAME[channel] + " blocked"
198186
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
199187
self._attr_entity_category = EntityCategory.DIAGNOSTIC
200188
self._attr_native_value = coordinator.data.outlet_blocked_state[self._channel]
@@ -226,16 +214,13 @@ def update(self) -> None:
226214

227215

228216
class SensorFaultSensor(BinarySensorEntity):
229-
_channel_name = ['A', 'B', 'C', 'D']
230-
_channel_id = ['a', 'b', 'c', 'd']
231-
232217
def __init__(self, coordinator: GrowcubeDataCoordinator, channel: int) -> None:
233218
self._coordinator = coordinator
234219
self._coordinator.entities.append(self)
235220
self._channel = channel
236-
self._attr_unique_id = f"{coordinator.data.device_id}_sensor_" + self._channel_id[channel] + "_fault"
221+
self._attr_unique_id = f"{coordinator.data.device_id}_sensor_" + CHANNEL_ID[channel] + "_fault"
237222
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
238-
self._attr_name = f"Sensor " + self._channel_name[channel] + " fault"
223+
self._attr_name = f"Sensor " + CHANNEL_NAME[channel] + " fault"
239224
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
240225
self._attr_entity_category = EntityCategory.DIAGNOSTIC
241226
self._attr_native_value = coordinator.data.sensor_abnormal[self._channel]
@@ -267,16 +252,13 @@ def update(self) -> None:
267252

268253

269254
class SensorDisconnectedSensor(BinarySensorEntity):
270-
_channel_name = ['A', 'B', 'C', 'D']
271-
_channel_id = ['a', 'b', 'c', 'd']
272-
273255
def __init__(self, coordinator: GrowcubeDataCoordinator, channel: int) -> None:
274256
self._coordinator = coordinator
275257
self._coordinator.entities.append(self)
276258
self._channel = channel
277-
self._attr_unique_id = f"{coordinator.data.device_id}_sensor_" + self._channel_id[channel] + "_disconnected"
259+
self._attr_unique_id = f"{coordinator.data.device_id}_sensor_" + CHANNEL_ID[channel] + "_disconnected"
278260
self.entity_id = f"{Platform.SENSOR}.{self._attr_unique_id}"
279-
self._attr_name = f"Sensor " + self._channel_name[channel] + " disconnected"
261+
self._attr_name = f"Sensor " + CHANNEL_NAME[channel] + " disconnected"
280262
self._attr_device_class = BinarySensorDeviceClass.PROBLEM
281263
self._attr_entity_category = EntityCategory.DIAGNOSTIC
282264
self._attr_native_value = coordinator.data.sensor_disconnected[self._channel]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
"""Constants for the Growcube integration."""
22

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

0 commit comments

Comments
 (0)