1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00
Files
core/homeassistant/components/meteoclimatic/sensor.py
T
2026-03-03 17:57:54 +01:00

165 lines
5.3 KiB
Python

"""Support for Meteoclimatic sensor."""
from typing import TYPE_CHECKING
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import (
DEGREE,
PERCENTAGE,
UnitOfPrecipitationDepth,
UnitOfPressure,
UnitOfSpeed,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ATTRIBUTION, DOMAIN, MANUFACTURER, MODEL
from .coordinator import MeteoclimaticConfigEntry, MeteoclimaticUpdateCoordinator
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="temp_current",
name="Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="temp_max",
name="Daily Max Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
),
SensorEntityDescription(
key="temp_min",
name="Daily Min Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE,
),
SensorEntityDescription(
key="humidity_current",
name="Humidity",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.HUMIDITY,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="humidity_max",
name="Daily Max Humidity",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.HUMIDITY,
),
SensorEntityDescription(
key="humidity_min",
name="Daily Min Humidity",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.HUMIDITY,
),
SensorEntityDescription(
key="pressure_current",
name="Pressure",
native_unit_of_measurement=UnitOfPressure.HPA,
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="pressure_max",
name="Daily Max Pressure",
native_unit_of_measurement=UnitOfPressure.HPA,
device_class=SensorDeviceClass.PRESSURE,
),
SensorEntityDescription(
key="pressure_min",
name="Daily Min Pressure",
native_unit_of_measurement=UnitOfPressure.HPA,
device_class=SensorDeviceClass.PRESSURE,
),
SensorEntityDescription(
key="wind_current",
name="Wind Speed",
native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR,
device_class=SensorDeviceClass.WIND_SPEED,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="wind_max",
name="Daily Max Wind Speed",
native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR,
device_class=SensorDeviceClass.WIND_SPEED,
),
SensorEntityDescription(
key="wind_bearing",
name="Wind Bearing",
native_unit_of_measurement=DEGREE,
icon="mdi:weather-windy",
device_class=SensorDeviceClass.WIND_DIRECTION,
state_class=SensorStateClass.MEASUREMENT_ANGLE,
),
SensorEntityDescription(
key="rain",
name="Daily Precipitation",
native_unit_of_measurement=UnitOfPrecipitationDepth.MILLIMETERS,
device_class=SensorDeviceClass.PRECIPITATION,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: MeteoclimaticConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the Meteoclimatic sensor platform."""
coordinator = entry.runtime_data
async_add_entities(
[MeteoclimaticSensor(coordinator, description) for description in SENSOR_TYPES],
False,
)
class MeteoclimaticSensor(
CoordinatorEntity[MeteoclimaticUpdateCoordinator], SensorEntity
):
"""Representation of a Meteoclimatic sensor."""
_attr_attribution = ATTRIBUTION
def __init__(
self,
coordinator: MeteoclimaticUpdateCoordinator,
description: SensorEntityDescription,
) -> None:
"""Initialize the Meteoclimatic sensor."""
super().__init__(coordinator)
self.entity_description = description
station = coordinator.data.station
self._attr_name = f"{station.name} {description.name}"
self._attr_unique_id = f"{station.code}_{description.key}"
if TYPE_CHECKING:
assert coordinator.config_entry.unique_id is not None
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, coordinator.config_entry.unique_id)},
manufacturer=MANUFACTURER,
model=MODEL,
name=coordinator.name,
)
@property
def native_value(self) -> float | None:
"""Return the state of the sensor."""
return (
getattr(self.coordinator.data.weather, self.entity_description.key)
if self.coordinator.data
else None
)