Skip to content

Commit

Permalink
Add guard to global updater
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulcahey committed Aug 22, 2024
1 parent 2f06f00 commit ec2466d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,52 @@ async def test_pollers_skip(
assert "Device availability checker interval skipped" in caplog.text


async def test_global_updater_guards(
zha_gateway: Gateway,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test global updater guards."""

assert (
"listener already registered with global updater - nothing to register"
not in caplog.text
)
assert (
"listener not registered with global updater - nothing to remove"
not in caplog.text
)

def listener():
pass

zha_gateway.global_updater.register_update_listener(listener)

assert (
"listener already registered with global updater - nothing to register"
not in caplog.text
)

zha_gateway.global_updater.register_update_listener(listener)

assert (
"listener already registered with global updater - nothing to register"
in caplog.text
)

zha_gateway.global_updater.remove_update_listener(listener)

assert (
"listener not registered with global updater - nothing to remove"
not in caplog.text
)

zha_gateway.global_updater.remove_update_listener(listener)

assert (
"listener not registered with global updater - nothing to remove" in caplog.text
)


async def test_gateway_handle_message(
zha_gateway: Gateway,
zha_dev_basic: Device, # pylint: disable=redefined-outer-name
Expand Down
10 changes: 10 additions & 0 deletions zha/application/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,21 @@ def stop(self):
def register_update_listener(self, listener: Callable):
"""Register an update listener."""
if listener in self._update_listeners:
_LOGGER.debug(
"listener already registered with global updater - nothing to register: %s",
listener,
)
return
self._update_listeners.append(listener)

def remove_update_listener(self, listener: Callable):
"""Remove an update listener."""
if listener not in self._update_listeners:
_LOGGER.debug(
"listener not registered with global updater - nothing to remove: %s",
listener,
)
return
self._update_listeners.remove(listener)

@periodic(_REFRESH_INTERVAL)
Expand Down

0 comments on commit ec2466d

Please sign in to comment.