Skip to content

Commit eebd91c

Browse files
Tests
1 parent 775d0d0 commit eebd91c

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

custom_components/label_state/binary_sensor.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ class LabelStateBinarySensor(BinarySensorEntity):
110110
_attr_should_poll = False
111111

112112
_state_dict: dict[str, str] = {}
113-
_tracked_entities: list[str] = []
114113

115114
def __init__(
116115
self,
@@ -202,21 +201,19 @@ def _async_entity_registry_modified(
202201
entity_registry = er.async_get(self.hass)
203202
entity_entry = entity_registry.async_get(data["entity_id"])
204203
if entity_entry and self._label in entity_entry.labels:
205-
# The entity has a label, ensure we listen to it, if not already
206-
if entity_entry.entity_id not in self._tracked_entities:
207-
self.async_on_remove(
208-
async_track_state_change_event(
209-
self.hass,
210-
entity_entry.entity_id,
211-
self._async_state_listener,
212-
)
213-
)
214-
self._tracked_entities.append(entity_entry.entity_id)
215-
LOGGER.debug(
216-
"Found label %s in entity %s",
217-
self._label,
204+
# The entity has a label, ensure we listen to it
205+
self.async_on_remove(
206+
async_track_state_change_event(
207+
self.hass,
218208
entity_entry.entity_id,
209+
self._async_state_listener,
219210
)
211+
)
212+
LOGGER.debug(
213+
"Found label %s in entity %s",
214+
self._label,
215+
entity_entry.entity_id,
216+
)
220217

221218
self._calc_state()
222219
self.async_write_ha_state()

tests/test_binary_sensor.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,29 @@
3333
"state_2",
3434
"state_3",
3535
"expected_state",
36+
"later_expected_state",
3637
),
3738
[
3839
(
3940
"unavailable",
4041
"on",
4142
"on",
4243
"on",
44+
"on",
4345
),
4446
(
4547
"on",
4648
"on",
4749
"on",
4850
"off",
51+
"off",
52+
),
53+
(
54+
"off",
55+
"off",
56+
"unavailable",
57+
"off",
58+
"on",
4959
),
5060
],
5161
)
@@ -55,6 +65,7 @@ async def test_state_sensor(
5565
state_2: str,
5666
state_3: str,
5767
expected_state: str,
68+
later_expected_state: str,
5869
entity_registry: er.EntityRegistry,
5970
label_registry: lr.LabelRegistry,
6071
) -> None:
@@ -127,7 +138,7 @@ async def test_state_sensor(
127138
state = hass.states.get("binary_sensor.test_state")
128139

129140
assert state is not None
130-
assert state.state == expected_state
141+
assert state.state == later_expected_state
131142

132143

133144
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)