Skip to content

Commit 4113bc7

Browse files
authored
Refresh coordinators when setting or clearing usercode (#68)
1 parent de4f8d5 commit 4113bc7

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

custom_components/lock_code_manager/binary_sensor.py

+39-32
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,12 @@ async def async_setup_entry(
4949
async_add_entities: AddEntitiesCallback,
5050
) -> bool:
5151
"""Set up config entry."""
52-
coordinators: list[LockUsercodeUpdateCoordinator] = list(
53-
hass.data[DOMAIN][config_entry.entry_id][COORDINATORS].values()
54-
)
5552

5653
@callback
5754
def add_pin_enabled_entity(slot_num: int, ent_reg: er.EntityRegistry) -> None:
5855
"""Add PIN enabled binary sensor entities for slot."""
5956
async_add_entities(
60-
[
61-
LockCodeManagerPINSyncedEntity(
62-
hass, ent_reg, config_entry, coordinators, slot_num
63-
)
64-
],
57+
[LockCodeManagerPINSyncedEntity(hass, ent_reg, config_entry, slot_num)],
6558
True,
6659
)
6760

@@ -84,14 +77,12 @@ def __init__(
8477
hass: HomeAssistant,
8578
ent_reg: er.EntityRegistry,
8679
config_entry: ConfigEntry,
87-
coordinators: list[LockUsercodeUpdateCoordinator],
8880
slot_num: int,
8981
) -> None:
9082
"""Initialize entity."""
9183
BaseLockCodeManagerEntity.__init__(
9284
self, hass, ent_reg, config_entry, slot_num, ATTR_PIN_SYNCED_TO_LOCKS
9385
)
94-
self.coordinators = coordinators
9586
self._entity_id_map: dict[str, str] = {}
9687
self._issue_reg: ir.IssueRegistry | None = None
9788
self._call_later_unsub: Callable | None = None
@@ -114,18 +105,7 @@ async def async_update_usercodes(
114105
"""Update usercodes on locks based on state change."""
115106
if not states:
116107
states = {}
117-
disabling_entity_ids = (
118-
state["entity_id"]
119-
for key, state in states.items()
120-
if (key != CONF_NUMBER_OF_USES and state["state"] != STATE_ON)
121-
or (
122-
key == CONF_NUMBER_OF_USES
123-
and (
124-
state["state"] in (STATE_UNAVAILABLE, STATE_UNKNOWN)
125-
or int(float(state["state"])) == 0
126-
)
127-
)
128-
)
108+
coordinators: list[LockUsercodeUpdateCoordinator] = []
129109
for lock in self.locks:
130110
lock_slot_sensor_state = self._lock_slot_sensor_state(lock)
131111
if self.is_on:
@@ -145,27 +125,52 @@ async def async_update_usercodes(
145125
if lock_slot_sensor_state == pin_state.state:
146126
continue
147127

128+
await lock.async_set_usercode(
129+
int(self.slot_num), pin_state.state, name_state.state
130+
)
131+
148132
_LOGGER.info(
149-
"%s (%s): Setting usercode for %s slot %s",
133+
"%s (%s): Set usercode for %s slot %s",
150134
self.config_entry.entry_id,
151135
self.config_entry.title,
152136
lock.lock.entity_id,
153137
self.slot_num,
154138
)
155-
156-
await lock.async_set_usercode(
157-
int(self.slot_num), pin_state.state, name_state.state
158-
)
159139
else:
160140
if lock_slot_sensor_state in (
161141
"",
162142
STATE_UNKNOWN,
163143
):
164144
continue
165145

146+
if not (
147+
disabling_entity_ids := list(
148+
state["entity_id"]
149+
for key, state in states.items()
150+
if (
151+
key not in (CONF_NUMBER_OF_USES, CONF_PIN)
152+
and state["state"] != STATE_ON
153+
)
154+
or (
155+
key == CONF_NUMBER_OF_USES
156+
and (
157+
state["state"] in (STATE_UNAVAILABLE, STATE_UNKNOWN)
158+
or int(float(state["state"])) == 0
159+
)
160+
)
161+
or (
162+
key == CONF_PIN
163+
and state["state"] != self._lock_slot_sensor_state(lock)
164+
)
165+
)
166+
):
167+
return
168+
169+
await lock.async_clear_usercode(int(self.slot_num))
170+
166171
_LOGGER.info(
167172
(
168-
"%s (%s): Clearing usercode for lock %s slot %s because the "
173+
"%s (%s): Cleared usercode for lock %s slot %s because the "
169174
"following entities indicate the slot is disabled: %s"
170175
),
171176
self.config_entry.entry_id,
@@ -175,12 +180,14 @@ async def async_update_usercodes(
175180
", ".join(disabling_entity_ids),
176181
)
177182

178-
await lock.async_clear_usercode(int(self.slot_num))
179-
180-
await asyncio.gather(
181-
*[coordinator.async_refresh() for coordinator in self.coordinators]
183+
coordinators.append(
184+
self.hass.data[DOMAIN][COORDINATORS][lock.lock.entity_id]
182185
)
183186

187+
await asyncio.gather(
188+
*[coordinator.async_refresh() for coordinator in coordinators]
189+
)
190+
184191
async def _update_state(self, _: datetime | None = None) -> None:
185192
"""Update binary sensor state by getting dependent states."""
186193
if self._call_later_unsub:

tests/test_binary_sensor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ async def test_binary_sensor_entity(
136136
)
137137
await hass.async_block_till_done()
138138

139-
assert hass.data[LOCK_DATA][LOCK_1_ENTITY_ID]["service_calls"]["set_usercode"] == [
140-
(2, "0987", "test2")
141-
]
139+
assert hass.data[LOCK_DATA][LOCK_1_ENTITY_ID]["service_calls"]["set_usercode"][
140+
-1
141+
] == (2, "0987", "test2")
142142

143143
new_config = copy.deepcopy(BASE_CONFIG)
144144
new_config[CONF_SLOTS][2][CONF_CALENDAR] = "calendar.test_2"

tests/test_sensor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44

5+
from homeassistant.const import STATE_UNAVAILABLE
56
from homeassistant.core import HomeAssistant
67

78
_LOGGER = logging.getLogger(__name__)
@@ -13,7 +14,7 @@ async def test_sensor_entity(
1314
lock_code_manager_config_entry,
1415
):
1516
"""Test sensor entity."""
16-
for code_slot, pin in ((1, "1234"), (2, "5678")):
17+
for code_slot, pin in ((1, "1234"), (2, STATE_UNAVAILABLE)):
1718
state = hass.states.get(f"sensor.test_1_code_slot_{code_slot}")
1819
assert state
1920
assert state.state == pin

0 commit comments

Comments
 (0)