88from homeassistant .components .sensor import SensorEntity
99from homeassistant .config_entries import ConfigEntry
1010from homeassistant .helpers .entity_platform import AddEntitiesCallback
11- from homeassistant .helpers .update_coordinator import DataUpdateCoordinator , UpdateFailed
11+ from homeassistant .helpers .update_coordinator import DataUpdateCoordinator , CoordinatorEntity , UpdateFailed
12+ from homeassistant .helpers .entity import EntityCategory
1213
1314from .const import DOMAIN , DEFAULT_INTERVAL , DEFAULT_URL
1415
@@ -139,6 +140,8 @@ async def async_update_data():
139140 update_interval = timedelta (seconds = interval ),
140141 )
141142
143+ hass .data [DOMAIN ]["coordinator" ] = coordinator
144+
142145 await coordinator .async_config_entry_first_refresh ()
143146
144147 entities = []
@@ -147,52 +150,41 @@ async def async_update_data():
147150 _LOGGER .debug (f"[Enpal] Sensor hinzugefügt: { sensor ['name' ]} " )
148151 entities .append (EnpalSensor (uid , sensor , coordinator ))
149152
150- if entry .options .get ("use_wallbox_addon" , False ):
151- entities .extend ([
152- WallboxModeSensor ("Wallbox Lademodus" , "wallbox_mode" , "Wallbox Lademodus" , "http://127.0.0.1:8090/wallbox/status" , "mode" ),
153- WallboxStatusSensor ("Wallbox Status" , "wallbox_status" , "Wallbox Status" , "http://127.0.0.1:8090/wallbox/status" , "status" ),
154- ])
155-
156- async_add_entities (entities )
157-
158-
159- class WallboxSensorBase (SensorEntity ):
160- def __init__ (self , name , unique_id , friendly_name , status_url , key ):
161- self ._attr_name = friendly_name
162- self ._attr_unique_id = unique_id
163- self ._status_url = status_url
164- self ._key = key
165- self ._attr_icon = "mdi:ev-station"
166- self ._attr_device_class = None
167- self ._attr_native_unit_of_measurement = None
168- self ._attr_should_poll = True
169- self ._attr_extra_state_attributes = {}
170-
171- @property
172- def device_info (self ):
173- return {
174- "identifiers" : {(DOMAIN , "enpal_device" )},
175- "name" : "Enpal Webgerät" ,
176- "manufacturer" : "Enpal" ,
177- "model" : "Webparser" ,
178- }
179-
180- async def async_update (self ):
153+
154+ wallbox_url = "http://127.0.0.1:36725/wallbox/status"
155+
156+ async def async_wallbox_update ():
157+ wallbox_url = "http://127.0.0.1:36725/wallbox/status"
181158 try :
182159 async with aiohttp .ClientSession () as session :
183- async with session .get (self ._status_url , timeout = 5 ) as resp :
184- data = await resp .json ()
185- self ._attr_native_value = data .get (self ._key )
160+ async with session .get (wallbox_url , timeout = 5 ) as resp :
161+ if resp .status != 200 :
162+ raise UpdateFailed (f"Wallbox API Error: { resp .status } " )
163+ return await resp .json ()
186164 except Exception as e :
187- _LOGGER .warning (f"[WallboxSensor] Fehler beim Abrufen: { e } " )
188- self ._attr_native_value = None
165+ _LOGGER .error (f"[Enpal] Fehler beim Wallbox-Statusabruf: { e } " )
166+ raise UpdateFailed (f"Wallbox-Update fehlgeschlagen: { e } " )
167+
168+ wallbox_coordinator = DataUpdateCoordinator (
169+ hass ,
170+ logger = _LOGGER ,
171+ name = "Wallbox Status" ,
172+ update_method = async_wallbox_update ,
173+ update_interval = timedelta (seconds = 30 ),
174+ )
175+ try :
176+ await wallbox_coordinator .async_config_entry_first_refresh ()
177+ except Exception as e :
178+ raise ConfigEntryNotReady (f"Wallbox-Daten konnten nicht geladen werden: { e } " )
189179
180+ if entry .options .get ("use_wallbox_addon" , False ):
181+ entities .extend ([
182+ WallboxModeSensor (wallbox_coordinator ),
183+ WallboxStatusSensor (wallbox_coordinator ),
184+ ])
190185
191- class WallboxModeSensor (WallboxSensorBase ):
192- pass
186+ async_add_entities (entities )
193187
194- class WallboxStatusSensor (WallboxSensorBase ):
195- pass
196188
197189class EnpalSensor (SensorEntity ):
198190 def __init__ (self , uid : str , sensor : dict , coordinator : DataUpdateCoordinator ):
@@ -235,4 +227,35 @@ def _handle_coordinator_update(self):
235227 self ._attr_native_value = sensor ["value" ]
236228 self ._attr_extra_state_attributes ["enpal_last_update" ] = sensor .get ("enpal_last_update" )
237229 break
238- self .async_write_ha_state ()
230+ self .async_write_ha_state ()
231+
232+
233+ class WallboxCoordinatorEntity (CoordinatorEntity , SensorEntity ):
234+ def __init__ (self , coordinator , name , unique_id , key ):
235+ super ().__init__ (coordinator )
236+ self ._attr_name = name
237+ self ._attr_unique_id = unique_id
238+ self ._key = key
239+ self ._attr_icon = "mdi:ev-station"
240+ self ._attr_entity_category = EntityCategory .DIAGNOSTIC
241+
242+ @property
243+ def device_info (self ):
244+ return {
245+ "identifiers" : {(DOMAIN , "enpal_device" )},
246+ "name" : "Enpal Webgerät" ,
247+ "manufacturer" : "Enpal" ,
248+ "model" : "Webparser" ,
249+ }
250+
251+ @property
252+ def native_value (self ):
253+ return self .coordinator .data .get (self ._key )
254+
255+ class WallboxModeSensor (WallboxCoordinatorEntity ):
256+ def __init__ (self , coordinator ):
257+ super ().__init__ (coordinator , "Wallbox Lademodus" , "wallbox_mode" , "mode" )
258+
259+ class WallboxStatusSensor (WallboxCoordinatorEntity ):
260+ def __init__ (self , coordinator ):
261+ super ().__init__ (coordinator , "Wallbox Status" , "wallbox_status" , "status" )
0 commit comments