This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensor.py
393 lines (311 loc) · 12.6 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
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
"""Sensors for the MySkoda integration."""
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, UnitOfLength, UnitOfPower, UnitOfTime
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DATA_COODINATOR, DOMAIN
from .entity import MySkodaDataEntity
from .myskoda import Vehicle
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
coordinator = hass.data[DOMAIN][config.entry_id][DATA_COODINATOR]
vehicles = coordinator.data.get("vehicles")
entities = []
for vehicle in vehicles:
entities.append(SoftwareVersion(coordinator, vehicle))
entities.append(BatteryPercentage(coordinator, vehicle))
entities.append(ChargingPower(coordinator, vehicle))
entities.append(RemainingDistance(coordinator, vehicle))
entities.append(TargetBatteryPercentage(coordinator, vehicle))
entities.append(Mileage(coordinator, vehicle))
entities.append(ChargeType(coordinator, vehicle))
entities.append(ChargingState(coordinator, vehicle))
entities.append(RemainingChargingTime(coordinator, vehicle))
entities.append(LastUpdated(coordinator, vehicle))
async_add_entities(entities, update_before_add=True)
class MySkodaSensor(MySkodaDataEntity, SensorEntity):
"""Base class for all MySkoda sensors."""
def __init__( # noqa: D107
self,
coordinator: DataUpdateCoordinator,
vehicle: Vehicle,
entity_description: EntityDescription,
) -> None:
super().__init__(coordinator, vehicle, entity_description)
SensorEntity.__init__(self)
class SoftwareVersion(MySkodaSensor):
"""Current software version of a vehicle."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="software_version",
name=f"{vehicle.info.title} Software Version",
icon="mdi:update",
),
)
self._attr_unique_id = f"{vehicle.info.vin}_software_version"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.info.software_version
class BatteryPercentage(MySkodaSensor):
"""Battery charging state in percent."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="battery_percentage",
name=f"{vehicle.info.title} Battery Percentage",
icon="mdi:battery",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.BATTERY,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_battery_percentage"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.battery_percent
@property
def icon(self): # noqa: D102
if not self.coordinator.data:
return "mdi:battery-unknown"
self._update_device_from_coordinator()
suffix = ""
if self.vehicle.charging.battery_percent >= 95:
suffix = "100"
elif self.vehicle.charging.battery_percent >= 85:
suffix = "90"
elif self.vehicle.charging.battery_percent >= 75:
suffix = "80"
elif self.vehicle.charging.battery_percent >= 65:
suffix = "70"
elif self.vehicle.charging.battery_percent >= 55:
suffix = "60"
elif self.vehicle.charging.battery_percent >= 45:
suffix = "50"
elif self.vehicle.charging.battery_percent >= 35:
suffix = "40"
elif self.vehicle.charging.battery_percent >= 25:
suffix = "30"
elif self.vehicle.charging.battery_percent >= 15:
suffix = "20"
elif self.vehicle.charging.battery_percent >= 5:
suffix = "10"
else:
suffix = "outline"
if self.vehicle.charging.state != "CONNECT_CABLE":
return f"mdi:battery-charging-{suffix}"
if suffix == "100":
return "mdi:battery"
return f"mdi:battery-{suffix}"
class ChargingPower(MySkodaSensor):
"""How fast the car is charging in kW."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="charging_power",
name=f"{vehicle.info.title} Charging Power",
icon="mdi:lightning-bolt",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfPower.KILO_WATT,
device_class=SensorDeviceClass.POWER,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_charging_power"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.charging_power_kw
class RemainingDistance(MySkodaSensor):
"""Estimated range of an electric vehicle in km."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="range",
name=f"{vehicle.info.title} Range",
icon="mdi:speedometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfLength.KILOMETERS,
device_class=SensorDeviceClass.DISTANCE,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_range"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.remaining_distance_m / 1000
class TargetBatteryPercentage(MySkodaSensor):
"""Charging target of the EV's battery in percent."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="target_battery_percentage",
name=f"{vehicle.info.title} Target Battery Percentage",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
icon="mdi:percent",
device_class=SensorDeviceClass.BATTERY,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_target_battery_percentage"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.target_percent
class Mileage(MySkodaSensor):
"""The vehicle's mileage (total kilometers driven)."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="milage",
name=f"{vehicle.info.title} Milage",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfLength.KILOMETERS,
icon="mdi:counter",
device_class=SensorDeviceClass.DISTANCE,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_milage"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.health.mileage_km
class ChargeType(MySkodaSensor):
"""How the vehicle is being charged (AC/DC)."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="charge_type",
name=f"{vehicle.info.title} Charge Type",
),
)
self._attr_unique_id = f"{vehicle.info.vin}_charge_type"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.charge_type
@property
def icon(self): # noqa: D102
if not self.coordinator.data:
return "mdi:ev-plug-type2"
self._update_device_from_coordinator()
if self.vehicle.charging.charge_type == "AC":
return "mdi:ev-plug-type2"
return "mdi:ev-plug-ccs2"
class ChargingState(MySkodaSensor):
"""Current state of charging (ready, charging, conserving, ...)."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="charging_state",
name=f"{vehicle.info.title} Charging State",
device_class=SensorDeviceClass.ENUM,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_charging_state"
self._attr_options = [
"CONNECT_CABLE",
"READY_FOR_CHARGING",
"CONSERVING",
"CHARGING",
]
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.state
@property
def icon(self): # noqa: D102
if not self.coordinator.data:
return "mdi:power_plug"
self._update_device_from_coordinator()
if self.vehicle.charging.state == "CONNECT_CABLE":
return "mdi:power-plug-off"
return "mdi:power-plug"
class RemainingChargingTime(MySkodaSensor):
"""Estimation on when the vehicle will be fully charged."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="remaining_charging_time",
name=f"{vehicle.info.title} Remaining Charging Time",
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.MINUTES,
icon="mdi:timer",
),
)
self._attr_unique_id = f"{vehicle.info.vin}_remaining_charging_time"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.remaining_time_min
class LastUpdated(MySkodaSensor):
"""Timestamp of when the car has sent the last update to the MySkoda server."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SensorEntityDescription(
key="car_captured",
name=f"{vehicle.info.title} Last Updated",
device_class=SensorDeviceClass.TIMESTAMP,
icon="mdi:clock",
),
)
self._attr_unique_id = f"{vehicle.info.vin}_car_captured"
@property
def native_value(self): # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.status.car_captured