Skip to content

Commit aa8504e

Browse files
authored
Remove config entry from lock device when needed (#65)
1 parent fb4fec3 commit aa8504e

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

custom_components/lock_code_manager/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ async def async_update_listener(hass: HomeAssistant, config_entry: ConfigEntry)
334334
"%s (%s): Removing lock %s entities", entry_id, entry_title, lock_entity_id
335335
)
336336
async_dispatcher_send(hass, f"{DOMAIN}_{entry_id}_remove_lock", lock_entity_id)
337+
lock: BaseLock = hass.data[DOMAIN][CONF_LOCKS][lock_entity_id]
338+
if lock.device_entry:
339+
dev_reg = dr.async_get(hass)
340+
dev_reg.async_update_device(
341+
lock.device_entry.id, remove_config_entry_id=entry_id
342+
)
337343
await async_unload_lock(hass, config_entry, lock_entity_id)
338344

339345
# Notify any existing entities that additional locks have been added then create

custom_components/lock_code_manager/entity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ def __init__(
370370
connections=lock.device_entry.connections,
371371
identifiers=lock.device_entry.identifiers,
372372
)
373+
self._attr_name = f"Code slot {slot_num}"
373374

374375
self._attr_unique_id = (
375376
f"{self.base_unique_id}|{slot_num}|{self.key}|{lock.lock.entity_id}"

tests/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from homeassistant.components.lock import LockEntity
1111
from homeassistant.const import CONF_ENABLED, CONF_NAME, CONF_PIN
1212
from homeassistant.core import HomeAssistant, callback
13+
from homeassistant.helpers.entity import DeviceInfo
1314
from homeassistant.util import slugify
1415

1516
from custom_components.lock_code_manager.const import (
@@ -130,6 +131,10 @@ def __init__(self, name: str) -> None:
130131
self._attr_name = name
131132
self._attr_unique_id = slugify(name)
132133
self._attr_is_locked = False
134+
self._attr_has_entity_name = False
135+
self._attr_device_info = DeviceInfo(
136+
identifiers={(DOMAIN, f"lock.{slugify(name)}")}, name=name
137+
)
133138
super().__init__()
134139

135140

tests/test_init.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Platform,
2020
)
2121
from homeassistant.core import HomeAssistant
22-
from homeassistant.helpers import entity_registry as er
22+
from homeassistant.helpers import device_registry as dr, entity_registry as er
2323

2424
from custom_components.lock_code_manager.const import (
2525
ATTR_PIN_SYNCED_TO_LOCKS,
@@ -49,9 +49,16 @@ async def test_entry_setup_and_unload(
4949
lock_code_manager_config_entry,
5050
):
5151
"""Test entry setup and unload."""
52+
mock_lock_entry_id = mock_lock_config_entry.entry_id
5253
lcm_entry_id = lock_code_manager_config_entry.entry_id
54+
dev_reg = dr.async_get(hass)
5355
ent_reg = er.async_get(hass)
5456

57+
for entity_id in (LOCK_1_ENTITY_ID, LOCK_2_ENTITY_ID):
58+
device = dev_reg.async_get_device({(DOMAIN, entity_id)})
59+
assert device
60+
assert device.config_entries == {mock_lock_entry_id, lcm_entry_id}
61+
5562
unique_ids = set()
5663
for slot in range(1, 3):
5764
for entity_id in (LOCK_1_ENTITY_ID, LOCK_2_ENTITY_ID):
@@ -148,6 +155,12 @@ async def test_entry_setup_and_unload(
148155
)
149156
await hass.async_block_till_done()
150157

158+
# Validate that the config entry is removed from the device associated with the
159+
# lock that was removed from the config entry
160+
device = dev_reg.async_get_device({(DOMAIN, LOCK_2_ENTITY_ID)})
161+
assert device
162+
assert device.config_entries == {mock_lock_entry_id}
163+
151164
unique_ids = set()
152165
for slot in range(1, 3):
153166
unique_ids.add(f"{lcm_entry_id}|{slot}|{CONF_CODE}|{LOCK_1_ENTITY_ID}")

tests/test_sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ async def test_sensor_entity(
1414
):
1515
"""Test sensor entity."""
1616
for code_slot, pin in ((1, "1234"), (2, "5678")):
17-
state = hass.states.get(f"sensor.mock_title_code_slot_{code_slot}_code")
17+
state = hass.states.get(f"sensor.test_1_code_slot_{code_slot}")
1818
assert state
1919
assert state.state == pin
20-
state = hass.states.get(f"sensor.mock_title_code_slot_{code_slot}_code_2")
20+
state = hass.states.get(f"sensor.test_2_code_slot_{code_slot}")
2121
assert state
2222
assert state.state == pin

0 commit comments

Comments
 (0)