Fix OMIE sensors not updating on setup (#172383)

This commit is contained in:
Abílio Costa
2026-06-01 16:04:53 +00:00
committed by Franck Nijhof
parent 2e4c6c4370
commit 9d60fce72e
2 changed files with 17 additions and 12 deletions
+5
View File
@@ -52,6 +52,11 @@ class OMIEPriceSensor(CoordinatorEntity[OMIECoordinator], SensorEntity):
self._attr_unique_id = pyomie_series_name
self._pyomie_series_name = pyomie_series_name
async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
await super().async_added_to_hass()
self._handle_coordinator_update()
@callback
def _handle_coordinator_update(self) -> None:
"""Update this sensor's state from the coordinator results."""
+12 -12
View File
@@ -10,35 +10,35 @@ import pytest
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import spot_price_fetcher
from tests.common import MockConfigEntry, async_fire_time_changed
@freeze_time("2024-01-15T14:01:00Z")
async def test_sensor_setup(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_pyomie: MagicMock,
entity_registry: er.EntityRegistry,
mock_omie_results_jan15: OMIEResults,
) -> None:
"""Test sensor platform setup."""
mock_pyomie.spot_price.side_effect = spot_price_fetcher(
{
"2024-01-15": mock_omie_results_jan15,
}
)
mock_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
entities = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
# Should have 2 sensors (PT and ES)
assert len(entities) == 2
unique_ids = {entity.unique_id for entity in entities}
expected_ids = {"pt_spot_price", "es_spot_price"}
assert unique_ids == expected_ids
assert (pt_state := hass.states.get("sensor.omie_portugal_spot_price"))
assert (es_state := hass.states.get("sensor.omie_spain_spot_price"))
assert pt_state.state == "351151500.0"
assert es_state.state == "34151500.0"
@pytest.mark.usefixtures("hass_lisbon")