1
mirror of https://github.com/home-assistant/core synced 2024-10-07 10:13:38 +02:00

Correctly load ZHA settings from API when integration is not running (#90476)

Correctly load settings from the zigpy database when ZHA is not running
This commit is contained in:
puddly 2023-03-29 17:24:26 -04:00 committed by GitHub
parent 43a7247dde
commit d0a492644d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 16 deletions

View File

@ -18,8 +18,6 @@ from .core.const import (
from .core.gateway import ZHAGateway
if TYPE_CHECKING:
from zigpy.application import ControllerApplication
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@ -49,21 +47,17 @@ def _get_config_entry(hass: HomeAssistant) -> ConfigEntry:
return entries[0]
def _wrap_network_settings(app: ControllerApplication) -> NetworkBackup:
"""Wrap the ZHA network settings into a `NetworkBackup`."""
def async_get_active_network_settings(hass: HomeAssistant) -> NetworkBackup:
"""Get the network settings for the currently active ZHA network."""
zha_gateway: ZHAGateway = _get_gateway(hass)
app = zha_gateway.application_controller
return NetworkBackup(
node_info=app.state.node_info,
network_info=app.state.network_info,
)
def async_get_active_network_settings(hass: HomeAssistant) -> NetworkBackup:
"""Get the network settings for the currently active ZHA network."""
zha_gateway: ZHAGateway = _get_gateway(hass)
return _wrap_network_settings(zha_gateway.application_controller)
async def async_get_last_network_settings(
hass: HomeAssistant, config_entry: ConfigEntry | None = None
) -> NetworkBackup | None:
@ -79,13 +73,12 @@ async def async_get_last_network_settings(
try:
await app._load_db() # pylint: disable=protected-access
settings = _wrap_network_settings(app)
settings = max(app.backups, key=lambda b: b.backup_time)
except ValueError:
settings = None
finally:
await app.shutdown()
if settings.network_info.channel == 0:
return None
return settings

View File

@ -2,6 +2,7 @@
from unittest.mock import patch
import pytest
import zigpy.backups
import zigpy.state
from homeassistant.components import zha
@ -36,7 +37,9 @@ async def test_async_get_network_settings_inactive(
gateway = api._get_gateway(hass)
await zha.async_unload_entry(hass, gateway.config_entry)
zigpy_app_controller.state.network_info.channel = 20
backup = zigpy.backups.NetworkBackup()
backup.network_info.channel = 20
zigpy_app_controller.backups.backups.append(backup)
with patch(
"bellows.zigbee.application.ControllerApplication.__new__",