2626from homeassistant .const import Platform
2727from homeassistant .core import Event , EventStateChangedData , callback
2828from homeassistant .exceptions import ConfigEntryNotReady
29+ from homeassistant .helpers import issue_registry as ir
2930import homeassistant .helpers .config_validation as cv
3031from homeassistant .helpers .event import async_track_state_change_event
3132from homeassistant .loader import async_get_loaded_integration
4546 DEFAULT_UPDATE_MODE ,
4647 DOMAIN ,
4748 LOGGER ,
49+ REPAIR_ISSUE_TRIGGER_ENTITY_MISSING ,
4850 SENSOR_TYPE_DATA ,
4951 SENSOR_TYPE_VALUE ,
5052 UPDATE_MODE_POLLING ,
@@ -249,6 +251,30 @@ def async_service_removed_listener(event: Event) -> None:
249251 if update_mode == UPDATE_MODE_STATE_TRIGGER :
250252 trigger_entity = entry .data .get (CONF_TRIGGER_ENTITY , "" )
251253 if trigger_entity :
254+ # Check if trigger entity exists
255+ if not hass .states .get (trigger_entity ):
256+ # Create repair issue for missing trigger entity
257+ entry_name = entry .data .get ("name" , "Unknown" )
258+ ir .async_create_issue (
259+ hass ,
260+ DOMAIN ,
261+ f"{ REPAIR_ISSUE_TRIGGER_ENTITY_MISSING } _{ entry .entry_id } " ,
262+ is_fixable = False ,
263+ severity = ir .IssueSeverity .WARNING ,
264+ translation_key = "trigger_entity_missing" ,
265+ translation_placeholders = {
266+ "entry_name" : entry_name ,
267+ "trigger_entity" : trigger_entity ,
268+ },
269+ )
270+ LOGGER .warning (
271+ "Trigger entity %s not found for config entry %s (%s). "
272+ "The integration will not update automatically until the entity becomes available." ,
273+ trigger_entity ,
274+ entry_name ,
275+ entry .entry_id ,
276+ )
277+
252278 trigger_from_state = entry .data .get (CONF_TRIGGER_FROM_STATE , "" )
253279 trigger_to_state = entry .data .get (CONF_TRIGGER_TO_STATE , "" )
254280
@@ -259,8 +285,43 @@ def async_state_change_listener(event: Event[EventStateChangedData]) -> None:
259285 new_state = event .data .get ("new_state" )
260286
261287 if new_state is None :
288+ # Entity was removed - create repair issue if it doesn't exist
289+ entry_name = entry .data .get ("name" , "Unknown" )
290+ issue_id = f"{ REPAIR_ISSUE_TRIGGER_ENTITY_MISSING } _{ entry .entry_id } "
291+
292+ # Check if issue already exists
293+ existing_issue = ir .async_get (hass ).async_get_issue (DOMAIN , issue_id )
294+ if not existing_issue :
295+ ir .async_create_issue (
296+ hass ,
297+ DOMAIN ,
298+ issue_id ,
299+ is_fixable = False ,
300+ severity = ir .IssueSeverity .WARNING ,
301+ translation_key = "trigger_entity_missing" ,
302+ translation_placeholders = {
303+ "entry_name" : entry_name ,
304+ "trigger_entity" : trigger_entity ,
305+ },
306+ )
307+ LOGGER .warning (
308+ "Trigger entity %s was removed for config entry %s (%s)" ,
309+ trigger_entity ,
310+ entry_name ,
311+ entry .entry_id ,
312+ )
262313 return
263314
315+ # Entity exists - delete repair issue if it exists
316+ issue_id = f"{ REPAIR_ISSUE_TRIGGER_ENTITY_MISSING } _{ entry .entry_id } "
317+ existing_issue = ir .async_get (hass ).async_get_issue (DOMAIN , issue_id )
318+ if existing_issue :
319+ ir .async_delete_issue (hass , DOMAIN , issue_id )
320+ LOGGER .info (
321+ "Trigger entity %s is now available again, repair issue removed" ,
322+ trigger_entity ,
323+ )
324+
264325 old_state_value = old_state .state if old_state else None
265326 new_state_value = new_state .state
266327
@@ -318,7 +379,7 @@ async def async_unload_entry(
318379 Unload a config entry.
319380
320381 This is called when the integration is being removed or reloaded.
321- It ensures proper cleanup of all platform entities.
382+ It ensures proper cleanup of all platform entities and repair issues .
322383
323384 Args:
324385 hass: The Home Assistant instance.
@@ -327,6 +388,13 @@ async def async_unload_entry(
327388 Returns:
328389 True if unload was successful.
329390 """
391+ # Clean up any repair issues for this entry
392+ issue_id = f"{ REPAIR_ISSUE_TRIGGER_ENTITY_MISSING } _{ entry .entry_id } "
393+ existing_issue = ir .async_get (hass ).async_get_issue (DOMAIN , issue_id )
394+ if existing_issue :
395+ ir .async_delete_issue (hass , DOMAIN , issue_id )
396+ LOGGER .debug ("Removed repair issue %s during unload" , issue_id )
397+
330398 platforms = _get_platforms_for_entry (entry )
331399 return await hass .config_entries .async_unload_platforms (entry , platforms )
332400
0 commit comments