-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
/
sensor.py
301 lines (252 loc) · 9.36 KB
/
sensor.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
"""The Washer/Dryer Sensor for Whirlpool Appliances."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, timedelta
import logging
from whirlpool.washerdryer import MachineState, WasherDryer
from homeassistant.components.sensor import (
RestoreSensor,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import utcnow
from . import WhirlpoolData
from .const import DOMAIN
TANK_FILL = {
"0": "unknown",
"1": "empty",
"2": "25",
"3": "50",
"4": "100",
"5": "active",
}
MACHINE_STATE = {
MachineState.Standby: "standby",
MachineState.Setting: "setting",
MachineState.DelayCountdownMode: "delay_countdown",
MachineState.DelayPause: "delay_paused",
MachineState.SmartDelay: "smart_delay",
MachineState.SmartGridPause: "smart_grid_pause",
MachineState.Pause: "pause",
MachineState.RunningMainCycle: "running_maincycle",
MachineState.RunningPostCycle: "running_postcycle",
MachineState.Exceptions: "exception",
MachineState.Complete: "complete",
MachineState.PowerFailure: "power_failure",
MachineState.ServiceDiagnostic: "service_diagnostic_mode",
MachineState.FactoryDiagnostic: "factory_diagnostic_mode",
MachineState.LifeTest: "life_test",
MachineState.CustomerFocusMode: "customer_focus_mode",
MachineState.DemoMode: "demo_mode",
MachineState.HardStopOrError: "hard_stop_or_error",
MachineState.SystemInit: "system_initialize",
}
CYCLE_FUNC = [
(WasherDryer.get_cycle_status_filling, "cycle_filling"),
(WasherDryer.get_cycle_status_rinsing, "cycle_rinsing"),
(WasherDryer.get_cycle_status_sensing, "cycle_sensing"),
(WasherDryer.get_cycle_status_soaking, "cycle_soaking"),
(WasherDryer.get_cycle_status_spinning, "cycle_spinning"),
(WasherDryer.get_cycle_status_washing, "cycle_washing"),
]
DOOR_OPEN = "door_open"
ICON_D = "mdi:tumble-dryer"
ICON_W = "mdi:washing-machine"
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(minutes=5)
def washer_state(washer: WasherDryer) -> str | None:
"""Determine correct states for a washer."""
if washer.get_attribute("Cavity_OpStatusDoorOpen") == "1":
return DOOR_OPEN
machine_state = washer.get_machine_state()
if machine_state == MachineState.RunningMainCycle:
for func, cycle_name in CYCLE_FUNC:
if func(washer):
return cycle_name
return MACHINE_STATE.get(machine_state)
@dataclass(frozen=True, kw_only=True)
class WhirlpoolSensorEntityDescription(SensorEntityDescription):
"""Describes Whirlpool Washer sensor entity."""
value_fn: Callable
SENSORS: tuple[WhirlpoolSensorEntityDescription, ...] = (
WhirlpoolSensorEntityDescription(
key="state",
translation_key="whirlpool_machine",
device_class=SensorDeviceClass.ENUM,
options=(
list(MACHINE_STATE.values())
+ [value for _, value in CYCLE_FUNC]
+ [DOOR_OPEN]
),
value_fn=washer_state,
),
WhirlpoolSensorEntityDescription(
key="DispenseLevel",
translation_key="whirlpool_tank",
entity_registry_enabled_default=False,
device_class=SensorDeviceClass.ENUM,
options=list(TANK_FILL.values()),
value_fn=lambda WasherDryer: TANK_FILL.get(
WasherDryer.get_attribute("WashCavity_OpStatusBulkDispense1Level")
),
),
)
SENSOR_TIMER: tuple[SensorEntityDescription] = (
SensorEntityDescription(
key="timeremaining",
translation_key="end_time",
device_class=SensorDeviceClass.TIMESTAMP,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Config flow entry for Whrilpool Laundry."""
entities: list = []
whirlpool_data: WhirlpoolData = hass.data[DOMAIN][config_entry.entry_id]
for appliance in whirlpool_data.appliances_manager.washer_dryers:
_wd = WasherDryer(
whirlpool_data.backend_selector,
whirlpool_data.auth,
appliance["SAID"],
async_get_clientsession(hass),
)
await _wd.connect()
entities.extend(
[
WasherDryerClass(
appliance["SAID"],
appliance["NAME"],
description,
_wd,
)
for description in SENSORS
]
)
entities.extend(
[
WasherDryerTimeClass(
appliance["SAID"],
appliance["NAME"],
description,
_wd,
)
for description in SENSOR_TIMER
]
)
async_add_entities(entities)
class WasherDryerClass(SensorEntity):
"""A class for the whirlpool/maytag washer account."""
_attr_should_poll = False
_attr_has_entity_name = True
def __init__(
self,
said: str,
name: str,
description: WhirlpoolSensorEntityDescription,
washdry: WasherDryer,
) -> None:
"""Initialize the washer sensor."""
self._wd: WasherDryer = washdry
if name == "dryer":
self._attr_icon = ICON_D
else:
self._attr_icon = ICON_W
self.entity_description: WhirlpoolSensorEntityDescription = description
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, said)},
name=name.capitalize(),
manufacturer="Whirlpool",
)
self._attr_unique_id = f"{said}-{description.key}"
async def async_added_to_hass(self) -> None:
"""Connect washer/dryer to the cloud."""
self._wd.register_attr_callback(self.async_write_ha_state)
async def async_will_remove_from_hass(self) -> None:
"""Close Whirlpool Appliance sockets before removing."""
self._wd.unregister_attr_callback(self.async_write_ha_state)
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._wd.get_online()
@property
def native_value(self) -> StateType | str:
"""Return native value of sensor."""
return self.entity_description.value_fn(self._wd)
class WasherDryerTimeClass(RestoreSensor):
"""A timestamp class for the whirlpool/maytag washer account."""
_attr_should_poll = True
_attr_has_entity_name = True
def __init__(
self,
said: str,
name: str,
description: SensorEntityDescription,
washdry: WasherDryer,
) -> None:
"""Initialize the washer sensor."""
self._wd: WasherDryer = washdry
if name == "dryer":
self._attr_icon = ICON_D
else:
self._attr_icon = ICON_W
self.entity_description: SensorEntityDescription = description
self._running: bool | None = None
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, said)},
name=name.capitalize(),
manufacturer="Whirlpool",
)
self._attr_unique_id = f"{said}-{description.key}"
async def async_added_to_hass(self) -> None:
"""Connect washer/dryer to the cloud."""
if restored_data := await self.async_get_last_sensor_data():
self._attr_native_value = restored_data.native_value
await super().async_added_to_hass()
self._wd.register_attr_callback(self.update_from_latest_data)
async def async_will_remove_from_hass(self) -> None:
"""Close Whrilpool Appliance sockets before removing."""
self._wd.unregister_attr_callback(self.update_from_latest_data)
await self._wd.disconnect()
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._wd.get_online()
async def async_update(self) -> None:
"""Update status of Whirlpool."""
await self._wd.fetch_data()
@callback
def update_from_latest_data(self) -> None:
"""Calculate the time stamp for completion."""
machine_state = self._wd.get_machine_state()
now = utcnow()
if (
machine_state.value
in {MachineState.Complete.value, MachineState.Standby.value}
and self._running
):
self._running = False
self._attr_native_value = now
self._async_write_ha_state()
if machine_state is MachineState.RunningMainCycle:
self._running = True
new_timestamp = now + timedelta(
seconds=int(self._wd.get_attribute("Cavity_TimeStatusEstTimeRemaining"))
)
if (
self._attr_native_value is None
or isinstance(self._attr_native_value, datetime)
and abs(new_timestamp - self._attr_native_value) > timedelta(seconds=60)
):
self._attr_native_value = new_timestamp
self._async_write_ha_state()