Skip to content

Commit

Permalink
Fix via_device
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Nov 15, 2024
1 parent 63b01ed commit 0a6178f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
69 changes: 51 additions & 18 deletions custom_components/keymaster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
CONF_LOCK_ENTITY_ID,
CONF_LOCK_NAME,
CONF_PARENT,
CONF_PARENT_ENTRY_ID,
CONF_SENSOR_NAME,
CONF_SLOTS,
CONF_START,
Expand Down Expand Up @@ -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],
Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions custom_components/keymaster/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions custom_components/keymaster/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 0a6178f

Please sign in to comment.