-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy pathswitch.py
574 lines (474 loc) · 19.3 KB
/
switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
"""Component providing support for RainMachine programs and zones."""
from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Callable, Coroutine
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Concatenate
from regenmaschine.errors import RainMachineError
import voluptuous as vol
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ID, EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import VolDictType
from . import RainMachineConfigEntry, RainMachineData, async_update_programs_and_zones
from .const import (
CONF_ALLOW_INACTIVE_ZONES_TO_RUN,
CONF_DEFAULT_ZONE_RUN_TIME,
CONF_DURATION,
CONF_USE_APP_RUN_TIMES,
DATA_PROGRAMS,
DATA_PROVISION_SETTINGS,
DATA_RESTRICTIONS_UNIVERSAL,
DATA_ZONES,
DEFAULT_ZONE_RUN,
)
from .entity import RainMachineEntity, RainMachineEntityDescription
from .util import RUN_STATE_MAP, key_exists
ATTR_ACTIVITY_TYPE = "activity_type"
ATTR_AREA = "area"
ATTR_CS_ON = "cs_on"
ATTR_CURRENT_CYCLE = "current_cycle"
ATTR_CYCLES = "cycles"
ATTR_DELAY = "delay"
ATTR_DELAY_ON = "delay_on"
ATTR_FIELD_CAPACITY = "field_capacity"
ATTR_NEXT_RUN = "next_run"
ATTR_NO_CYCLES = "number_of_cycles"
ATTR_PRECIP_RATE = "sprinkler_head_precipitation_rate"
ATTR_RESTRICTIONS = "restrictions"
ATTR_SLOPE = "slope"
ATTR_SOAK = "soak"
ATTR_SOIL_TYPE = "soil_type"
ATTR_SPRINKLER_TYPE = "sprinkler_head_type"
ATTR_STATUS = "status"
ATTR_SUN_EXPOSURE = "sun_exposure"
ATTR_VEGETATION_TYPE = "vegetation_type"
ATTR_ZONES = "zones"
ATTR_ZONE_RUN_TIME = "zone_run_time_from_app"
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
SOIL_TYPE_MAP = {
0: "Not Set",
1: "Clay Loam",
2: "Silty Clay",
3: "Clay",
4: "Loam",
5: "Sandy Loam",
6: "Loamy Sand",
7: "Sand",
8: "Sandy Clay",
9: "Silt Loam",
10: "Silt",
99: "Other",
}
SLOPE_TYPE_MAP = {
0: "Not Set",
1: "Flat",
2: "Moderate",
3: "High",
4: "Very High",
99: "Other",
}
SPRINKLER_TYPE_MAP = {
0: "Not Set",
1: "Popup Spray",
2: "Rotors Low Rate",
3: "Surface Drip",
4: "Bubblers Drip",
5: "Rotors High Rate",
99: "Other",
}
SUN_EXPOSURE_MAP = {0: "Not Set", 1: "Full Sun", 2: "Partial Shade", 3: "Full Shade"}
VEGETATION_MAP = {
0: "Not Set",
1: "Not Set",
2: "Cool Season Grass",
3: "Fruit Trees",
4: "Flowers",
5: "Vegetables",
6: "Citrus",
7: "Bushes",
9: "Drought Tolerant Plants",
10: "Warm Season Grass",
11: "Trees",
99: "Other",
}
def raise_on_request_error[_T: RainMachineBaseSwitch, **_P](
func: Callable[Concatenate[_T, _P], Awaitable[None]],
) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]:
"""Define a decorator to raise on a request error."""
async def decorator(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
"""Decorate."""
try:
await func(self, *args, **kwargs)
except RainMachineError as err:
raise HomeAssistantError(
f"Error while executing {func.__name__}: {err}",
) from err
return decorator
@dataclass(frozen=True, kw_only=True)
class RainMachineSwitchDescription(
SwitchEntityDescription, RainMachineEntityDescription
):
"""Describe a RainMachine switch."""
@dataclass(frozen=True, kw_only=True)
class RainMachineActivitySwitchDescription(RainMachineSwitchDescription):
"""Describe a RainMachine activity (program/zone) switch."""
kind: str
uid: int
@dataclass(frozen=True, kw_only=True)
class RainMachineRestrictionSwitchDescription(RainMachineSwitchDescription):
"""Describe a RainMachine restriction switch."""
data_key: str
TYPE_RESTRICTIONS_FREEZE_PROTECT_ENABLED = "freeze_protect_enabled"
TYPE_RESTRICTIONS_HOT_DAYS_EXTRA_WATERING = "hot_days_extra_watering"
RESTRICTIONS_SWITCH_DESCRIPTIONS = (
RainMachineRestrictionSwitchDescription(
key=TYPE_RESTRICTIONS_FREEZE_PROTECT_ENABLED,
translation_key=TYPE_RESTRICTIONS_FREEZE_PROTECT_ENABLED,
icon="mdi:snowflake-alert",
api_category=DATA_RESTRICTIONS_UNIVERSAL,
data_key="freezeProtectEnabled",
),
RainMachineRestrictionSwitchDescription(
key=TYPE_RESTRICTIONS_HOT_DAYS_EXTRA_WATERING,
translation_key=TYPE_RESTRICTIONS_HOT_DAYS_EXTRA_WATERING,
icon="mdi:heat-wave",
api_category=DATA_RESTRICTIONS_UNIVERSAL,
data_key="hotDaysExtraWatering",
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: RainMachineConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up RainMachine switches based on a config entry."""
platform = entity_platform.async_get_current_platform()
services: tuple[tuple[str, VolDictType | None, str], ...] = (
("start_program", None, "async_start_program"),
(
"start_zone",
{
vol.Optional(
CONF_DEFAULT_ZONE_RUN_TIME, default=DEFAULT_ZONE_RUN
): cv.positive_int
},
"async_start_zone",
),
("stop_program", None, "async_stop_program"),
("stop_zone", None, "async_stop_zone"),
)
for service_name, schema, method in services:
platform.async_register_entity_service(service_name, schema, method)
data = entry.runtime_data
entities: list[RainMachineBaseSwitch] = []
for kind, api_category, switch_class, switch_enabled_class in (
("program", DATA_PROGRAMS, RainMachineProgram, RainMachineProgramEnabled),
("zone", DATA_ZONES, RainMachineZone, RainMachineZoneEnabled),
):
coordinator = data.coordinators[api_category]
for uid, activity in coordinator.data.items():
name = activity["name"].capitalize()
# Add a switch to start/stop the program or zone:
entities.append(
switch_class(
entry,
data,
RainMachineActivitySwitchDescription(
key=f"{kind}_{uid}",
name=name,
api_category=api_category,
kind=kind,
uid=uid,
),
)
)
# Add a switch to enabled/disable the program or zone:
entities.append(
switch_enabled_class(
entry,
data,
RainMachineActivitySwitchDescription(
key=f"{kind}_{uid}_enabled",
name=f"{name} enabled",
api_category=api_category,
kind=kind,
uid=uid,
),
)
)
# Add switches to control restrictions:
for description in RESTRICTIONS_SWITCH_DESCRIPTIONS:
coordinator = data.coordinators[description.api_category]
if not key_exists(coordinator.data, description.data_key):
continue
entities.append(RainMachineRestrictionSwitch(entry, data, description))
async_add_entities(entities)
class RainMachineBaseSwitch(RainMachineEntity, SwitchEntity):
"""Define a base RainMachine switch."""
entity_description: RainMachineSwitchDescription
def __init__(
self,
entry: ConfigEntry,
data: RainMachineData,
description: RainMachineSwitchDescription,
) -> None:
"""Initialize."""
super().__init__(entry, data, description)
self._attr_is_on = False
self._entry = entry
@callback
def _update_activities(self) -> None:
"""Update all activity data."""
self.hass.async_create_task(
async_update_programs_and_zones(self.hass, self._entry)
)
async def async_start_program(self) -> None:
"""Execute the start_program entity service."""
raise NotImplementedError("Service not implemented for this entity")
async def async_start_zone(self, *, zone_run_time: int) -> None:
"""Execute the start_zone entity service."""
raise NotImplementedError("Service not implemented for this entity")
async def async_stop_program(self) -> None:
"""Execute the stop_program entity service."""
raise NotImplementedError("Service not implemented for this entity")
async def async_stop_zone(self) -> None:
"""Execute the stop_zone entity service."""
raise NotImplementedError("Service not implemented for this entity")
class RainMachineActivitySwitch(RainMachineBaseSwitch):
"""Define a RainMachine switch to start/stop an activity (program or zone)."""
_attr_icon = "mdi:water"
entity_description: RainMachineActivitySwitchDescription
def __init__(
self,
entry: ConfigEntry,
data: RainMachineData,
description: RainMachineSwitchDescription,
) -> None:
"""Initialize."""
super().__init__(entry, data, description)
self._attr_extra_state_attributes[ATTR_ACTIVITY_TYPE] = (
self.entity_description.kind
)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off.
The only way this could occur is if someone rapidly turns a disabled activity
off right after turning it on.
"""
if (
not self._entry.options[CONF_ALLOW_INACTIVE_ZONES_TO_RUN]
and not self.coordinator.data[self.entity_description.uid]["active"]
):
raise HomeAssistantError(
f"Cannot turn off an inactive program/zone: {self.name}"
)
await self.async_turn_off_when_active(**kwargs)
@raise_on_request_error
async def async_turn_off_when_active(self, **kwargs: Any) -> None:
"""Turn the switch off when its associated activity is active."""
raise NotImplementedError
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
if (
not self._entry.options[CONF_ALLOW_INACTIVE_ZONES_TO_RUN]
and not self.coordinator.data[self.entity_description.uid]["active"]
):
self._attr_is_on = False
self.async_write_ha_state()
raise HomeAssistantError(
f"Cannot turn on an inactive program/zone: {self.name}"
)
await self.async_turn_on_when_active(**kwargs)
@raise_on_request_error
async def async_turn_on_when_active(self, **kwargs: Any) -> None:
"""Turn the switch on when its associated activity is active."""
raise NotImplementedError
class RainMachineEnabledSwitch(RainMachineBaseSwitch):
"""Define a RainMachine switch to enable/disable an activity (program or zone)."""
_attr_entity_category = EntityCategory.CONFIG
_attr_icon = "mdi:cog"
entity_description: RainMachineActivitySwitchDescription
def __init__(
self,
entry: ConfigEntry,
data: RainMachineData,
description: RainMachineSwitchDescription,
) -> None:
"""Initialize."""
super().__init__(entry, data, description)
self._attr_extra_state_attributes[ATTR_ACTIVITY_TYPE] = (
self.entity_description.kind
)
@callback
def update_from_latest_data(self) -> None:
"""Update the entity when new data is received."""
self._attr_is_on = self.coordinator.data[self.entity_description.uid]["active"]
class RainMachineProgram(RainMachineActivitySwitch):
"""Define a RainMachine program."""
async def async_start_program(self) -> None:
"""Start the program."""
await self.async_turn_on()
async def async_stop_program(self) -> None:
"""Stop the program."""
await self.async_turn_off()
@raise_on_request_error
async def async_turn_off_when_active(self, **kwargs: Any) -> None:
"""Turn the switch off when its associated activity is active."""
await self._data.controller.programs.stop(self.entity_description.uid)
self._update_activities()
@raise_on_request_error
async def async_turn_on_when_active(self, **kwargs: Any) -> None:
"""Turn the switch on when its associated activity is active."""
await self._data.controller.programs.start(self.entity_description.uid)
self._update_activities()
@callback
def update_from_latest_data(self) -> None:
"""Update the entity when new data is received."""
data = self.coordinator.data[self.entity_description.uid]
self._attr_is_on = bool(data["status"])
next_run: str | None
if data.get("nextRun") is None:
next_run = None
else:
next_run = datetime.strptime(
f"{data['nextRun']} {data['startTime']}",
"%Y-%m-%d %H:%M",
).isoformat()
self._attr_extra_state_attributes.update(
{
ATTR_ID: self.entity_description.uid,
ATTR_NEXT_RUN: next_run,
ATTR_SOAK: data.get("soak"),
ATTR_STATUS: RUN_STATE_MAP[data["status"]],
ATTR_ZONES: [z for z in data["wateringTimes"] if z["active"]],
}
)
class RainMachineProgramEnabled(RainMachineEnabledSwitch):
"""Define a switch to enable/disable a RainMachine program."""
@raise_on_request_error
async def async_turn_off(self, **kwargs: Any) -> None:
"""Disable the program."""
tasks = [
self._data.controller.programs.stop(self.entity_description.uid),
self._data.controller.programs.disable(self.entity_description.uid),
]
await asyncio.gather(*tasks)
self._update_activities()
@raise_on_request_error
async def async_turn_on(self, **kwargs: Any) -> None:
"""Enable the program."""
await self._data.controller.programs.enable(self.entity_description.uid)
self._update_activities()
class RainMachineRestrictionSwitch(RainMachineBaseSwitch):
"""Define a RainMachine restriction setting."""
_attr_entity_category = EntityCategory.CONFIG
entity_description: RainMachineRestrictionSwitchDescription
@raise_on_request_error
async def async_turn_off(self, **kwargs: Any) -> None:
"""Disable the restriction."""
await self._data.controller.restrictions.set_universal(
{self.entity_description.data_key: False}
)
self._attr_is_on = False
self.async_write_ha_state()
@raise_on_request_error
async def async_turn_on(self, **kwargs: Any) -> None:
"""Enable the restriction."""
await self._data.controller.restrictions.set_universal(
{self.entity_description.data_key: True}
)
self._attr_is_on = True
self.async_write_ha_state()
@callback
def update_from_latest_data(self) -> None:
"""Update the entity when new data is received."""
self._attr_is_on = self.coordinator.data[self.entity_description.data_key]
class RainMachineZone(RainMachineActivitySwitch):
"""Define a RainMachine zone."""
async def async_start_zone(self, *, zone_run_time: int) -> None:
"""Start a particular zone for a certain amount of time."""
await self.async_turn_on(duration=zone_run_time)
async def async_stop_zone(self) -> None:
"""Stop a zone."""
await self.async_turn_off()
@raise_on_request_error
async def async_turn_off_when_active(self, **kwargs: Any) -> None:
"""Turn the switch off when its associated activity is active."""
await self._data.controller.zones.stop(self.entity_description.uid)
self._update_activities()
@raise_on_request_error
async def async_turn_on_when_active(self, **kwargs: Any) -> None:
"""Turn the switch on when its associated activity is active."""
# 1. Use duration parameter if provided from service call
duration = kwargs.get(CONF_DURATION)
if not duration:
if (
self._entry.options[CONF_USE_APP_RUN_TIMES]
and ATTR_ZONE_RUN_TIME in self._attr_extra_state_attributes
):
# 2. Use app's zone-specific default, if enabled and available
duration = self._attr_extra_state_attributes[ATTR_ZONE_RUN_TIME]
else:
# 3. Fall back to global zone default duration
duration = self._entry.options[CONF_DEFAULT_ZONE_RUN_TIME]
await self._data.controller.zones.start(
self.entity_description.uid,
duration,
)
self._update_activities()
@callback
def update_from_latest_data(self) -> None:
"""Update the entity when new data is received."""
data = self.coordinator.data[self.entity_description.uid]
self._attr_is_on = bool(data["state"])
attrs = {
ATTR_CURRENT_CYCLE: data["cycle"],
ATTR_ID: data["uid"],
ATTR_NO_CYCLES: data["noOfCycles"],
ATTR_RESTRICTIONS: data["restriction"],
ATTR_SLOPE: SLOPE_TYPE_MAP.get(data["slope"], 99),
ATTR_SOIL_TYPE: SOIL_TYPE_MAP.get(data["soil"], 99),
ATTR_SPRINKLER_TYPE: SPRINKLER_TYPE_MAP.get(data["group_id"], 99),
ATTR_STATUS: RUN_STATE_MAP[data["state"]],
ATTR_SUN_EXPOSURE: SUN_EXPOSURE_MAP.get(data.get("sun")),
ATTR_VEGETATION_TYPE: VEGETATION_MAP.get(data["type"], 99),
}
if "waterSense" in data:
if "area" in data["waterSense"]:
attrs[ATTR_AREA] = round(data["waterSense"]["area"], 2)
if "fieldCapacity" in data["waterSense"]:
attrs[ATTR_FIELD_CAPACITY] = round(
data["waterSense"]["fieldCapacity"], 2
)
if "precipitationRate" in data["waterSense"]:
attrs[ATTR_PRECIP_RATE] = round(
data["waterSense"]["precipitationRate"], 2
)
if self._entry.options[CONF_USE_APP_RUN_TIMES]:
provision_data = self._data.coordinators[DATA_PROVISION_SETTINGS].data
if zone_durations := provision_data.get("system", {}).get("zoneDuration"):
attrs[ATTR_ZONE_RUN_TIME] = zone_durations[
list(self.coordinator.data).index(self.entity_description.uid)
]
self._attr_extra_state_attributes.update(attrs)
class RainMachineZoneEnabled(RainMachineEnabledSwitch):
"""Define a switch to enable/disable a RainMachine zone."""
@raise_on_request_error
async def async_turn_off(self, **kwargs: Any) -> None:
"""Disable the zone."""
tasks = [
self._data.controller.zones.stop(self.entity_description.uid),
self._data.controller.zones.disable(self.entity_description.uid),
]
await asyncio.gather(*tasks)
self._update_activities()
@raise_on_request_error
async def async_turn_on(self, **kwargs: Any) -> None:
"""Enable the zone."""
await self._data.controller.zones.enable(self.entity_description.uid)
self._update_activities()