mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Use EntityDescription - zamg (#54942)
This commit is contained in:
parent
1152330865
commit
9fe434ac36
@ -1,16 +1,20 @@
|
|||||||
"""Sensor for the Austrian "Zentralanstalt für Meteorologie und Geodynamik"."""
|
"""Sensor for the Austrian "Zentralanstalt für Meteorologie und Geodynamik"."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import gzip
|
import gzip
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
from typing import Type, Union
|
||||||
|
|
||||||
from aiohttp.hdrs import USER_AGENT
|
from aiohttp.hdrs import USER_AGENT
|
||||||
import requests
|
import requests
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
AREA_SQUARE_METERS,
|
AREA_SQUARE_METERS,
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
@ -43,72 +47,141 @@ DEFAULT_NAME = "zamg"
|
|||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=10)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=10)
|
||||||
VIENNA_TIME_ZONE = dt_util.get_time_zone("Europe/Vienna")
|
VIENNA_TIME_ZONE = dt_util.get_time_zone("Europe/Vienna")
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
DTypeT = Union[Type[int], Type[float], Type[str]]
|
||||||
"pressure": ("Pressure", PRESSURE_HPA, None, "LDstat hPa", float),
|
|
||||||
"pressure_sealevel": (
|
|
||||||
"Pressure at Sea Level",
|
@dataclass
|
||||||
PRESSURE_HPA,
|
class ZamgRequiredKeysMixin:
|
||||||
None,
|
"""Mixin for required keys."""
|
||||||
"LDred hPa",
|
|
||||||
float,
|
col_heading: str
|
||||||
|
dtype: DTypeT
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ZamgSensorEntityDescription(SensorEntityDescription, ZamgRequiredKeysMixin):
|
||||||
|
"""Describes Zamg sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_TYPES: tuple[ZamgSensorEntityDescription, ...] = (
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="pressure",
|
||||||
|
name="Pressure",
|
||||||
|
native_unit_of_measurement=PRESSURE_HPA,
|
||||||
|
col_heading="LDstat hPa",
|
||||||
|
dtype=float,
|
||||||
),
|
),
|
||||||
"humidity": ("Humidity", PERCENTAGE, None, "RF %", int),
|
ZamgSensorEntityDescription(
|
||||||
"wind_speed": (
|
key="pressure_sealevel",
|
||||||
"Wind Speed",
|
name="Pressure at Sea Level",
|
||||||
SPEED_KILOMETERS_PER_HOUR,
|
native_unit_of_measurement=PRESSURE_HPA,
|
||||||
None,
|
col_heading="LDred hPa",
|
||||||
f"WG {SPEED_KILOMETERS_PER_HOUR}",
|
dtype=float,
|
||||||
float,
|
|
||||||
),
|
),
|
||||||
"wind_bearing": ("Wind Bearing", DEGREE, None, f"WR {DEGREE}", int),
|
ZamgSensorEntityDescription(
|
||||||
"wind_max_speed": (
|
key="humidity",
|
||||||
"Top Wind Speed",
|
name="Humidity",
|
||||||
None,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
SPEED_KILOMETERS_PER_HOUR,
|
col_heading="RF %",
|
||||||
f"WSG {SPEED_KILOMETERS_PER_HOUR}",
|
dtype=int,
|
||||||
float,
|
|
||||||
),
|
),
|
||||||
"wind_max_bearing": ("Top Wind Bearing", DEGREE, None, f"WSR {DEGREE}", int),
|
ZamgSensorEntityDescription(
|
||||||
"sun_last_hour": ("Sun Last Hour", PERCENTAGE, None, f"SO {PERCENTAGE}", int),
|
key="wind_speed",
|
||||||
"temperature": (
|
name="Wind Speed",
|
||||||
"Temperature",
|
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
|
||||||
TEMP_CELSIUS,
|
col_heading=f"WG {SPEED_KILOMETERS_PER_HOUR}",
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
dtype=float,
|
||||||
f"T {TEMP_CELSIUS}",
|
|
||||||
float,
|
|
||||||
),
|
),
|
||||||
"precipitation": (
|
ZamgSensorEntityDescription(
|
||||||
"Precipitation",
|
key="wind_bearing",
|
||||||
None,
|
name="Wind Bearing",
|
||||||
f"l/{AREA_SQUARE_METERS}",
|
native_unit_of_measurement=DEGREE,
|
||||||
f"N l/{AREA_SQUARE_METERS}",
|
col_heading=f"WR {DEGREE}",
|
||||||
float,
|
dtype=int,
|
||||||
),
|
),
|
||||||
"dewpoint": (
|
ZamgSensorEntityDescription(
|
||||||
"Dew Point",
|
key="wind_max_speed",
|
||||||
TEMP_CELSIUS,
|
name="Top Wind Speed",
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
|
||||||
f"TP {TEMP_CELSIUS}",
|
col_heading=f"WSG {SPEED_KILOMETERS_PER_HOUR}",
|
||||||
float,
|
dtype=float,
|
||||||
|
),
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="wind_max_bearing",
|
||||||
|
name="Top Wind Bearing",
|
||||||
|
native_unit_of_measurement=DEGREE,
|
||||||
|
col_heading=f"WSR {DEGREE}",
|
||||||
|
dtype=int,
|
||||||
|
),
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="sun_last_hour",
|
||||||
|
name="Sun Last Hour",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
col_heading=f"SO {PERCENTAGE}",
|
||||||
|
dtype=int,
|
||||||
|
),
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="temperature",
|
||||||
|
name="Temperature",
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
col_heading=f"T {TEMP_CELSIUS}",
|
||||||
|
dtype=float,
|
||||||
|
),
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="precipitation",
|
||||||
|
name="Precipitation",
|
||||||
|
native_unit_of_measurement=f"l/{AREA_SQUARE_METERS}",
|
||||||
|
col_heading=f"N l/{AREA_SQUARE_METERS}",
|
||||||
|
dtype=float,
|
||||||
|
),
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="dewpoint",
|
||||||
|
name="Dew Point",
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
col_heading=f"TP {TEMP_CELSIUS}",
|
||||||
|
dtype=float,
|
||||||
),
|
),
|
||||||
# The following probably not useful for general consumption,
|
# The following probably not useful for general consumption,
|
||||||
# but we need them to fill in internal attributes
|
# but we need them to fill in internal attributes
|
||||||
"station_name": ("Station Name", None, None, "Name", str),
|
ZamgSensorEntityDescription(
|
||||||
"station_elevation": (
|
key="station_name",
|
||||||
"Station Elevation",
|
name="Station Name",
|
||||||
LENGTH_METERS,
|
col_heading="Name",
|
||||||
None,
|
dtype=str,
|
||||||
f"Höhe {LENGTH_METERS}",
|
|
||||||
int,
|
|
||||||
),
|
),
|
||||||
"update_date": ("Update Date", None, None, "Datum", str),
|
ZamgSensorEntityDescription(
|
||||||
"update_time": ("Update Time", None, None, "Zeit", str),
|
key="station_elevation",
|
||||||
|
name="Station Elevation",
|
||||||
|
native_unit_of_measurement=LENGTH_METERS,
|
||||||
|
col_heading=f"Höhe {LENGTH_METERS}",
|
||||||
|
dtype=int,
|
||||||
|
),
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="update_date",
|
||||||
|
name="Update Date",
|
||||||
|
col_heading="Datum",
|
||||||
|
dtype=str,
|
||||||
|
),
|
||||||
|
ZamgSensorEntityDescription(
|
||||||
|
key="update_time",
|
||||||
|
name="Update Time",
|
||||||
|
col_heading="Zeit",
|
||||||
|
dtype=str,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
|
API_FIELDS: dict[str, tuple[str, DTypeT]] = {
|
||||||
|
desc.col_heading: (desc.key, desc.dtype) for desc in SENSOR_TYPES
|
||||||
}
|
}
|
||||||
|
|
||||||
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_MONITORED_CONDITIONS, default=["temperature"]): vol.All(
|
vol.Required(CONF_MONITORED_CONDITIONS, default=["temperature"]): vol.All(
|
||||||
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||||
),
|
),
|
||||||
vol.Optional(CONF_STATION_ID): cv.string,
|
vol.Optional(CONF_STATION_ID): cv.string,
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
@ -124,7 +197,7 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the ZAMG sensor platform."""
|
"""Set up the ZAMG sensor platform."""
|
||||||
name = config.get(CONF_NAME)
|
name = config[CONF_NAME]
|
||||||
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
||||||
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
||||||
|
|
||||||
@ -146,10 +219,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
_LOGGER.error("Received error from ZAMG: %s", err)
|
_LOGGER.error("Received error from ZAMG: %s", err)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||||
add_entities(
|
add_entities(
|
||||||
[
|
[
|
||||||
ZamgSensor(probe, variable, name)
|
ZamgSensor(probe, name, description)
|
||||||
for variable in config[CONF_MONITORED_CONDITIONS]
|
for description in SENSOR_TYPES
|
||||||
|
if description.key in monitored_conditions
|
||||||
],
|
],
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
@ -158,27 +233,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class ZamgSensor(SensorEntity):
|
class ZamgSensor(SensorEntity):
|
||||||
"""Implementation of a ZAMG sensor."""
|
"""Implementation of a ZAMG sensor."""
|
||||||
|
|
||||||
def __init__(self, probe, variable, name):
|
entity_description: ZamgSensorEntityDescription
|
||||||
"""Initialize the sensor."""
|
|
||||||
self.probe = probe
|
|
||||||
self.client_name = name
|
|
||||||
self.variable = variable
|
|
||||||
self._attr_device_class = SENSOR_TYPES[variable][2]
|
|
||||||
|
|
||||||
@property
|
def __init__(self, probe, name, description: ZamgSensorEntityDescription):
|
||||||
def name(self):
|
"""Initialize the sensor."""
|
||||||
"""Return the name of the sensor."""
|
self.entity_description = description
|
||||||
return f"{self.client_name} {self.variable}"
|
self.probe = probe
|
||||||
|
self._attr_name = f"{name} {description.key}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.probe.get_data(self.variable)
|
return self.probe.get_data(self.entity_description.key)
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement of this entity, if any."""
|
|
||||||
return SENSOR_TYPES[self.variable][1]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
@ -238,22 +304,12 @@ class ZamgData:
|
|||||||
|
|
||||||
for row in self.current_observations():
|
for row in self.current_observations():
|
||||||
if row.get("Station") == self._station_id:
|
if row.get("Station") == self._station_id:
|
||||||
api_fields = {
|
|
||||||
col_heading: (standard_name, dtype)
|
|
||||||
for standard_name, (
|
|
||||||
_,
|
|
||||||
_,
|
|
||||||
_,
|
|
||||||
col_heading,
|
|
||||||
dtype,
|
|
||||||
) in SENSOR_TYPES.items()
|
|
||||||
}
|
|
||||||
self.data = {
|
self.data = {
|
||||||
api_fields.get(col_heading)[0]: api_fields.get(col_heading)[1](
|
API_FIELDS[col_heading][0]: API_FIELDS[col_heading][1](
|
||||||
v.replace(",", ".")
|
v.replace(",", ".")
|
||||||
)
|
)
|
||||||
for col_heading, v in row.items()
|
for col_heading, v in row.items()
|
||||||
if col_heading in api_fields and v
|
if col_heading in API_FIELDS and v
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user