mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 04:05:20 +01:00
Fix image platform state for Vodafone Station (#162747)
Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
@@ -12,6 +12,7 @@ from homeassistant.const import EntityCategory
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import _LOGGER
|
from .const import _LOGGER
|
||||||
from .coordinator import VodafoneConfigEntry, VodafoneStationRouter
|
from .coordinator import VodafoneConfigEntry, VodafoneStationRouter
|
||||||
@@ -75,9 +76,11 @@ class VodafoneGuestWifiQRImage(
|
|||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._attr_device_info = coordinator.device_info
|
self._attr_device_info = coordinator.device_info
|
||||||
self._attr_unique_id = f"{coordinator.serial_number}-{description.key}-qr-code"
|
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:
|
@property
|
||||||
"""Return QR code image bytes."""
|
def _qr_code(self) -> bytes:
|
||||||
|
"""Return QR code bytes."""
|
||||||
qr_code = cast(
|
qr_code = cast(
|
||||||
BytesIO,
|
BytesIO,
|
||||||
self.coordinator.data.wifi[WIFI_DATA][self.entity_description.key][
|
self.coordinator.data.wifi[WIFI_DATA][self.entity_description.key][
|
||||||
@@ -85,3 +88,24 @@ class VodafoneGuestWifiQRImage(
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
return qr_code.getvalue()
|
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
|
||||||
|
|||||||
@@ -47,7 +47,7 @@
|
|||||||
'last_changed': <ANY>,
|
'last_changed': <ANY>,
|
||||||
'last_reported': <ANY>,
|
'last_reported': <ANY>,
|
||||||
'last_updated': <ANY>,
|
'last_updated': <ANY>,
|
||||||
'state': 'unknown',
|
'state': '2026-01-05T15:00:00+00:00',
|
||||||
})
|
})
|
||||||
# ---
|
# ---
|
||||||
# name: test_all_entities[image.vodafone_station_m123456789_guest_network-entry]
|
# name: test_all_entities[image.vodafone_station_m123456789_guest_network-entry]
|
||||||
@@ -98,7 +98,7 @@
|
|||||||
'last_changed': <ANY>,
|
'last_changed': <ANY>,
|
||||||
'last_reported': <ANY>,
|
'last_reported': <ANY>,
|
||||||
'last_updated': <ANY>,
|
'last_updated': <ANY>,
|
||||||
'state': 'unknown',
|
'state': '2026-01-05T15:00:00+00:00',
|
||||||
})
|
})
|
||||||
# ---
|
# ---
|
||||||
# name: test_image_entity
|
# name: test_image_entity
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from homeassistant.config_entries import ConfigEntryState
|
|||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from . import setup_integration
|
from . import setup_integration
|
||||||
from .const import TEST_SERIAL_NUMBER
|
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)
|
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(
|
async def test_image_entity(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client: ClientSessionGenerator,
|
hass_client: ClientSessionGenerator,
|
||||||
@@ -82,7 +84,11 @@ async def test_image_entity(
|
|||||||
body = await resp.read()
|
body = await resp.read()
|
||||||
assert body == snapshot
|
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(
|
async def test_image_update(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client: ClientSessionGenerator,
|
hass_client: ClientSessionGenerator,
|
||||||
@@ -104,6 +110,9 @@ async def test_image_update(
|
|||||||
|
|
||||||
resp_body = await resp.read()
|
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 = {
|
mock_vodafone_station_router.get_wifi_data.return_value = {
|
||||||
WIFI_DATA: {
|
WIFI_DATA: {
|
||||||
"guest": {
|
"guest": {
|
||||||
@@ -122,6 +131,7 @@ async def test_image_update(
|
|||||||
freezer.tick(SCAN_INTERVAL)
|
freezer.tick(SCAN_INTERVAL)
|
||||||
async_fire_time_changed(hass)
|
async_fire_time_changed(hass)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
new_time = dt_util.utcnow()
|
||||||
|
|
||||||
resp = await client.get(f"/api/image_proxy/{entity_id}")
|
resp = await client.get(f"/api/image_proxy/{entity_id}")
|
||||||
assert resp.status == HTTPStatus.OK
|
assert resp.status == HTTPStatus.OK
|
||||||
@@ -129,6 +139,9 @@ async def test_image_update(
|
|||||||
resp_body_new = await resp.read()
|
resp_body_new = await resp.read()
|
||||||
assert resp_body != resp_body_new
|
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(
|
async def test_no_wifi_data(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
|||||||
Reference in New Issue
Block a user