10
10
from homeassistant .helpers .entity import generate_entity_id
11
11
from homeassistant .util .dt import (utcnow )
12
12
from .const import DOMAIN , DATA_COORDINATORS , DATA_SLOTS , COORDINATOR_CHARGESESSIONS , COORDINATOR_ADVANCED , DATA_CLIENT
13
- from .coordinator import OhmeChargeSessionsCoordinator , OhmeAdvancedSettingsCoordinator
14
13
from .utils import in_slot
14
+ from .base import OhmeEntity
15
15
16
16
_LOGGER = logging .getLogger (__name__ )
17
17
@@ -36,40 +36,14 @@ async def async_setup_entry(
36
36
37
37
38
38
class ConnectedBinarySensor (
39
- CoordinatorEntity [ OhmeChargeSessionsCoordinator ] ,
39
+ OhmeEntity ,
40
40
BinarySensorEntity ):
41
41
"""Binary sensor for if car is plugged in."""
42
42
43
- _attr_name = "Car Connected"
43
+ _attr_translation_key = "car_connected"
44
+ _attr_icon = "mdi:ev-plug-type2"
44
45
_attr_device_class = BinarySensorDeviceClass .PLUG
45
46
46
- def __init__ (
47
- self ,
48
- coordinator : OhmeChargeSessionsCoordinator ,
49
- hass : HomeAssistant ,
50
- client ):
51
- super ().__init__ (coordinator = coordinator )
52
-
53
- self ._attributes = {}
54
- self ._last_updated = None
55
- self ._state = False
56
- self ._client = client
57
-
58
- self .entity_id = generate_entity_id (
59
- "binary_sensor.{}" , "ohme_car_connected" , hass = hass )
60
-
61
- self ._attr_device_info = client .get_device_info ()
62
-
63
- @property
64
- def icon (self ):
65
- """Icon of the sensor."""
66
- return "mdi:ev-plug-type2"
67
-
68
- @property
69
- def unique_id (self ) -> str :
70
- """Return the unique ID of the sensor."""
71
- return self ._client .get_unique_id ("car_connected" )
72
-
73
47
@property
74
48
def is_on (self ) -> bool :
75
49
if self .coordinator .data is None :
@@ -81,24 +55,20 @@ def is_on(self) -> bool:
81
55
82
56
83
57
class ChargingBinarySensor (
84
- CoordinatorEntity [ OhmeChargeSessionsCoordinator ] ,
58
+ OhmeEntity ,
85
59
BinarySensorEntity ):
86
60
"""Binary sensor for if car is charging."""
87
61
88
- _attr_name = "Car Charging"
62
+ _attr_translation_key = "car_charging"
63
+ _attr_icon = "mdi:battery-charging-100"
89
64
_attr_device_class = BinarySensorDeviceClass .BATTERY_CHARGING
90
65
91
66
def __init__ (
92
67
self ,
93
68
coordinator : OhmeChargeSessionsCoordinator ,
94
69
hass : HomeAssistant ,
95
70
client ):
96
- super ().__init__ (coordinator = coordinator )
97
-
98
- self ._attributes = {}
99
- self ._last_updated = None
100
- self ._state = False
101
- self ._client = client
71
+ super ().__init__ (coordinator , hass , client )
102
72
103
73
# Cache the last power readings
104
74
self ._last_reading = None
@@ -107,21 +77,6 @@ def __init__(
107
77
# State variables for charge state detection
108
78
self ._trigger_count = 0
109
79
110
- self .entity_id = generate_entity_id (
111
- "binary_sensor.{}" , "ohme_car_charging" , hass = hass )
112
-
113
- self ._attr_device_info = client .get_device_info ()
114
-
115
- @property
116
- def icon (self ):
117
- """Icon of the sensor."""
118
- return "mdi:battery-charging-100"
119
-
120
- @property
121
- def unique_id (self ) -> str :
122
- """Return the unique ID of the sensor."""
123
- return self ._client .get_unique_id ("ohme_car_charging" )
124
-
125
80
@property
126
81
def is_on (self ) -> bool :
127
82
return self ._state
@@ -208,38 +163,12 @@ def _handle_coordinator_update(self) -> None:
208
163
209
164
210
165
class PendingApprovalBinarySensor (
211
- CoordinatorEntity [ OhmeChargeSessionsCoordinator ] ,
166
+ OhmeEntity ,
212
167
BinarySensorEntity ):
213
168
"""Binary sensor for if a charge is pending approval."""
214
169
215
- _attr_name = "Pending Approval"
216
-
217
- def __init__ (
218
- self ,
219
- coordinator : OhmeChargeSessionsCoordinator ,
220
- hass : HomeAssistant ,
221
- client ):
222
- super ().__init__ (coordinator = coordinator )
223
-
224
- self ._attributes = {}
225
- self ._last_updated = None
226
- self ._state = False
227
- self ._client = client
228
-
229
- self .entity_id = generate_entity_id (
230
- "binary_sensor.{}" , "ohme_pending_approval" , hass = hass )
231
-
232
- self ._attr_device_info = client .get_device_info ()
233
-
234
- @property
235
- def icon (self ):
236
- """Icon of the sensor."""
237
- return "mdi:alert-decagram"
238
-
239
- @property
240
- def unique_id (self ) -> str :
241
- """Return the unique ID of the sensor."""
242
- return self ._client .get_unique_id ("pending_approval" )
170
+ _attr_translation_key = "pending_approval"
171
+ _attr_icon = "mdi:alert-decagram"
243
172
244
173
@property
245
174
def is_on (self ) -> bool :
@@ -253,38 +182,12 @@ def is_on(self) -> bool:
253
182
254
183
255
184
class CurrentSlotBinarySensor (
256
- CoordinatorEntity [ OhmeChargeSessionsCoordinator ] ,
185
+ OhmeEntity ,
257
186
BinarySensorEntity ):
258
187
"""Binary sensor for if we are currently in a smart charge slot."""
259
188
260
- _attr_name = "Charge Slot Active"
261
-
262
- def __init__ (
263
- self ,
264
- coordinator : OhmeChargeSessionsCoordinator ,
265
- hass : HomeAssistant ,
266
- client ):
267
- super ().__init__ (coordinator = coordinator )
268
-
269
- self ._last_updated = None
270
- self ._state = False
271
- self ._client = client
272
- self ._hass = hass
273
-
274
- self .entity_id = generate_entity_id (
275
- "binary_sensor.{}" , "ohme_slot_active" , hass = hass )
276
-
277
- self ._attr_device_info = client .get_device_info ()
278
-
279
- @property
280
- def icon (self ):
281
- """Icon of the sensor."""
282
- return "mdi:calendar-check"
283
-
284
- @property
285
- def unique_id (self ) -> str :
286
- """Return the unique ID of the sensor."""
287
- return self ._client .get_unique_id ("ohme_slot_active" )
189
+ _attr_translation_key = "slot_active"
190
+ _attr_icon = "mdi:calendar-check"
288
191
289
192
@property
290
193
def extra_state_attributes (self ):
@@ -316,40 +219,14 @@ def _handle_coordinator_update(self) -> None:
316
219
self .async_write_ha_state ()
317
220
318
221
class ChargerOnlineBinarySensor (
319
- CoordinatorEntity [ OhmeAdvancedSettingsCoordinator ] ,
222
+ OhmeEntity ,
320
223
BinarySensorEntity ):
321
224
"""Binary sensor for if charger is online."""
322
225
323
- _attr_name = "Charger Online"
226
+ _attr_translation_key = "charger_online"
227
+ _attr_icon = "mdi:web"
324
228
_attr_device_class = BinarySensorDeviceClass .CONNECTIVITY
325
229
326
- def __init__ (
327
- self ,
328
- coordinator : OhmeAdvancedSettingsCoordinator ,
329
- hass : HomeAssistant ,
330
- client ):
331
- super ().__init__ (coordinator = coordinator )
332
-
333
- self ._attributes = {}
334
- self ._last_updated = None
335
- self ._state = None
336
- self ._client = client
337
-
338
- self .entity_id = generate_entity_id (
339
- "binary_sensor.{}" , "ohme_charger_online" , hass = hass )
340
-
341
- self ._attr_device_info = client .get_device_info ()
342
-
343
- @property
344
- def icon (self ):
345
- """Icon of the sensor."""
346
- return "mdi:web"
347
-
348
- @property
349
- def unique_id (self ) -> str :
350
- """Return the unique ID of the sensor."""
351
- return self ._client .get_unique_id ("charger_online" )
352
-
353
230
@property
354
231
def is_on (self ) -> bool :
355
232
if self .coordinator .data and self .coordinator .data ["online" ]:
0 commit comments