Skip to content

Commit 40eec07

Browse files
Avoid circular listeners
1 parent 3be2e92 commit 40eec07

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

custom_components/label_state/binary_sensor.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ async def async_added_to_hass(self) -> None:
150150
entries = er.async_entries_for_label(ent_reg, self._label)
151151

152152
for entity_entry in entries:
153+
if entity_entry.entity_id == self.entity_id:
154+
LOGGER.debug(
155+
"We don't watch ourself %s",
156+
entity_entry.entity_id,
157+
)
158+
continue
153159
for label in entity_entry.labels:
154160
if label == self._label:
155161
LOGGER.debug(
@@ -200,20 +206,27 @@ def _async_entity_registry_modified(
200206
# Get the entity, check it's current labels
201207
entity_registry = er.async_get(self.hass)
202208
entity_entry = entity_registry.async_get(data["entity_id"])
209+
203210
if entity_entry and self._label in entity_entry.labels:
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,
211+
if entity_entry.entity_id == self.entity_id:
212+
LOGGER.debug(
213+
"We don't watch ourself %s",
214+
entity_entry.entity_id,
215+
)
216+
else:
217+
# The entity has a label, ensure we listen to it
218+
self.async_on_remove(
219+
async_track_state_change_event(
220+
self.hass,
221+
entity_entry.entity_id,
222+
self._async_state_listener,
223+
)
224+
)
225+
LOGGER.debug(
226+
"Found label %s in entity %s",
227+
self._label,
208228
entity_entry.entity_id,
209-
self._async_state_listener,
210229
)
211-
)
212-
LOGGER.debug(
213-
"Found label %s in entity %s",
214-
self._label,
215-
entity_entry.entity_id,
216-
)
217230

218231
self._calc_state()
219232
self.async_write_ha_state()

0 commit comments

Comments
 (0)