From 3a4100fa94352b51075645cd658fa0e0fa0308a1 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Wed, 11 Feb 2026 17:39:51 +0100 Subject: [PATCH] Fix image platform state for Vodafone Station (#162747) Co-authored-by: Joostlek --- .../components/vodafone_station/image.py | 28 +++++++++++++++++-- .../snapshots/test_image.ambr | 4 +-- .../components/vodafone_station/test_image.py | 13 +++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/vodafone_station/image.py b/homeassistant/components/vodafone_station/image.py index f28d7eb9955..be549df6418 100644 --- a/homeassistant/components/vodafone_station/image.py +++ b/homeassistant/components/vodafone_station/image.py @@ -12,6 +12,7 @@ from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.util import dt as dt_util from .const import _LOGGER from .coordinator import VodafoneConfigEntry, VodafoneStationRouter @@ -75,9 +76,11 @@ class VodafoneGuestWifiQRImage( self.entity_description = description self._attr_device_info = coordinator.device_info self._attr_unique_id = f"{coordinator.serial_number}-{description.key}-qr-code" + self._cached_qr_code: bytes | None = None - async def async_image(self) -> bytes | None: - """Return QR code image bytes.""" + @property + def _qr_code(self) -> bytes: + """Return QR code bytes.""" qr_code = cast( BytesIO, self.coordinator.data.wifi[WIFI_DATA][self.entity_description.key][ @@ -85,3 +88,24 @@ class VodafoneGuestWifiQRImage( ], ) return qr_code.getvalue() + + async def async_added_to_hass(self) -> None: + """Set the update time.""" + self._attr_image_last_updated = dt_util.utcnow() + await super().async_added_to_hass() + + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator. + + If the coordinator has updated the QR code, we can update the image. + """ + qr_code = self._qr_code + if self._cached_qr_code != qr_code: + self._cached_qr_code = qr_code + self._attr_image_last_updated = dt_util.utcnow() + + super()._handle_coordinator_update() + + async def async_image(self) -> bytes | None: + """Return QR code image.""" + return self._qr_code diff --git a/tests/components/vodafone_station/snapshots/test_image.ambr b/tests/components/vodafone_station/snapshots/test_image.ambr index 07ff84dc325..6b6071e1e2b 100644 --- a/tests/components/vodafone_station/snapshots/test_image.ambr +++ b/tests/components/vodafone_station/snapshots/test_image.ambr @@ -47,7 +47,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '2026-01-05T15:00:00+00:00', }) # --- # name: test_all_entities[image.vodafone_station_m123456789_guest_network-entry] @@ -98,7 +98,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '2026-01-05T15:00:00+00:00', }) # --- # name: test_image_entity diff --git a/tests/components/vodafone_station/test_image.py b/tests/components/vodafone_station/test_image.py index 4fc7b2a60cf..bb97f80c457 100644 --- a/tests/components/vodafone_station/test_image.py +++ b/tests/components/vodafone_station/test_image.py @@ -15,6 +15,7 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er +from homeassistant.util import dt as dt_util from . import setup_integration from .const import TEST_SERIAL_NUMBER @@ -39,6 +40,7 @@ async def test_all_entities( await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) +@pytest.mark.freeze_time("2023-12-02T13:00:00+00:00") async def test_image_entity( hass: HomeAssistant, hass_client: ClientSessionGenerator, @@ -82,7 +84,11 @@ async def test_image_entity( body = await resp.read() assert body == snapshot + assert (state := hass.states.async_all(IMAGE_DOMAIN)[0]) + assert state.state == "2023-12-02T13:00:00+00:00" + +@pytest.mark.freeze_time("2023-12-02T13:00:00+00:00") async def test_image_update( hass: HomeAssistant, hass_client: ClientSessionGenerator, @@ -104,6 +110,9 @@ async def test_image_update( resp_body = await resp.read() + assert (state := hass.states.get(entity_id)) + assert state.state == "2023-12-02T13:00:00+00:00" + mock_vodafone_station_router.get_wifi_data.return_value = { WIFI_DATA: { "guest": { @@ -122,6 +131,7 @@ async def test_image_update( freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done() + new_time = dt_util.utcnow() resp = await client.get(f"/api/image_proxy/{entity_id}") assert resp.status == HTTPStatus.OK @@ -129,6 +139,9 @@ async def test_image_update( resp_body_new = await resp.read() assert resp_body != resp_body_new + assert (state := hass.states.get(entity_id)) + assert state.state == new_time.isoformat() + async def test_no_wifi_data( hass: HomeAssistant,