WIP start adding support for sensors for current weather

This commit is contained in:
Jules 2025-02-21 20:47:22 +01:00
parent fbab30e33f
commit fb59936c79
Signed by: jdejaegh
GPG key ID: 99D6D184CA66933A
3 changed files with 82 additions and 3 deletions

View file

@ -1,6 +1,7 @@
"""Constants for the IRM KMI integration."""
from typing import Final
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.weather import (ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY,
ATTR_CONDITION_FOG,
@ -11,7 +12,7 @@ from homeassistant.components.weather import (ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_SNOWY,
ATTR_CONDITION_SNOWY_RAINY,
ATTR_CONDITION_SUNNY)
from homeassistant.const import Platform
from homeassistant.const import Platform, UnitOfPressure, UnitOfSpeed, UnitOfTemperature, DEGREE
DOMAIN: Final = 'irm_kmi'
PLATFORMS: Final = [Platform.WEATHER, Platform.CAMERA, Platform.BINARY_SENSOR, Platform.SENSOR]
@ -160,4 +161,23 @@ IRM_KMI_NAME: Final = {
WEEKDAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
USER_AGENT: Final = 'github.com/jdejaegh/irm-kmi-ha 0.2.29'
USER_AGENT: Final = 'github.com/jdejaegh/irm-kmi-ha 0.2.29'
CURRENT_WEATHER_SENSORS: Final = {'temperature', 'wind_speed', 'wind_gust_speed', 'wind_bearing', 'uv_index',
'pressure'}
CURRENT_WEATHER_SENSOR_UNITS: Final = {'temperature': UnitOfTemperature.CELSIUS,
'wind_speed': UnitOfSpeed.KILOMETERS_PER_HOUR,
'wind_gust_speed': UnitOfSpeed.KILOMETERS_PER_HOUR,
'wind_bearing': DEGREE,
'uv_index': None,
'pressure': UnitOfPressure.HPA}
CURRENT_WEATHER_SENSOR_CLASS: Final = {'temperature': SensorDeviceClass.TEMPERATURE,
'wind_speed': SensorDeviceClass.WIND_SPEED,
'wind_gust_speed': SensorDeviceClass.WIND_SPEED,
'wind_bearing': None,
'uv_index': None,
'pressure': SensorDeviceClass.ATMOSPHERIC_PRESSURE}
# TODO add sensor icon map for nice display

View file

@ -11,7 +11,8 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt
from custom_components.irm_kmi import DOMAIN, IrmKmiCoordinator
from custom_components.irm_kmi.const import POLLEN_NAMES, POLLEN_TO_ICON_MAP
from custom_components.irm_kmi.const import POLLEN_NAMES, POLLEN_TO_ICON_MAP, CURRENT_WEATHER_SENSOR_UNITS, \
CURRENT_WEATHER_SENSOR_CLASS, CURRENT_WEATHER_SENSORS
from custom_components.irm_kmi.data import IrmKmiForecast
from custom_components.irm_kmi.pollen import PollenParser
@ -22,6 +23,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
"""Set up the sensor platform"""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities([IrmKmiPollen(coordinator, entry, pollen.lower()) for pollen in POLLEN_NAMES])
async_add_entities([IrmKmiCurrentWeather(coordinator, entry, name) for name in CURRENT_WEATHER_SENSORS])
async_add_entities([IrmKmiNextWarning(coordinator, entry),])
if coordinator.data.get('country') != 'NL':
@ -135,3 +137,36 @@ class IrmKmiNextSunMove(CoordinatorEntity, SensorEntity):
if len(upcoming) > 0:
return upcoming[0]
return None
class IrmKmiCurrentWeather(CoordinatorEntity, SensorEntity):
"""Representation of a current weather sensor"""
_attr_has_entity_name = True
_attr_attribution = "Weather data from the Royal Meteorological Institute of Belgium meteo.be"
def __init__(self,
coordinator: IrmKmiCoordinator,
entry: ConfigEntry,
sensor_name: str) -> None:
super().__init__(coordinator)
SensorEntity.__init__(self)
self._attr_unique_id = f"{entry.entry_id}-current-{sensor_name}"
self.entity_id = sensor.ENTITY_ID_FORMAT.format(f"{str(entry.title).lower()}_next_{sensor_name}")
self._attr_device_info = coordinator.shared_device_info
# TODO
# self._attr_translation_key = f"next_{move}"
self._sensor_name: str = sensor_name
@property
def native_value(self) -> float | None:
"""Return the current value of the sensor"""
return self.coordinator.data.get('current_weather', {}).get(self._sensor_name, None)
@property
def native_unit_of_measurement(self) -> str | None:
return CURRENT_WEATHER_SENSOR_UNITS[self._sensor_name]
@property
def device_class(self) -> SensorDeviceClass | None:
return CURRENT_WEATHER_SENSOR_CLASS[self._sensor_name]

View file

@ -0,0 +1,24 @@
import inspect
from custom_components.irm_kmi.const import CURRENT_WEATHER_SENSORS, CURRENT_WEATHER_SENSOR_UNITS, \
CURRENT_WEATHER_SENSOR_CLASS
from custom_components.irm_kmi.data import CurrentWeatherData
def test_sensors_in_current_weather_data():
weather_data_keys = inspect.get_annotations(CurrentWeatherData).keys()
for sensor in CURRENT_WEATHER_SENSORS:
assert sensor in weather_data_keys
def test_sensors_have_unit():
weather_sensor_units_keys = CURRENT_WEATHER_SENSOR_UNITS.keys()
for sensor in CURRENT_WEATHER_SENSORS:
assert sensor in weather_sensor_units_keys
def test_sensors_have_class():
weather_sensor_class_keys = CURRENT_WEATHER_SENSOR_CLASS.keys()
for sensor in CURRENT_WEATHER_SENSORS:
assert sensor in weather_sensor_class_keys