Add update platform to LaMetric (#147354)

This commit is contained in:
Joost Lekkerkerker 2025-06-23 20:08:13 +02:00 committed by GitHub
parent 6b242fd277
commit 442fb88011
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 153 additions and 3 deletions

View File

@ -13,6 +13,7 @@ PLATFORMS = [
Platform.SELECT, Platform.SELECT,
Platform.SENSOR, Platform.SENSOR,
Platform.SWITCH, Platform.SWITCH,
Platform.UPDATE,
] ]
LOGGER = logging.getLogger(__package__) LOGGER = logging.getLogger(__package__)

View File

@ -0,0 +1,46 @@
"""LaMetric Update platform."""
from awesomeversion import AwesomeVersion
from homeassistant.components.update import UpdateDeviceClass, UpdateEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .coordinator import LaMetricConfigEntry, LaMetricDataUpdateCoordinator
from .entity import LaMetricEntity
async def async_setup_entry(
hass: HomeAssistant,
config_entry: LaMetricConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up LaMetric update platform."""
coordinator = config_entry.runtime_data
if coordinator.data.os_version >= AwesomeVersion("2.3.0"):
async_add_entities([LaMetricUpdate(coordinator)])
class LaMetricUpdate(LaMetricEntity, UpdateEntity):
"""Representation of LaMetric Update."""
_attr_device_class = UpdateDeviceClass.FIRMWARE
def __init__(self, coordinator: LaMetricDataUpdateCoordinator) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._attr_unique_id = f"{coordinator.data.serial_number}-update"
@property
def installed_version(self) -> str:
"""Return the installed version of the entity."""
return self.coordinator.data.os_version
@property
def latest_version(self) -> str | None:
"""Return the latest version of the entity."""
if not self.coordinator.data.update:
return None
return self.coordinator.data.update.version

View File

@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Generator from collections.abc import Generator
from contextlib import nullcontext
from unittest.mock import AsyncMock, MagicMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from demetriek import CloudDevice, Device from demetriek import CloudDevice, Device
@ -97,12 +98,20 @@ def mock_lametric(device_fixture: str) -> Generator[MagicMock]:
@pytest.fixture @pytest.fixture
async def init_integration( async def init_integration(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_lametric: MagicMock hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_lametric: MagicMock,
request: pytest.FixtureRequest,
) -> MockConfigEntry: ) -> MockConfigEntry:
"""Set up the LaMetric integration for testing.""" """Set up the LaMetric integration for testing."""
mock_config_entry.add_to_hass(hass) mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id) context = nullcontext()
await hass.async_block_till_done() if platform := getattr(request, "param", None):
context = patch("homeassistant.components.lametric.PLATFORMS", [platform])
with context:
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
return mock_config_entry return mock_config_entry

View File

@ -57,6 +57,9 @@
"name": "spyfly's LaMetric SKY", "name": "spyfly's LaMetric SKY",
"os_version": "3.0.13", "os_version": "3.0.13",
"serial_number": "SA52100000123TBNC", "serial_number": "SA52100000123TBNC",
"update_available": {
"version": "3.2.1"
},
"wifi": { "wifi": {
"active": true, "active": true,
"mac": "AA:BB:CC:DD:EE:FF", "mac": "AA:BB:CC:DD:EE:FF",

View File

@ -0,0 +1,62 @@
# serializer version: 1
# name: test_all_entities[device_sa5-update][update.spyfly_s_lametric_sky_firmware-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'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.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'update.spyfly_s_lametric_sky_firmware',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <UpdateDeviceClass.FIRMWARE: 'firmware'>,
'original_icon': None,
'original_name': 'Firmware',
'platform': 'lametric',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'SA52100000123TBNC-update',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[device_sa5-update][update.spyfly_s_lametric_sky_firmware-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'auto_update': False,
'device_class': 'firmware',
'display_precision': 0,
'entity_picture': 'https://brands.home-assistant.io/_/lametric/icon.png',
'friendly_name': "spyfly's LaMetric SKY Firmware",
'in_progress': False,
'installed_version': '3.0.13',
'latest_version': '3.2.1',
'release_summary': None,
'release_url': None,
'skipped_version': None,
'supported_features': <UpdateEntityFeature: 0>,
'title': None,
'update_percentage': None,
}),
'context': <ANY>,
'entity_id': 'update.spyfly_s_lametric_sky_firmware',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View File

@ -0,0 +1,29 @@
"""Tests for the LaMetric update platform."""
from unittest.mock import MagicMock
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
pytestmark = [
pytest.mark.parametrize("init_integration", [Platform.UPDATE], indirect=True),
pytest.mark.usefixtures("init_integration"),
]
@pytest.mark.parametrize("device_fixture", ["device_sa5"])
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_lametric: MagicMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)