mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00: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:
parent
02f108498c
commit
30af4c769e
@ -18,8 +18,6 @@ from .core.const import (
|
|||||||
from .core.gateway import ZHAGateway
|
from .core.gateway import ZHAGateway
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from zigpy.application import ControllerApplication
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
@ -49,21 +47,17 @@ def _get_config_entry(hass: HomeAssistant) -> ConfigEntry:
|
|||||||
return entries[0]
|
return entries[0]
|
||||||
|
|
||||||
|
|
||||||
def _wrap_network_settings(app: ControllerApplication) -> NetworkBackup:
|
def async_get_active_network_settings(hass: HomeAssistant) -> NetworkBackup:
|
||||||
"""Wrap the ZHA network settings into a `NetworkBackup`."""
|
"""Get the network settings for the currently active ZHA network."""
|
||||||
|
zha_gateway: ZHAGateway = _get_gateway(hass)
|
||||||
|
app = zha_gateway.application_controller
|
||||||
|
|
||||||
return NetworkBackup(
|
return NetworkBackup(
|
||||||
node_info=app.state.node_info,
|
node_info=app.state.node_info,
|
||||||
network_info=app.state.network_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(
|
async def async_get_last_network_settings(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry | None = None
|
hass: HomeAssistant, config_entry: ConfigEntry | None = None
|
||||||
) -> NetworkBackup | None:
|
) -> NetworkBackup | None:
|
||||||
@ -79,13 +73,12 @@ async def async_get_last_network_settings(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
await app._load_db() # pylint: disable=protected-access
|
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:
|
finally:
|
||||||
await app.shutdown()
|
await app.shutdown()
|
||||||
|
|
||||||
if settings.network_info.channel == 0:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return settings
|
return settings
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import zigpy.backups
|
||||||
import zigpy.state
|
import zigpy.state
|
||||||
|
|
||||||
from homeassistant.components import zha
|
from homeassistant.components import zha
|
||||||
@ -36,7 +37,9 @@ async def test_async_get_network_settings_inactive(
|
|||||||
gateway = api._get_gateway(hass)
|
gateway = api._get_gateway(hass)
|
||||||
await zha.async_unload_entry(hass, gateway.config_entry)
|
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(
|
with patch(
|
||||||
"bellows.zigbee.application.ControllerApplication.__new__",
|
"bellows.zigbee.application.ControllerApplication.__new__",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user