Skip to content

Commit

Permalink
Add config option to disable polling mains devices at startup (#143)
Browse files Browse the repository at this point in the history
* Add config option to disable polling mains devices at startup

* rename

* add test

* rename
  • Loading branch information
dmulcahey authored Aug 17, 2024
1 parent c7c921b commit 6189a2a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,41 @@ async def test_gateway_starts_entity_exception(
await zha_gateway.shutdown()


@pytest.mark.parametrize(("enabled", "await_count"), [(True, 1), (False, 0)])
async def test_mains_devices_startup_polling_config(
zha_data: ZHAData,
zigpy_app_controller: ControllerApplication,
enabled: bool,
await_count: int,
) -> None:
"""Test mains powered device startup polling config is respected."""

with (
patch(
"bellows.zigbee.application.ControllerApplication.new",
return_value=zigpy_app_controller,
),
patch(
"bellows.zigbee.application.ControllerApplication",
return_value=zigpy_app_controller,
),
):
zha_data.config.device_options.enable_mains_startup_polling = enabled
zha_gateway = await Gateway.async_from_config(zha_data)
zha_gateway.async_fetch_updated_state_mains = AsyncMock(
wraps=zha_gateway.async_fetch_updated_state_mains
)
await zha_gateway.async_initialize()
await zha_gateway.async_block_till_done()
await zha_gateway.async_initialize_devices_and_entities()
await zha_gateway.async_block_till_done()

assert zha_gateway.async_fetch_updated_state_mains.await_count == await_count

await zha_gateway.shutdown()
await zha_gateway.async_block_till_done()


async def test_gateway_group_methods(
zha_gateway: Gateway,
device_light_1, # pylint: disable=redefined-outer-name
Expand Down
4 changes: 4 additions & 0 deletions zha/application/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ async def async_initialize_devices_and_entities(self) -> None:
*(dev.async_initialize(from_cache=True) for dev in self.devices.values())
)

if not self.config.config.device_options.enable_mains_startup_polling:
_LOGGER.debug("Polling of mains powered devices at startup is disabled")
return

async def fetch_updated_state() -> None:
"""Fetch updated state for mains powered devices."""
await self.async_fetch_updated_state_mains()
Expand Down
1 change: 1 addition & 0 deletions zha/application/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class DeviceOptions:
consider_unavailable_battery: int = dataclasses.field(
default=CONF_DEFAULT_CONSIDER_UNAVAILABLE_BATTERY
)
enable_mains_startup_polling: bool = dataclasses.field(default=True)


@dataclass(kw_only=True, slots=True)
Expand Down

0 comments on commit 6189a2a

Please sign in to comment.