1414from homeassistant .helpers import config_validation as cv
1515from homeassistant .helpers import entity_registry as er
1616from homeassistant .helpers .helper_integration import async_handle_source_entity_changes
17+ from homeassistant .helpers .issue_registry import (
18+ IssueSeverity ,
19+ async_create_issue ,
20+ async_delete_issue ,
21+ )
1722from homeassistant .helpers .typing import ConfigType
1823
1924from .const import (
2025 CONF_CALENDAR_ENTITY_ID ,
2126 DOMAIN ,
27+ ISSUE_MISSING_CALENDAR_ENTITY ,
2228 LOGGER ,
2329 MIN_HA_VERSION ,
2430 PLATFORMS ,
@@ -49,31 +55,69 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
4955 """Set up calendar_event from a config entry."""
5056
5157 entity_registry = er .async_get (hass )
52- try :
53- entity_id = er .async_validate_entity_id ( # noqa: F841
54- entity_registry , entry .options [CONF_CALENDAR_ENTITY_ID ]
58+
59+ # try:
60+ # entity_id = er.async_validate_entity_id( # noqa: F841
61+ # entity_registry, entry.options[CONF_CALENDAR_ENTITY_ID]
62+
63+ if (
64+ calendar_entity := entity_registry .entities .get_entry (
65+ entry .options [CONF_CALENDAR_ENTITY_ID ]
5566 )
56- except vol .Invalid :
57- # The entity is identified by an unknown entity registry ID
58- LOGGER .error (
59- "Failed to setup calender_event for unknown entity %s" ,
60- entry .options [CONF_CALENDAR_ENTITY_ID ],
67+ ) is None :
68+ async_create_issue (
69+ hass ,
70+ DOMAIN ,
71+ f"{ ISSUE_MISSING_CALENDAR_ENTITY } _{ entry .options [CONF_CALENDAR_ENTITY_ID ]} " ,
72+ is_fixable = True ,
73+ severity = IssueSeverity .ERROR ,
74+ translation_key = "missing_calendar_entity" ,
75+ translation_placeholders = {
76+ "calendar_entity" : entry .options [CONF_CALENDAR_ENTITY_ID ],
77+ "entry_title" : entry .title or "Calendar Event" ,
78+ },
79+ data = {"entry_id" : entry .entry_id },
6180 )
81+
6282 return False
6383
84+ # Clean up any existing repair issues since the entity is now valid
85+ async_delete_issue (
86+ hass , DOMAIN , f"{ ISSUE_MISSING_CALENDAR_ENTITY } _{ entry .entry_id } "
87+ )
88+
6489 def set_source_entity_id_or_uuid (source_entity_id : str ) -> None :
6590 hass .config_entries .async_update_entry (
6691 entry ,
6792 options = {** entry .options , CONF_CALENDAR_ENTITY_ID : source_entity_id },
6893 )
94+ # Clean up repair issue when entity is updated
95+ async_delete_issue (
96+ hass , DOMAIN , f"{ ISSUE_MISSING_CALENDAR_ENTITY } _{ entry .entry_id } "
97+ )
6998
7099 async def source_entity_removed () -> None :
71100 # The source entity has been removed.
72101 LOGGER .error (
73- "Failed to setup calender_event for unknown entity %s" ,
102+ "Failed to setup calendar_event for unknown entity %s" ,
74103 entry .options [CONF_CALENDAR_ENTITY_ID ],
75104 )
76105
106+ # Create a repair issue for the removed calendar entity
107+ async_create_issue (
108+ hass ,
109+ DOMAIN ,
110+ f"{ ISSUE_MISSING_CALENDAR_ENTITY } _{ entry .entry_id } " ,
111+ is_fixable = True ,
112+ severity = IssueSeverity .ERROR ,
113+ translation_key = "missing_calendar_entity" ,
114+ translation_placeholders = {
115+ "calendar_entity" : entry .options [CONF_CALENDAR_ENTITY_ID ],
116+ "entry_title" : entry .title or "Calendar Event" ,
117+ },
118+ data = {"entry_id" : entry .entry_id },
119+ )
120+
77121 entry .async_on_unload (
78122 async_handle_source_entity_changes (
79123 hass ,
@@ -99,4 +143,10 @@ async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry)
99143
100144async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
101145 """Unload a config entry."""
146+
147+ # Clean up any repair issues when unloading the entry
148+ async_delete_issue (
149+ hass , DOMAIN , f"{ ISSUE_MISSING_CALENDAR_ENTITY } _{ entry .entry_id } "
150+ )
151+
102152 return await hass .config_entries .async_unload_platforms (entry , PLATFORMS )
0 commit comments