11"""Sensor platform for battery_notes."""
22from __future__ import annotations
33
4+ from dataclasses import dataclass
45import voluptuous as vol
56
67from homeassistant .components .sensor import (
78 PLATFORM_SCHEMA ,
89 SensorEntity ,
10+ SensorEntityDescription ,
11+ RestoreSensor ,
912)
1013from homeassistant .config_entries import ConfigEntry
1114from homeassistant .const import CONF_ENTITY_ID
2326)
2427
2528from homeassistant .helpers .reload import async_setup_reload_service
26- from homeassistant .helpers .typing import (
27- ConfigType ,
28- )
2929
3030from homeassistant .const import (
3131 CONF_NAME ,
32- CONF_UNIQUE_ID ,
3332)
3433
35- from . import PLATFORMS
36-
3734from .const import (
3835 DOMAIN ,
36+ PLATFORMS ,
3937 CONF_BATTERY_TYPE ,
4038 CONF_DEVICE_ID ,
4139)
4240
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+ )
4461
4562PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
4663 {
@@ -110,76 +127,68 @@ async def async_registry_updated(event: Event) -> None:
110127
111128 device_id = async_add_to_device (hass , config_entry )
112129
113- async_add_entities (
114- [
115- BatteryTypeSensor (
130+ entities = [
131+ BatteryNotesTypeSensor (
116132 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+
124143
125144async def async_setup_platform (
126145 hass : HomeAssistant ,
127- config : ConfigType ,
128- async_add_entities : AddEntitiesCallback ,
129146) -> 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."""
136148 await async_setup_reload_service (hass , DOMAIN , PLATFORMS )
137149
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."""
144152
145- _attr_icon = ICON
146153 _attr_should_poll = False
154+ entity_description : BatteryNotesSensorEntityDescription
147155
148156 def __init__ (
149157 self ,
150- hass : HomeAssistant ,
151- name : str ,
152- unique_id : str ,
158+ hass ,
159+ description : BatteryNotesSensorEntityDescription ,
153160 device_id : str ,
154- battery_type : str ,
161+ unique_id : str ,
155162 ) -> None :
156- """Create a battery type sensor."""
163+ """Initialize the sensor."""
157164 device_registry = dr .async_get (hass )
158165
166+ self .entity_description = description
167+ self ._attr_has_entity_name = True
159168 self ._attr_unique_id = unique_id
160- self ._attr_name = name + " Battery type"
161169 self ._device_id = device_id
162170
163- self ._device_id = device_id
164171 if device_id and (device := device_registry .async_get (device_id )):
165172 self ._attr_device_info = DeviceInfo (
166173 connections = device .connections ,
167174 identifiers = device .identifiers ,
168175 )
169- self ._attr_entity_category = EntityCategory .DIAGNOSTIC
170- self ._battery_type = battery_type
171-
172176
173177 async def async_added_to_hass (self ) -> None :
174178 """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+
175184 self .async_on_remove (
176185 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
178187 )
179188 )
180189
181190 # Call once on adding
182- self ._async_battery_type_state_changed_listener ()
191+ self ._async_battery_note_state_changed_listener ()
183192
184193 # Update entity options
185194 registry = er .async_get (self .hass )
@@ -190,10 +199,36 @@ async def async_added_to_hass(self) -> None:
190199 {"entity_id" : self ._attr_unique_id },
191200 )
192201
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+
193228 @property
194229 def native_value (self ) -> str :
195230 """Return the native value of the sensor."""
196- # return self.battery_type
231+
197232 return self ._battery_type
198233
199234 @callback
0 commit comments