Add sensor platform to Meteoclimatic integration (#51467)

* Add meteoclimatic sensor platform

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>

* Add sensor.py to coverage file

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>

* Add explicit return type None

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>

* Fix sample station code

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>

* Apply frenck suggestions

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>

* Remove extra attributes

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>

* Revert translations

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>

* Remove None icons and classes

Signed-off-by: Adrian Moreno <adrian@morenomartinez.com>
This commit is contained in:
Adrián Moreno 2021-06-29 13:52:39 +02:00 committed by GitHub
parent 198b664409
commit a639cb7ba7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 12 deletions

View file

@ -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

View file

@ -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,
)

View file

@ -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",
},
}

View file

@ -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}

View file

@ -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."""