"""Sensor for the OMIE - Spain and Portugal electricity prices integration.""" from homeassistant.components.sensor import ( SensorEntity, SensorEntityDescription, SensorStateClass, ) from homeassistant.const import CURRENCY_EURO, UnitOfEnergy from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import dt as dt_util from . import util from .const import DOMAIN from .coordinator import OMIEConfigEntry, OMIECoordinator PARALLEL_UPDATES = 0 _ATTRIBUTION = "Data provided by OMIE.es" SENSOR_DESCRIPTIONS: dict[str, SensorEntityDescription] = { key: SensorEntityDescription( key=key, translation_key=key, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}", suggested_display_precision=4, ) for key in ("pt_spot_price", "es_spot_price") } class OMIEPriceSensor(CoordinatorEntity[OMIECoordinator], SensorEntity): """OMIE price sensor.""" _attr_has_entity_name = True _attr_should_poll = False _attr_attribution = _ATTRIBUTION def __init__( self, coordinator: OMIECoordinator, device_info: DeviceInfo, pyomie_series_name: str, ) -> None: """Initialize the sensor.""" super().__init__(coordinator) self.entity_description = SENSOR_DESCRIPTIONS[pyomie_series_name] self._attr_device_info = device_info 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.""" value = self._get_current_quarter_hour_value() self._attr_available = value is not None self._attr_native_value = value if self._attr_available else None super()._handle_coordinator_update() @property def available(self) -> bool: """Return if entity is available.""" return super().available and self._attr_available def _get_current_quarter_hour_value(self) -> float | None: """Get current quarter-hour's price value from coordinator data.""" current_quarter_hour_cet = util.current_quarter_hour_cet(dt_util.now()) pyomie_results = self.coordinator.data pyomie_quarter_hours = util.pick_series_cet( pyomie_results, self._pyomie_series_name ) # Convert to €/kWh value_mwh = pyomie_quarter_hours.get(current_quarter_hour_cet) return value_mwh / 1000 if value_mwh is not None else None async def async_setup_entry( hass: HomeAssistant, entry: OMIEConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up OMIE from its config entry.""" coordinator = entry.runtime_data device_info = DeviceInfo( configuration_url="https://www.omie.es/en/market-results", entry_type=DeviceEntryType.SERVICE, identifiers={(DOMAIN, DOMAIN)}, name="OMIE", ) sensors = [ OMIEPriceSensor(coordinator, device_info, pyomie_series_name="pt_spot_price"), OMIEPriceSensor(coordinator, device_info, pyomie_series_name="es_spot_price"), ] async_add_entities(sensors)