2022-10-26 16:35:12 +00:00
|
|
|
"""Sensor for zamg the Austrian "Zentralanstalt für Meteorologie und Geodynamik" integration."""
|
2022-01-04 09:52:30 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-01-17 20:58:20 +00:00
|
|
|
from homeassistant.components.weather import WeatherEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-06-28 09:02:13 +00:00
|
|
|
from homeassistant.const import (
|
2022-11-29 17:13:05 +00:00
|
|
|
UnitOfPrecipitationDepth,
|
|
|
|
UnitOfPressure,
|
|
|
|
UnitOfSpeed,
|
|
|
|
UnitOfTemperature,
|
2022-06-28 09:02:13 +00:00
|
|
|
)
|
2022-01-04 09:52:30 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-10-26 16:35:12 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-04 09:52:30 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-10-26 16:35:12 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2017-02-24 21:45:46 +00:00
|
|
|
|
2022-10-26 16:35:12 +00:00
|
|
|
from .const import ATTRIBUTION, CONF_STATION_ID, DOMAIN, MANUFACTURER_URL
|
|
|
|
from .coordinator import ZamgDataUpdateCoordinator
|
2017-02-24 21:45:46 +00:00
|
|
|
|
|
|
|
|
2022-10-26 16:35:12 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up the ZAMG weather platform."""
|
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities(
|
|
|
|
[ZamgWeather(coordinator, entry.title, entry.data[CONF_STATION_ID])]
|
|
|
|
)
|
2017-02-24 21:45:46 +00:00
|
|
|
|
|
|
|
|
2022-10-26 16:35:12 +00:00
|
|
|
class ZamgWeather(CoordinatorEntity, WeatherEntity):
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Representation of a weather condition."""
|
|
|
|
|
2022-12-20 10:47:32 +00:00
|
|
|
_attr_attribution = ATTRIBUTION
|
|
|
|
|
2022-10-26 16:35:12 +00:00
|
|
|
def __init__(
|
2022-12-20 10:47:32 +00:00
|
|
|
self, coordinator: ZamgDataUpdateCoordinator, name: str, station_id: str
|
2022-10-26 16:35:12 +00:00
|
|
|
) -> None:
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Initialise the platform with a data instance and station name."""
|
2022-10-26 16:35:12 +00:00
|
|
|
super().__init__(coordinator)
|
2022-12-20 10:47:32 +00:00
|
|
|
self._attr_unique_id = station_id
|
2022-10-26 16:35:12 +00:00
|
|
|
self._attr_name = f"ZAMG {name}"
|
|
|
|
self.station_id = f"{station_id}"
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
identifiers={(DOMAIN, station_id)},
|
|
|
|
manufacturer=ATTRIBUTION,
|
|
|
|
configuration_url=MANUFACTURER_URL,
|
|
|
|
name=coordinator.name,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-10-26 16:35:12 +00:00
|
|
|
# set units of ZAMG API
|
2022-11-29 17:13:05 +00:00
|
|
|
self._attr_native_temperature_unit = UnitOfTemperature.CELSIUS
|
|
|
|
self._attr_native_pressure_unit = UnitOfPressure.HPA
|
|
|
|
self._attr_native_wind_speed_unit = UnitOfSpeed.METERS_PER_SECOND
|
|
|
|
self._attr_native_precipitation_unit = UnitOfPrecipitationDepth.MILLIMETERS
|
2017-02-24 21:45:46 +00:00
|
|
|
|
|
|
|
@property
|
2022-10-26 16:35:12 +00:00
|
|
|
def condition(self) -> str | None:
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Return the current condition."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2022-10-26 16:35:12 +00:00
|
|
|
def native_temperature(self) -> float | None:
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Return the platform temperature."""
|
2022-10-26 16:35:12 +00:00
|
|
|
try:
|
2022-12-20 10:47:32 +00:00
|
|
|
return float(self.coordinator.data[self.station_id]["TL"]["data"])
|
|
|
|
except (KeyError, ValueError):
|
2022-10-26 16:35:12 +00:00
|
|
|
return None
|
2017-02-24 21:45:46 +00:00
|
|
|
|
|
|
|
@property
|
2022-10-26 16:35:12 +00:00
|
|
|
def native_pressure(self) -> float | None:
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Return the pressure."""
|
2022-10-26 16:35:12 +00:00
|
|
|
try:
|
2022-12-20 10:47:32 +00:00
|
|
|
return float(self.coordinator.data[self.station_id]["P"]["data"])
|
|
|
|
except (KeyError, ValueError):
|
2022-10-26 16:35:12 +00:00
|
|
|
return None
|
2017-02-24 21:45:46 +00:00
|
|
|
|
|
|
|
@property
|
2022-10-26 16:35:12 +00:00
|
|
|
def humidity(self) -> float | None:
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Return the humidity."""
|
2022-10-26 16:35:12 +00:00
|
|
|
try:
|
2022-12-20 10:47:32 +00:00
|
|
|
return float(self.coordinator.data[self.station_id]["RFAM"]["data"])
|
|
|
|
except (KeyError, ValueError):
|
2022-10-26 16:35:12 +00:00
|
|
|
return None
|
2017-02-24 21:45:46 +00:00
|
|
|
|
|
|
|
@property
|
2022-10-26 16:35:12 +00:00
|
|
|
def native_wind_speed(self) -> float | None:
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Return the wind speed."""
|
2022-10-26 16:35:12 +00:00
|
|
|
try:
|
2022-12-20 10:47:32 +00:00
|
|
|
return float(self.coordinator.data[self.station_id]["FFAM"]["data"])
|
|
|
|
except (KeyError, ValueError):
|
2022-10-26 16:35:12 +00:00
|
|
|
return None
|
2017-02-24 21:45:46 +00:00
|
|
|
|
|
|
|
@property
|
2022-10-26 16:35:12 +00:00
|
|
|
def wind_bearing(self) -> float | str | None:
|
2017-02-24 21:45:46 +00:00
|
|
|
"""Return the wind bearing."""
|
2022-10-26 16:35:12 +00:00
|
|
|
try:
|
2022-12-20 10:47:32 +00:00
|
|
|
return self.coordinator.data[self.station_id]["DD"]["data"]
|
|
|
|
except (KeyError, ValueError):
|
2022-10-26 16:35:12 +00:00
|
|
|
return None
|