Add firmware update platform to WattWächter Plus (#180203)

This commit is contained in:
smartcircuits
2026-08-26 17:44:14 +02:00
committed by GitHub
parent 9feefc0ebe
commit 2fcb86ffc8
8 changed files with 379 additions and 11 deletions
+24 -1
View File
@@ -3,7 +3,13 @@
from collections.abc import Generator
from unittest.mock import AsyncMock, MagicMock, patch
from aio_wattwaechter.models import AliveResponse, _parse_meter_data, _parse_system_info
from aio_wattwaechter.models import (
AliveResponse,
OtaCheckResponse,
OtaData,
_parse_meter_data,
_parse_system_info,
)
import pytest
from homeassistant.components.wattwaechter.const import CONF_FW_VERSION, DOMAIN
@@ -49,6 +55,21 @@ MOCK_METER_DATA_MINIMAL = _parse_meter_data(
load_json_object_fixture("meter_data_minimal.json", DOMAIN)
)
MOCK_OTA_CHECK = OtaCheckResponse(
ok=True,
data=OtaData(
update_available=True,
version="1.3.0",
tag="v1.3.0",
release_date="2026-07-01",
release_note_de="Fehlerbehebungen und Verbesserungen",
release_note_en="Bug fixes and improvements",
last_checked=1720000000,
url="https://example.com/firmware/1.3.0.bin",
md5="0123456789abcdef",
),
)
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
@@ -78,6 +99,8 @@ def mock_client() -> Generator[AsyncMock]:
client.system_info = AsyncMock(return_value=MOCK_SYSTEM_INFO)
client.settings = AsyncMock(return_value=MOCK_SETTINGS)
client.meter_data = AsyncMock(return_value=MOCK_METER_DATA)
client.ota_check = AsyncMock(return_value=MOCK_OTA_CHECK)
client.ota_start = AsyncMock(return_value=True)
yield client
@@ -0,0 +1,64 @@
# serializer version: 1
# name: test_all_entities[update.haushalt_test_firmware-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.haushalt_test_firmware',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Firmware',
'options': dict({
}),
'original_device_class': <UpdateDeviceClass.FIRMWARE: 'firmware'>,
'original_icon': None,
'original_name': 'Firmware',
'platform': 'wattwaechter',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <UpdateEntityFeature: 17>,
'translation_key': None,
'unique_id': 'ABC123',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[update.haushalt_test_firmware-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<UpdateEntityStateAttribute.AUTO_UPDATE: 'auto_update'>: False,
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'firmware',
<UpdateEntityStateAttribute.DISPLAY_PRECISION: 'display_precision'>: 0,
<EntityStateAttribute.ENTITY_PICTURE: 'entity_picture'>: '/api/brands/integration/wattwaechter/icon.png',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Haushalt Test Firmware',
<UpdateEntityStateAttribute.IN_PROGRESS: 'in_progress'>: False,
<UpdateEntityStateAttribute.INSTALLED_VERSION: 'installed_version'>: '1.2.3',
<UpdateEntityStateAttribute.LATEST_VERSION: 'latest_version'>: '1.3.0',
<UpdateEntityStateAttribute.RELEASE_SUMMARY: 'release_summary'>: None,
<UpdateEntityStateAttribute.RELEASE_URL: 'release_url'>: None,
<UpdateEntityStateAttribute.SKIPPED_VERSION: 'skipped_version'>: None,
<EntityStateAttribute.SUPPORTED_FEATURES: 'supported_features'>: <UpdateEntityFeature: 17>,
<UpdateEntityStateAttribute.TITLE: 'title'>: None,
<UpdateEntityStateAttribute.UPDATE_PERCENTAGE: 'update_percentage'>: None,
}),
'context': <ANY>,
'entity_id': 'update.haushalt_test_firmware',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
+5 -4
View File
@@ -3,7 +3,7 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, patch
from aio_wattwaechter import (
WattwaechterConnectionError,
@@ -16,7 +16,7 @@ import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.wattwaechter.const import DEFAULT_SCAN_INTERVAL, DOMAIN
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -34,8 +34,9 @@ async def test_all_entities(
mock_client: AsyncMock,
) -> None:
"""Test all sensor entities created from a full OBIS payload."""
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
with patch("homeassistant.components.wattwaechter.PLATFORMS", [Platform.SENSOR]):
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@@ -0,0 +1,183 @@
"""Tests for the WattWächter Plus update platform."""
from dataclasses import replace
from unittest.mock import AsyncMock, patch
from aio_wattwaechter import WattwaechterConnectionError
from aio_wattwaechter.models import OtaCheckResponse
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.update import DOMAIN as UPDATE_DOMAIN, SERVICE_INSTALL
from homeassistant.components.wattwaechter.const import DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from .conftest import MOCK_DEVICE_ID, MOCK_FW_VERSION, MOCK_OTA_CHECK
from tests.common import MockConfigEntry, snapshot_platform
from tests.typing import WebSocketGenerator
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
mock_client: AsyncMock,
) -> None:
"""Test the firmware update entity."""
with patch("homeassistant.components.wattwaechter.PLATFORMS", [Platform.UPDATE]):
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_install(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: AsyncMock,
entity_registry: er.EntityRegistry,
) -> None:
"""Test installing a firmware update triggers ota_start."""
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
entity_id = entity_registry.async_get_entity_id("update", DOMAIN, MOCK_DEVICE_ID)
assert entity_id is not None
await hass.services.async_call(
UPDATE_DOMAIN,
SERVICE_INSTALL,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mock_client.ota_start.assert_awaited_once()
@pytest.mark.parametrize(
("ota_start_return", "ota_start_side_effect"),
[
pytest.param(False, None, id="rejected"),
pytest.param(True, WattwaechterConnectionError("offline"), id="error"),
],
)
async def test_install_fails(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: AsyncMock,
entity_registry: er.EntityRegistry,
ota_start_return: bool,
ota_start_side_effect: Exception | None,
) -> None:
"""Test a rejected or failed firmware update raises an error."""
mock_client.ota_start.return_value = ota_start_return
mock_client.ota_start.side_effect = ota_start_side_effect
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
entity_id = entity_registry.async_get_entity_id("update", DOMAIN, MOCK_DEVICE_ID)
assert entity_id is not None
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
UPDATE_DOMAIN,
SERVICE_INSTALL,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
async def test_installed_version_falls_back_without_system_info(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: AsyncMock,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the installed version falls back to the stored version."""
mock_client.system_info.side_effect = WattwaechterConnectionError("offline")
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
entity_id = entity_registry.async_get_entity_id("update", DOMAIN, MOCK_DEVICE_ID)
assert entity_id is not None
assert hass.states.get(entity_id).attributes["installed_version"] == MOCK_FW_VERSION
@pytest.mark.parametrize(
("ota_check_return", "ota_check_side_effect", "expected_latest"),
[
pytest.param(
OtaCheckResponse(
ok=True, data=replace(MOCK_OTA_CHECK.data, update_available=False)
),
None,
MOCK_FW_VERSION,
id="up_to_date",
),
pytest.param(
MOCK_OTA_CHECK,
WattwaechterConnectionError("offline"),
None,
id="ota_unavailable",
),
],
)
async def test_latest_version(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_client: AsyncMock,
entity_registry: er.EntityRegistry,
ota_check_return: OtaCheckResponse,
ota_check_side_effect: Exception | None,
expected_latest: str | None,
) -> None:
"""Test the latest version reflects the OTA status."""
mock_client.ota_check.return_value = ota_check_return
mock_client.ota_check.side_effect = ota_check_side_effect
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
entity_id = entity_registry.async_get_entity_id("update", DOMAIN, MOCK_DEVICE_ID)
assert entity_id is not None
assert hass.states.get(entity_id).attributes["latest_version"] == expected_latest
@pytest.mark.parametrize(
("ota_check_side_effect", "expected_notes"),
[
pytest.param(None, "Bug fixes and improvements", id="available"),
pytest.param(WattwaechterConnectionError("offline"), None, id="unavailable"),
],
)
async def test_release_notes(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
mock_config_entry: MockConfigEntry,
mock_client: AsyncMock,
entity_registry: er.EntityRegistry,
ota_check_side_effect: Exception | None,
expected_notes: str | None,
) -> None:
"""Test the firmware release notes are exposed."""
mock_client.ota_check.side_effect = ota_check_side_effect
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
entity_id = entity_registry.async_get_entity_id("update", DOMAIN, MOCK_DEVICE_ID)
assert entity_id is not None
client = await hass_ws_client(hass)
await client.send_json_auto_id(
{"type": "update/release_notes", "entity_id": entity_id}
)
result = await client.receive_json()
assert result["result"] == expected_notes