From a639cb7ba787458c56420777845f627d121a2947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Moreno?= Date: Tue, 29 Jun 2021 13:52:39 +0200 Subject: [PATCH] Add sensor platform to Meteoclimatic integration (#51467) * Add meteoclimatic sensor platform Signed-off-by: Adrian Moreno * Add sensor.py to coverage file Signed-off-by: Adrian Moreno * Add explicit return type None Signed-off-by: Adrian Moreno * Fix sample station code Signed-off-by: Adrian Moreno * Apply frenck suggestions Signed-off-by: Adrian Moreno * Remove extra attributes Signed-off-by: Adrian Moreno * Revert translations Signed-off-by: Adrian Moreno * Remove None icons and classes Signed-off-by: Adrian Moreno --- .coveragerc | 1 + .../components/meteoclimatic/__init__.py | 2 +- .../components/meteoclimatic/const.py | 22 +++--- .../components/meteoclimatic/sensor.py | 79 +++++++++++++++++++ .../components/meteoclimatic/weather.py | 13 ++- 5 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 homeassistant/components/meteoclimatic/sensor.py diff --git a/.coveragerc b/.coveragerc index 29ba3dcdeb1..ade61c1fb07 100644 --- a/.coveragerc +++ b/.coveragerc @@ -611,6 +611,7 @@ omit = homeassistant/components/meteoalarm/* homeassistant/components/meteoclimatic/__init__.py homeassistant/components/meteoclimatic/const.py + homeassistant/components/meteoclimatic/sensor.py homeassistant/components/meteoclimatic/weather.py homeassistant/components/metoffice/sensor.py homeassistant/components/metoffice/weather.py diff --git a/homeassistant/components/meteoclimatic/__init__.py b/homeassistant/components/meteoclimatic/__init__.py index 20f72fd4410..58e51d0490a 100644 --- a/homeassistant/components/meteoclimatic/__init__.py +++ b/homeassistant/components/meteoclimatic/__init__.py @@ -31,7 +31,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: coordinator = DataUpdateCoordinator( hass, _LOGGER, - name=f"Meteoclimatic Coordinator for {station_code}", + name=f"Meteoclimatic weather for {entry.title} ({station_code})", update_method=async_update_data, update_interval=SCAN_INTERVAL, ) diff --git a/homeassistant/components/meteoclimatic/const.py b/homeassistant/components/meteoclimatic/const.py index eb3823a9b42..cd4be5821ea 100644 --- a/homeassistant/components/meteoclimatic/const.py +++ b/homeassistant/components/meteoclimatic/const.py @@ -34,8 +34,10 @@ from homeassistant.const import ( ) DOMAIN = "meteoclimatic" -PLATFORMS = ["weather"] +PLATFORMS = ["sensor", "weather"] ATTRIBUTION = "Data provided by Meteoclimatic" +MODEL = "Meteoclimatic RSS feed" +MANUFACTURER = "Meteoclimatic" SCAN_INTERVAL = timedelta(minutes=10) @@ -54,12 +56,12 @@ SENSOR_TYPES = { SENSOR_TYPE_CLASS: DEVICE_CLASS_TEMPERATURE, }, "temp_max": { - SENSOR_TYPE_NAME: "Max Temp.", + SENSOR_TYPE_NAME: "Daily Max Temperature", SENSOR_TYPE_UNIT: TEMP_CELSIUS, SENSOR_TYPE_CLASS: DEVICE_CLASS_TEMPERATURE, }, "temp_min": { - SENSOR_TYPE_NAME: "Min Temp.", + SENSOR_TYPE_NAME: "Daily Min Temperature", SENSOR_TYPE_UNIT: TEMP_CELSIUS, SENSOR_TYPE_CLASS: DEVICE_CLASS_TEMPERATURE, }, @@ -69,12 +71,12 @@ SENSOR_TYPES = { SENSOR_TYPE_CLASS: DEVICE_CLASS_HUMIDITY, }, "humidity_max": { - SENSOR_TYPE_NAME: "Max Humidity", + SENSOR_TYPE_NAME: "Daily Max Humidity", SENSOR_TYPE_UNIT: PERCENTAGE, SENSOR_TYPE_CLASS: DEVICE_CLASS_HUMIDITY, }, "humidity_min": { - SENSOR_TYPE_NAME: "Min Humidity", + SENSOR_TYPE_NAME: "Daily Min Humidity", SENSOR_TYPE_UNIT: PERCENTAGE, SENSOR_TYPE_CLASS: DEVICE_CLASS_HUMIDITY, }, @@ -84,12 +86,12 @@ SENSOR_TYPES = { SENSOR_TYPE_CLASS: DEVICE_CLASS_PRESSURE, }, "pressure_max": { - SENSOR_TYPE_NAME: "Max Pressure", + SENSOR_TYPE_NAME: "Daily Max Pressure", SENSOR_TYPE_UNIT: PRESSURE_HPA, SENSOR_TYPE_CLASS: DEVICE_CLASS_PRESSURE, }, "pressure_min": { - SENSOR_TYPE_NAME: "Min Pressure", + SENSOR_TYPE_NAME: "Daily Min Pressure", SENSOR_TYPE_UNIT: PRESSURE_HPA, SENSOR_TYPE_CLASS: DEVICE_CLASS_PRESSURE, }, @@ -99,7 +101,7 @@ SENSOR_TYPES = { SENSOR_TYPE_ICON: "mdi:weather-windy", }, "wind_max": { - SENSOR_TYPE_NAME: "Max Wind Speed", + SENSOR_TYPE_NAME: "Daily Max Wind Speed", SENSOR_TYPE_UNIT: SPEED_KILOMETERS_PER_HOUR, SENSOR_TYPE_ICON: "mdi:weather-windy", }, @@ -109,9 +111,9 @@ SENSOR_TYPES = { SENSOR_TYPE_ICON: "mdi:weather-windy", }, "rain": { - SENSOR_TYPE_NAME: "Rain", + SENSOR_TYPE_NAME: "Daily Precipitation", SENSOR_TYPE_UNIT: LENGTH_MILLIMETERS, - SENSOR_TYPE_ICON: "mdi:weather-rainy", + SENSOR_TYPE_ICON: "mdi:cup-water", }, } diff --git a/homeassistant/components/meteoclimatic/sensor.py b/homeassistant/components/meteoclimatic/sensor.py new file mode 100644 index 00000000000..bcd597d4b0c --- /dev/null +++ b/homeassistant/components/meteoclimatic/sensor.py @@ -0,0 +1,79 @@ +"""Support for Meteoclimatic sensor.""" +import logging + +from homeassistant.components.sensor import SensorEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ATTR_ATTRIBUTION +from homeassistant.helpers.typing import HomeAssistantType +from homeassistant.helpers.update_coordinator import ( + CoordinatorEntity, + DataUpdateCoordinator, +) + +from .const import ( + ATTRIBUTION, + DOMAIN, + MANUFACTURER, + MODEL, + SENSOR_TYPE_CLASS, + SENSOR_TYPE_ICON, + SENSOR_TYPE_NAME, + SENSOR_TYPE_UNIT, + SENSOR_TYPES, +) + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistantType, entry: ConfigEntry, async_add_entities +) -> None: + """Set up the Meteoclimatic sensor platform.""" + coordinator = hass.data[DOMAIN][entry.entry_id] + + async_add_entities( + [MeteoclimaticSensor(sensor_type, coordinator) for sensor_type in SENSOR_TYPES], + False, + ) + + +class MeteoclimaticSensor(CoordinatorEntity, SensorEntity): + """Representation of a Meteoclimatic sensor.""" + + def __init__(self, sensor_type: str, coordinator: DataUpdateCoordinator) -> None: + """Initialize the Meteoclimatic sensor.""" + super().__init__(coordinator) + self._type = sensor_type + station = self.coordinator.data["station"] + self._attr_device_class = SENSOR_TYPES[sensor_type].get(SENSOR_TYPE_CLASS) + self._attr_icon = SENSOR_TYPES[sensor_type].get(SENSOR_TYPE_ICON) + self._attr_name = ( + f"{station.name} {SENSOR_TYPES[sensor_type][SENSOR_TYPE_NAME]}" + ) + self._attr_unique_id = f"{station.code}_{sensor_type}" + self._attr_unit_of_measurement = SENSOR_TYPES[sensor_type].get(SENSOR_TYPE_UNIT) + + @property + def device_info(self): + """Return the device info.""" + return { + "identifiers": {(DOMAIN, self.platform.config_entry.unique_id)}, + "name": self.coordinator.name, + "manufacturer": MANUFACTURER, + "model": MODEL, + "entry_type": "service", + } + + @property + def state(self): + """Return the state of the sensor.""" + return ( + getattr(self.coordinator.data["weather"], self._type) + if self.coordinator.data + else None + ) + + @property + def extra_state_attributes(self): + """Return the state attributes.""" + return {ATTR_ATTRIBUTION: ATTRIBUTION} diff --git a/homeassistant/components/meteoclimatic/weather.py b/homeassistant/components/meteoclimatic/weather.py index 98507eae995..1326d700826 100644 --- a/homeassistant/components/meteoclimatic/weather.py +++ b/homeassistant/components/meteoclimatic/weather.py @@ -11,7 +11,7 @@ from homeassistant.helpers.update_coordinator import ( DataUpdateCoordinator, ) -from .const import ATTRIBUTION, CONDITION_CLASSES, DOMAIN +from .const import ATTRIBUTION, CONDITION_CLASSES, DOMAIN, MANUFACTURER, MODEL def format_condition(condition): @@ -52,6 +52,17 @@ class MeteoclimaticWeather(CoordinatorEntity, WeatherEntity): """Return the unique id of the sensor.""" return self._unique_id + @property + def device_info(self): + """Return the device info.""" + return { + "identifiers": {(DOMAIN, self.platform.config_entry.unique_id)}, + "name": self.coordinator.name, + "manufacturer": MANUFACTURER, + "model": MODEL, + "entry_type": "service", + } + @property def condition(self): """Return the current condition."""