1
1
"""Sensor platform for battery_notes."""
2
2
from __future__ import annotations
3
3
4
+ from dataclasses import dataclass
4
5
import voluptuous as vol
5
6
6
7
from homeassistant .components .sensor import (
7
8
PLATFORM_SCHEMA ,
8
9
SensorEntity ,
10
+ SensorEntityDescription ,
11
+ RestoreSensor ,
9
12
)
10
13
from homeassistant .config_entries import ConfigEntry
11
14
from homeassistant .const import CONF_ENTITY_ID
23
26
)
24
27
25
28
from homeassistant .helpers .reload import async_setup_reload_service
26
- from homeassistant .helpers .typing import (
27
- ConfigType ,
28
- )
29
29
30
30
from homeassistant .const import (
31
31
CONF_NAME ,
32
- CONF_UNIQUE_ID ,
33
32
)
34
33
35
- from . import PLATFORMS
36
-
37
34
from .const import (
38
35
DOMAIN ,
36
+ PLATFORMS ,
39
37
CONF_BATTERY_TYPE ,
40
38
CONF_DEVICE_ID ,
41
39
)
42
40
43
- ICON = "mdi:battery-unknown"
41
+ from .entity import (
42
+ BatteryNotesEntityDescription ,
43
+ )
44
+
45
+
46
+ @dataclass
47
+ class BatteryNotesSensorEntityDescription (
48
+ BatteryNotesEntityDescription ,
49
+ SensorEntityDescription ,
50
+ ):
51
+ """Describes Battery Notes sensor entity."""
52
+ unique_id_suffix : str
53
+
54
+ typeSensorEntityDescription = BatteryNotesSensorEntityDescription (
55
+ unique_id_suffix = "" , # battery_type has uniqueId set to entityId in V1, never add a suffix
56
+ key = "battery_type" ,
57
+ translation_key = "battery_type" ,
58
+ icon = "mdi:battery-unknown" ,
59
+ entity_category = EntityCategory .DIAGNOSTIC ,
60
+ )
44
61
45
62
PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
46
63
{
@@ -110,76 +127,68 @@ async def async_registry_updated(event: Event) -> None:
110
127
111
128
device_id = async_add_to_device (hass , config_entry )
112
129
113
- async_add_entities (
114
- [
115
- BatteryTypeSensor (
130
+ entities = [
131
+ BatteryNotesTypeSensor (
116
132
hass ,
117
- config_entry .title ,
118
- config_entry .entry_id ,
119
- device_id = device_id ,
120
- battery_type = battery_type ,
121
- )
122
- ]
123
- )
133
+ typeSensorEntityDescription ,
134
+ device_id ,
135
+ f"{ config_entry .entry_id } { typeSensorEntityDescription .unique_id_suffix } " ,
136
+ battery_type
137
+ ),
138
+
139
+ ]
140
+
141
+ async_add_entities (entities )
142
+
124
143
125
144
async def async_setup_platform (
126
145
hass : HomeAssistant ,
127
- config : ConfigType ,
128
- async_add_entities : AddEntitiesCallback ,
129
146
) -> None :
130
- """Set up the battery type sensor."""
131
- name : str | None = config .get (CONF_NAME )
132
- unique_id = config .get (CONF_UNIQUE_ID )
133
- device_id : str = config [CONF_DEVICE_ID ]
134
- battery_type : str = config [CONF_BATTERY_TYPE ]
135
-
147
+ """Set up the battery note sensor."""
136
148
await async_setup_reload_service (hass , DOMAIN , PLATFORMS )
137
149
138
- async_add_entities (
139
- [BatteryTypeSensor (hass , name , unique_id , device_id , battery_type )]
140
- )
141
-
142
- class BatteryTypeSensor (SensorEntity ):
143
- """Represents a battery type sensor."""
150
+ class BatteryNotesSensor (RestoreSensor , SensorEntity ):
151
+ """Represents a battery note sensor."""
144
152
145
- _attr_icon = ICON
146
153
_attr_should_poll = False
154
+ entity_description : BatteryNotesSensorEntityDescription
147
155
148
156
def __init__ (
149
157
self ,
150
- hass : HomeAssistant ,
151
- name : str ,
152
- unique_id : str ,
158
+ hass ,
159
+ description : BatteryNotesSensorEntityDescription ,
153
160
device_id : str ,
154
- battery_type : str ,
161
+ unique_id : str ,
155
162
) -> None :
156
- """Create a battery type sensor."""
163
+ """Initialize the sensor."""
157
164
device_registry = dr .async_get (hass )
158
165
166
+ self .entity_description = description
167
+ self ._attr_has_entity_name = True
159
168
self ._attr_unique_id = unique_id
160
- self ._attr_name = name + " Battery type"
161
169
self ._device_id = device_id
162
170
163
- self ._device_id = device_id
164
171
if device_id and (device := device_registry .async_get (device_id )):
165
172
self ._attr_device_info = DeviceInfo (
166
173
connections = device .connections ,
167
174
identifiers = device .identifiers ,
168
175
)
169
- self ._attr_entity_category = EntityCategory .DIAGNOSTIC
170
- self ._battery_type = battery_type
171
-
172
176
173
177
async def async_added_to_hass (self ) -> None :
174
178
"""Handle added to Hass."""
179
+ await super ().async_added_to_hass ()
180
+ state = await self .async_get_last_sensor_data ()
181
+ if state :
182
+ self ._attr_native_value = state .native_value
183
+
175
184
self .async_on_remove (
176
185
async_track_state_change_event (
177
- self .hass , [self ._attr_unique_id ], self ._async_battery_type_state_changed_listener
186
+ self .hass , [self ._attr_unique_id ], self ._async_battery_note_state_changed_listener
178
187
)
179
188
)
180
189
181
190
# Call once on adding
182
- self ._async_battery_type_state_changed_listener ()
191
+ self ._async_battery_note_state_changed_listener ()
183
192
184
193
# Update entity options
185
194
registry = er .async_get (self .hass )
@@ -190,10 +199,36 @@ async def async_added_to_hass(self) -> None:
190
199
{"entity_id" : self ._attr_unique_id },
191
200
)
192
201
202
+ @callback
203
+ def _async_battery_note_state_changed_listener (self ) -> None :
204
+ """Handle the sensor state changes."""
205
+
206
+ self .async_write_ha_state ()
207
+ self .async_schedule_update_ha_state (True )
208
+
209
+
210
+ class BatteryNotesTypeSensor (BatteryNotesSensor ):
211
+ """Represents a battery note sensor."""
212
+
213
+ entity_description : BatteryNotesSensorEntityDescription
214
+
215
+ def __init__ (
216
+ self ,
217
+ hass ,
218
+ description : BatteryNotesSensorEntityDescription ,
219
+ device_id : str ,
220
+ unique_id : str ,
221
+ battery_type : str | None = None ,
222
+ ) -> None :
223
+ """Initialize the sensor."""
224
+ super ().__init__ (hass , description , device_id , unique_id )
225
+
226
+ self ._battery_type = battery_type
227
+
193
228
@property
194
229
def native_value (self ) -> str :
195
230
"""Return the native value of the sensor."""
196
- # return self.battery_type
231
+
197
232
return self ._battery_type
198
233
199
234
@callback
0 commit comments