mirror of
https://github.com/home-assistant/core.git
synced 2025-08-07 14:45:09 +02:00
Unit test get_zha_firmware_info
This commit is contained in:
@@ -1,13 +1,17 @@
|
|||||||
"""Test hardware utilities."""
|
"""Test hardware utilities."""
|
||||||
|
|
||||||
|
import sys
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.hassio import AddonError, AddonInfo, AddonState
|
from homeassistant.components.hassio import AddonError, AddonInfo, AddonState
|
||||||
from homeassistant.components.homeassistant_hardware.util import (
|
from homeassistant.components.homeassistant_hardware.util import (
|
||||||
ApplicationType,
|
ApplicationType,
|
||||||
FirmwareInfo,
|
FirmwareInfo,
|
||||||
OwningAddon,
|
OwningAddon,
|
||||||
OwningIntegration,
|
OwningIntegration,
|
||||||
|
get_zha_firmware_info,
|
||||||
guess_firmware_info,
|
guess_firmware_info,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
@@ -234,3 +238,97 @@ async def test_firmware_info(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert (await firmware_info2.is_running(hass)) is False
|
assert (await firmware_info2.is_running(hass)) is False
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_zha_firmware_info_normal(hass: HomeAssistant) -> None:
|
||||||
|
"""Test `get_zha_firmware_info`."""
|
||||||
|
|
||||||
|
zha = MockConfigEntry(
|
||||||
|
domain="zha",
|
||||||
|
unique_id="some_unique_id",
|
||||||
|
data={
|
||||||
|
"device": {
|
||||||
|
"path": "/dev/ttyUSB1",
|
||||||
|
"baudrate": 115200,
|
||||||
|
"flow_control": None,
|
||||||
|
},
|
||||||
|
"radio_type": "ezsp",
|
||||||
|
},
|
||||||
|
version=4,
|
||||||
|
)
|
||||||
|
zha.add_to_hass(hass)
|
||||||
|
zha.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
|
|
||||||
|
# With ZHA running
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.zha.helpers.get_zha_gateway"
|
||||||
|
) as mock_get_zha_gateway:
|
||||||
|
mock_get_zha_gateway.return_value.state.node_info.version = "1.2.3.4"
|
||||||
|
fw_info_running = await get_zha_firmware_info(hass, zha)
|
||||||
|
|
||||||
|
assert fw_info_running == FirmwareInfo(
|
||||||
|
device="/dev/ttyUSB1",
|
||||||
|
firmware_type=ApplicationType.EZSP,
|
||||||
|
firmware_version="1.2.3.4",
|
||||||
|
source="zha",
|
||||||
|
owners=[OwningIntegration(config_entry_id=zha.entry_id)],
|
||||||
|
)
|
||||||
|
assert await fw_info_running.is_running(hass) is True
|
||||||
|
|
||||||
|
# With ZHA not running
|
||||||
|
zha.mock_state(hass, ConfigEntryState.NOT_LOADED)
|
||||||
|
fw_info_not_running = await get_zha_firmware_info(hass, zha)
|
||||||
|
|
||||||
|
assert fw_info_not_running == FirmwareInfo(
|
||||||
|
device="/dev/ttyUSB1",
|
||||||
|
firmware_type=ApplicationType.EZSP,
|
||||||
|
firmware_version=None,
|
||||||
|
source="zha",
|
||||||
|
owners=[OwningIntegration(config_entry_id=zha.entry_id)],
|
||||||
|
)
|
||||||
|
assert await fw_info_not_running.is_running(hass) is False
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"data",
|
||||||
|
[
|
||||||
|
# Missing data
|
||||||
|
{},
|
||||||
|
# Bad radio type
|
||||||
|
{"device": {"path": "/dev/ttyUSB1"}, "radio_type": "znp"},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_get_zha_firmware_info_errors(
|
||||||
|
hass: HomeAssistant, data: dict[str, str | int | None]
|
||||||
|
) -> None:
|
||||||
|
"""Test `get_zha_firmware_info` with config entry data format errors."""
|
||||||
|
zha = MockConfigEntry(
|
||||||
|
domain="zha",
|
||||||
|
unique_id="some_unique_id",
|
||||||
|
data=data,
|
||||||
|
version=4,
|
||||||
|
)
|
||||||
|
zha.add_to_hass(hass)
|
||||||
|
|
||||||
|
assert (await get_zha_firmware_info(hass, zha)) is None
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_zha_firmware_info_stale(hass: HomeAssistant) -> None:
|
||||||
|
"""Test `get_zha_firmware_info` with a stale config entry."""
|
||||||
|
zha = MockConfigEntry(
|
||||||
|
domain="zha",
|
||||||
|
unique_id="some_unique_id",
|
||||||
|
data={
|
||||||
|
"device": {
|
||||||
|
"path": "/dev/ttyUSB1",
|
||||||
|
"baudrate": 115200,
|
||||||
|
"flow_control": None,
|
||||||
|
},
|
||||||
|
"radio_type": "ezsp",
|
||||||
|
},
|
||||||
|
version=4,
|
||||||
|
)
|
||||||
|
zha.add_to_hass(hass)
|
||||||
|
|
||||||
|
with patch.dict(sys.modules, {"homeassistant.components.zha.helpers": None}):
|
||||||
|
assert (await get_zha_firmware_info(hass, zha)) is None
|
||||||
|
Reference in New Issue
Block a user