diff --git a/custom_components/keymaster/__init__.py b/custom_components/keymaster/__init__.py index 3520fbf9..86ddd24f 100644 --- a/custom_components/keymaster/__init__.py +++ b/custom_components/keymaster/__init__.py @@ -23,6 +23,7 @@ CONF_LOCK_ENTITY_ID, CONF_LOCK_NAME, CONF_PARENT, + CONF_PARENT_ENTRY_ID, CONF_SENSOR_NAME, CONF_SLOTS, CONF_START, @@ -82,22 +83,57 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b elif config_entry.data[CONF_PARENT] == "(none)": updated_config[CONF_PARENT] = None + if config_entry.data.get(CONF_PARENT_ENTRY_ID) == config_entry.entry_id: + updated_config[CONF_PARENT_ENTRY_ID] = None + + if updated_config.get(CONF_PARENT) is None: + updated_config[CONF_PARENT_ENTRY_ID] = None + elif updated_config.get(CONF_PARENT_ENTRY_ID) is None: + for entry in hass.config_entries.async_entries(DOMAIN): + if updated_config.get(CONF_PARENT) == entry.data.get(CONF_LOCK_NAME): + updated_config[CONF_PARENT_ENTRY_ID] = entry.entry_id + break + if updated_config != config_entry.data: hass.config_entries.async_update_entry(config_entry, data=updated_config) + _LOGGER.debug( + f"[init async_setup_entry] updated config_entry.data: {config_entry.data}" + ) + config_entry.add_update_listener(update_listener) await async_setup_services(hass) + if COORDINATOR not in hass.data[DOMAIN]: + coordinator = KeymasterCoordinator(hass) + hass.data[DOMAIN][COORDINATOR] = coordinator + else: + coordinator = hass.data[DOMAIN][COORDINATOR] + device_registry = dr.async_get(hass) + via_device: str | None = None + if config_entry.data.get(CONF_PARENT_ENTRY_ID): + via_device = (DOMAIN, config_entry.data.get(CONF_PARENT_ENTRY_ID)) + + _LOGGER.debug( + f"[init async_setup_entry] name: {config_entry.data.get(CONF_LOCK_NAME)}, " + f"parent_name: {config_entry.data.get(CONF_PARENT)}, " + f"parent_entry_id: {config_entry.data.get(CONF_PARENT_ENTRY_ID)}, " + f"via_device: {via_device}" + ) + device = device_registry.async_get_or_create( config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, config_entry.entry_id)}, - name=config_entry.data[CONF_LOCK_NAME], + name=config_entry.data.get(CONF_LOCK_NAME), configuration_url="https://github.com/FutureTense/keymaster", + via_device=via_device, ) + _LOGGER.debug(f"[init async_setup_entry] device: {device}") + code_slots: Mapping[int, KeymasterCodeSlot] = {} for x in range( config_entry.data[CONF_START], @@ -121,30 +157,25 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b code_slots[x] = KeymasterCodeSlot(number=x, accesslimit_day_of_week=dow_slots) kmlock = KeymasterLock( - lock_name=config_entry.data[CONF_LOCK_NAME], - lock_entity_id=config_entry.data[CONF_LOCK_ENTITY_ID], + lock_name=config_entry.data.get(CONF_LOCK_NAME), + lock_entity_id=config_entry.data.get(CONF_LOCK_ENTITY_ID), keymaster_device_id=device.id, keymaster_config_entry_id=config_entry.entry_id, - alarm_level_or_user_code_entity_id=config_entry.data[ + alarm_level_or_user_code_entity_id=config_entry.data.get( CONF_ALARM_LEVEL_OR_USER_CODE_ENTITY_ID - ], - alarm_type_or_access_control_entity_id=config_entry.data[ + ), + alarm_type_or_access_control_entity_id=config_entry.data.get( CONF_ALARM_TYPE_OR_ACCESS_CONTROL_ENTITY_ID - ], - door_sensor_entity_id=config_entry.data[CONF_SENSOR_NAME], - number_of_code_slots=config_entry.data[CONF_SLOTS], - starting_code_slot=config_entry.data[CONF_START], + ), + door_sensor_entity_id=config_entry.data.get(CONF_SENSOR_NAME), + number_of_code_slots=config_entry.data.get(CONF_SLOTS), + starting_code_slot=config_entry.data.get(CONF_START), code_slots=code_slots, - parent_name=config_entry.data[CONF_PARENT], + parent_name=config_entry.data.get(CONF_PARENT), + parent_config_entry_id=config_entry.data.get(CONF_PARENT_ENTRY_ID), ) hass.data[DOMAIN][config_entry.entry_id] = device.id - if COORDINATOR not in hass.data[DOMAIN]: - coordinator = KeymasterCoordinator(hass) - hass.data[DOMAIN][COORDINATOR] = coordinator - else: - coordinator = hass.data[DOMAIN][COORDINATOR] - await coordinator.add_lock(kmlock=kmlock) # await coordinator.async_config_entry_first_refresh() @@ -172,7 +203,7 @@ async def system_health_check(hass: HomeAssistant, config_entry: ConfigEntry) -> async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Handle removal of an entry.""" - lockname = config_entry.data[CONF_LOCK_NAME] + lockname = config_entry.data.get(CONF_LOCK_NAME) notification_id = f"{DOMAIN}_{lockname}_unload" async_create( hass, @@ -261,6 +292,8 @@ async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> Non old_slots = get_code_slots_list(config_entry.data) new_slots = get_code_slots_list(config_entry.options) + # TODO: Get this working and reduce duplicate code + new_data = config_entry.options.copy() new_data.pop(CONF_GENERATE, None) diff --git a/custom_components/keymaster/const.py b/custom_components/keymaster/const.py index 2f6fb989..a5c87ad9 100644 --- a/custom_components/keymaster/const.py +++ b/custom_components/keymaster/const.py @@ -47,6 +47,7 @@ CONF_LOCK_ENTITY_ID = "lock_entity_id" CONF_LOCK_NAME = "lockname" CONF_PARENT = "parent" +CONF_PARENT_ENTRY_ID = "parent_entry_id" CONF_PATH = "packages_path" CONF_SENSOR_NAME = "sensorname" CONF_SLOTS = "slots" diff --git a/custom_components/keymaster/entity.py b/custom_components/keymaster/entity.py index 63c5c853..a3092d21 100644 --- a/custom_components/keymaster/entity.py +++ b/custom_components/keymaster/entity.py @@ -52,9 +52,6 @@ def __init__(self, entity_description: EntityDescription) -> None: self._attr_extra_state_attributes: Mapping[str, Any] = {} self._attr_device_info: Mapping[str, Any] = { "identifiers": {(DOMAIN, self._config_entry.entry_id)}, - "via_device": self.coordinator.sync_get_device_id_from_config_entry_id( - self._kmlock.parent_config_entry_id - ), } _LOGGER.debug( f"[Entity init] Entity created: {self.name}, device_info: {self.device_info}"