Skip to content

Commit 25be7ed

Browse files
committed
Continues pull->push refactoring
1 parent 1cecad0 commit 25be7ed

File tree

4 files changed

+115
-65
lines changed

4 files changed

+115
-65
lines changed

custom_components/growcube/binary_sensor.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from homeassistant.core import callback, HomeAssistant
33
from homeassistant.helpers.device_registry import DeviceInfo
44
from homeassistant import config_entries
5-
from homeassistant.config_entries import ConfigEntry
65
from homeassistant.helpers.entity_platform import AddEntitiesCallback
76
from .coordinator import GrowcubeDataCoordinator
87
from homeassistant.components.binary_sensor import BinarySensorEntity, BinarySensorDeviceClass
@@ -56,9 +55,10 @@ def device_info(self) -> DeviceInfo | None:
5655
return self._coordinator.device.device_info
5756

5857
@callback
59-
def update(self, new_state: bool) -> None:
60-
_LOGGER.debug("%s: Update device_locked %s",
58+
def update(self, new_state: bool | None) -> None:
59+
_LOGGER.debug("%s: Update device_locked %s -> %s",
6160
self._coordinator.data.device_id,
61+
self._attr_is_on,
6262
new_state
6363
)
6464
if new_state != self._attr_is_on:
@@ -89,7 +89,7 @@ def icon(self) -> str:
8989
return "mdi:water-check"
9090

9191
@callback
92-
def update(self, new_state: bool) -> None:
92+
def update(self, new_state: bool | None) -> None:
9393
_LOGGER.debug("%s: Update water_state %s -> %s",
9494
self._coordinator.data.device_id,
9595
self._attr_is_on,
@@ -125,7 +125,7 @@ def icon(self) -> str:
125125
return "mdi:water-off"
126126

127127
@callback
128-
def update(self, new_state: bool) -> None:
128+
def update(self, new_state: bool | None) -> None:
129129
_LOGGER.debug("%s: Update pump_state[%s] %s -> %s",
130130
self._coordinator.data.device_id,
131131
self._channel,
@@ -161,7 +161,7 @@ def icon(self) -> str:
161161
return "mdi:pump"
162162

163163
@callback
164-
def update(self, new_state: bool) -> None:
164+
def update(self, new_state: bool | None) -> None:
165165
_LOGGER.debug("%s: Update pump_lock_state[%s] %s -> %s",
166166
self._coordinator.data.device_id,
167167
self._channel,
@@ -197,7 +197,7 @@ def icon(self) -> str:
197197
return "mdi:water-pump"
198198

199199
@callback
200-
def update(self, new_state: bool) -> None:
200+
def update(self, new_state: bool | None) -> None:
201201
_LOGGER.debug("%s: Update pump_lock_state[%s] %s -> %s",
202202
self._coordinator.data.device_id,
203203
self._channel,
@@ -233,10 +233,11 @@ def icon(self) -> str:
233233
return "mdi:thermometer-probe"
234234

235235
@callback
236-
def update(self, new_state: bool) -> None:
237-
_LOGGER.debug("%s: Update sensor_state[%s] %s",
236+
def update(self, new_state: bool | None) -> None:
237+
_LOGGER.debug("%s: Update sensor_state[%s] %s -> %s",
238238
self._coordinator.data.device_id,
239239
self._channel,
240+
self._attr_is_on,
240241
new_state
241242
)
242243
if new_state != self._attr_is_on:
@@ -268,10 +269,11 @@ def icon(self) -> str:
268269
return "mdi:thermometer-probe"
269270

270271
@callback
271-
def update(self, new_state: bool) -> None:
272-
_LOGGER.debug("%s: Update sensor_state[%s] %s",
272+
def update(self, new_state: bool | None) -> None:
273+
_LOGGER.debug("%s: Update sensor_state[%s] %s -> %s",
273274
self._coordinator.data.device_id,
274275
self._channel,
276+
self._attr_is_on,
275277
new_state
276278
)
277279
if new_state != self._attr_is_on:

custom_components/growcube/coordinator.py

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ def __init__(self, host: str, hass: HomeAssistant):
5757
self._outlet_blocked_callbacks: dict[int, Callable[[bool], None]] = {}
5858
self._sensor_fault_callbacks: dict[int, Callable[[bool], None]] = {}
5959
self._sensor_disconnected_callbacks: dict[int, Callable[[bool], None]] = {}
60-
self._temperature_value_callback: Callable[[int], None] | None = None
61-
self._humidity_value_callback: Callable[[int], None] | None = None
62-
self._moisture_value_callbacks: dict[int, Callable[[bool], None]] = {}
60+
self._temperature_value_callback: Callable[[int | None], None] | None = None
61+
self._humidity_value_callback: Callable[[int | None], None] | None = None
62+
self._moisture_value_callbacks: dict[int, Callable[[bool | None], None]] = {}
6363

6464
def set_device_id(self, device_id: str) -> None:
6565
self.device_id = hex(int(device_id))[2:]
66-
self.data.device_id = "growcube_{}".format(self.device_id)
67-
self.data.device_info = {
66+
self.device.device_id = "growcube_{}".format(self.device_id)
67+
self.device.device_info = {
6868
"name": "GrowCube " + self.device_id,
69-
"identifiers": {(DOMAIN, self.data.device_id)},
69+
"identifiers": {(DOMAIN, self.device.device_id)},
7070
"manufacturer": "Elecrow",
7171
"model": "Growcube",
72-
"sw_version": self.data.version,
72+
"sw_version": self.device.version,
7373
}
7474

7575
async def connect(self) -> Tuple[bool, str]:
@@ -79,17 +79,17 @@ async def connect(self) -> Tuple[bool, str]:
7979

8080
self.shutting_down = False
8181
# Wait for the device to send back the DeviceVersionGrowcubeReport
82-
while not self.data.device_id:
82+
while not self.device.device_id:
8383
await asyncio.sleep(0.1)
8484
_LOGGER.debug(
8585
"Growcube device id: %s",
86-
self.data.device_id
86+
self.device_id
8787
)
8888

8989
time_command = SyncTimeCommand(datetime.now())
9090
_LOGGER.debug(
9191
"%s: Sending SyncTimeCommand",
92-
self.data.device_id
92+
self.device_id
9393
)
9494
self.client.send_command(time_command)
9595
return True, ""
@@ -106,15 +106,15 @@ async def reconnect(self) -> None:
106106
if result:
107107
_LOGGER.debug(
108108
"Reconnect to %s succeeded",
109-
self.data.host
109+
self.host
110110
)
111111
self.shutting_down = False
112112
await asyncio.sleep(10)
113113
return
114114

115115
_LOGGER.debug(
116116
"Reconnect failed for %s with error '%s', retrying in 10 seconds",
117-
self.data.host,
117+
self.host,
118118
error)
119119
await asyncio.sleep(10)
120120

@@ -155,9 +155,9 @@ def on_connected(self, host: str) -> None:
155155

156156
async def on_disconnected(self, host: str) -> None:
157157
_LOGGER.debug("Connection to %s lost", host)
158-
if self.data.device_id is not None:
158+
if self.device.device_id is not None:
159159
self.hass.states.async_set(
160-
DOMAIN + "." + self.data.device_id, STATE_UNAVAILABLE
160+
DOMAIN + "." + self.device.device_id, STATE_UNAVAILABLE
161161
)
162162
self.reset_sensor_data()
163163
if not self.shutting_down:
@@ -173,16 +173,62 @@ def disconnect(self) -> None:
173173
self.client.disconnect()
174174

175175
def reset_sensor_data(self) -> None:
176-
self.data.temperature = None
177-
self.data.humidity = None
178-
self.data.moisture = [None, None, None, None]
179-
self.data.pump_open = [False, False, False, False]
180-
self.data.device_locked = False
181-
self.data.water_warning = False
182-
self.data.sensor_abnormal = [False, False, False, False]
183-
self.data.sensor_disconnected = [False, False, False, False]
184-
self.data.outlet_blocked_state = [False, False, False, False]
185-
self.data.outlet_locked_state = [False, False, False, False]
176+
if self._device_locked_callback is not None:
177+
self._device_locked_callback(False)
178+
if self._water_warning_callback is not None:
179+
self._water_warning_callback(False)
180+
if self._pump_open_callbacks[0] is not None:
181+
self._pump_open_callbacks[0](False)
182+
if self._pump_open_callbacks[1] is not None:
183+
self._pump_open_callbacks[1](False)
184+
if self._pump_open_callbacks[2] is not None:
185+
self._pump_open_callbacks[2](False)
186+
if self._pump_open_callbacks[3] is not None:
187+
self._pump_open_callbacks[3](False)
188+
if self._outlet_locked_callbacks[0] is not None:
189+
self._outlet_locked_callbacks[0](False)
190+
if self._outlet_locked_callbacks[1] is not None:
191+
self._outlet_locked_callbacks[1](False)
192+
if self._outlet_locked_callbacks[2] is not None:
193+
self._outlet_locked_callbacks[2](False)
194+
if self._outlet_locked_callbacks[3] is not None:
195+
self._outlet_locked_callbacks[3](False)
196+
if self._outlet_blocked_callbacks[0] is not None:
197+
self._outlet_blocked_callbacks[0](False)
198+
if self._outlet_blocked_callbacks[1] is not None:
199+
self._outlet_blocked_callbacks[1](False)
200+
if self._outlet_blocked_callbacks[2] is not None:
201+
self._outlet_blocked_callbacks[2](False)
202+
if self._outlet_blocked_callbacks[3] is not None:
203+
self._outlet_blocked_callbacks[3](False)
204+
if self._sensor_fault_callbacks[0] is not None:
205+
self._sensor_fault_callbacks[0](False)
206+
if self._sensor_fault_callbacks[1] is not None:
207+
self._sensor_fault_callbacks[1](False)
208+
if self._sensor_fault_callbacks[2] is not None:
209+
self._sensor_fault_callbacks[2](False)
210+
if self._sensor_fault_callbacks[3] is not None:
211+
self._sensor_fault_callbacks[3](False)
212+
if self._sensor_disconnected_callbacks[0] is not None:
213+
self._sensor_disconnected_callbacks[0](False)
214+
if self._sensor_disconnected_callbacks[1] is not None:
215+
self._sensor_disconnected_callbacks[1](False)
216+
if self._sensor_disconnected_callbacks[2] is not None:
217+
self._sensor_disconnected_callbacks[2](False)
218+
if self._sensor_disconnected_callbacks[3] is not None:
219+
self._sensor_disconnected_callbacks[3](False)
220+
if self._temperature_value_callback is not None:
221+
self._temperature_value_callback(None)
222+
if self._humidity_value_callback is not None:
223+
self._humidity_value_callback(None)
224+
if self._moisture_value_callbacks[0] is not None:
225+
self._moisture_value_callbacks[0](None)
226+
if self._moisture_value_callbacks[1] is not None:
227+
self._moisture_value_callbacks[1](None)
228+
if self._moisture_value_callbacks[2] is not None:
229+
self._moisture_value_callbacks[2](None)
230+
if self._moisture_value_callbacks[3] is not None:
231+
self._moisture_value_callbacks[3](None)
186232

187233
def register_device_locked_state_callback(self, callback: Callable[[bool], None]) -> None:
188234
self._device_locked_callback = callback
@@ -254,13 +300,13 @@ def handle_report(self, report: GrowcubeReport) -> None:
254300
report.version
255301
)
256302
self.reset_sensor_data()
257-
self.data.version = report.version
303+
self.device.version = report.version
258304
self.set_device_id(report.device_id)
259305
# 20 - RepWaterState
260306
elif isinstance(report, WaterStateGrowcubeReport):
261307
_LOGGER.debug(
262308
"%s: Water state %s",
263-
self.data.device_id,
309+
self.device_id,
264310
report.water_warning
265311
)
266312
if self._water_warning_callback is not None:
@@ -269,7 +315,7 @@ def handle_report(self, report: GrowcubeReport) -> None:
269315
elif isinstance(report, MoistureHumidityStateGrowcubeReport):
270316
_LOGGER.debug(
271317
"%s: Sensor reading, channel %s, humidity %s, temperature %s, moisture %s",
272-
self.data.device_id,
318+
self.device_id,
273319
report.channel,
274320
report.humidity,
275321
report.temperature,
@@ -285,7 +331,7 @@ def handle_report(self, report: GrowcubeReport) -> None:
285331
elif isinstance(report, PumpOpenGrowcubeReport):
286332
_LOGGER.debug(
287333
"%s: Pump open, channel %s",
288-
self.data.device_id,
334+
self.device_id,
289335
report.channel
290336
)
291337
if report.channel.value in self._pump_open_callbacks:
@@ -294,7 +340,7 @@ def handle_report(self, report: GrowcubeReport) -> None:
294340
elif isinstance(report, PumpCloseGrowcubeReport):
295341
_LOGGER.debug(
296342
"%s: Pump closed, channel %s",
297-
self.data.device_id,
343+
self.device_id,
298344
report.channel
299345
)
300346
if report.channel.value in self._pump_open_callbacks:
@@ -303,7 +349,7 @@ def handle_report(self, report: GrowcubeReport) -> None:
303349
elif isinstance(report, CheckSensorGrowcubeReport):
304350
_LOGGER.debug(
305351
"%s: Sensor abnormal, channel %s",
306-
self.data.device_id,
352+
self.device_id,
307353
report.channel
308354
)
309355
if report.channel.value in self._sensor_fault_callbacks:
@@ -312,7 +358,7 @@ def handle_report(self, report: GrowcubeReport) -> None:
312358
elif isinstance(report, CheckOutletBlockedGrowcubeReport):
313359
_LOGGER.debug(
314360
"%s: Outlet blocked, channel %s",
315-
self.data.device_id,
361+
self.device_id,
316362
report.channel
317363
)
318364
if report.channel.value in self._outlet_blocked_callbacks:
@@ -321,7 +367,7 @@ def handle_report(self, report: GrowcubeReport) -> None:
321367
elif isinstance(report, CheckSensorNotConnectedGrowcubeReport):
322368
_LOGGER.debug(
323369
"%s: Check sensor, channel %s",
324-
self.data.device_id,
370+
self.device_id,
325371
report.channel
326372
)
327373
if report.channel.value in self._sensor_disconnected_callbacks:
@@ -330,12 +376,12 @@ def handle_report(self, report: GrowcubeReport) -> None:
330376
elif isinstance(report, LockStateGrowcubeReport):
331377
_LOGGER.debug(
332378
"%s: Lock state, %s",
333-
self.data.device_id,
379+
self.device_id,
334380
report.lock_state
335381
)
336382
# Handle case where the button on the device was pressed, this should do a reconnect
337383
# to read any problems still present
338-
if not report.lock_state and self.data.device_locked:
384+
if not report.lock_state:
339385
self.reset_sensor_data()
340386
self.reconnect()
341387
if self._device_locked_callback is not None:
@@ -344,7 +390,7 @@ def handle_report(self, report: GrowcubeReport) -> None:
344390
elif isinstance(report, CheckOutletLockedGrowcubeReport):
345391
_LOGGER.debug(
346392
"%s Check outlet, channel %s",
347-
self.data.device_id,
393+
self.device_id,
348394
report.channel
349395
)
350396
if report.channel.value in self._outlet_locked_callbacks:
@@ -356,7 +402,7 @@ async def water_plant(self, channel: int) -> None:
356402
async def handle_water_plant(self, channel: Channel, duration: int) -> None:
357403
_LOGGER.debug(
358404
"%s: Service water_plant called, %s, %s",
359-
self.data.device_id,
405+
self.device_id,
360406
channel,
361407
duration
362408
)
@@ -369,7 +415,7 @@ async def handle_set_smart_watering(self, channel: Channel,
369415

370416
_LOGGER.debug(
371417
"%s: Service set_smart_watering called, %s, %s, %s, %s",
372-
self.data.device_id,
418+
self.device_id,
373419
channel,
374420
all_day,
375421
min_moisture,
@@ -384,7 +430,7 @@ async def handle_set_manual_watering(self, channel: Channel, duration: int, inte
384430

385431
_LOGGER.debug(
386432
"%s: Service set_manual_watering called, %s, %s, %s",
387-
self.data.device_id,
433+
self.device_id,
388434
channel,
389435
duration,
390436
interval,
@@ -397,7 +443,7 @@ async def handle_delete_watering(self, channel: Channel) -> None:
397443

398444
_LOGGER.debug(
399445
"%s: Service delete_watering called, %s,",
400-
self.data.device_id,
446+
self.device_id,
401447
channel
402448
)
403449
command = PlantEndCommand(channel)

0 commit comments

Comments
 (0)