2019-02-14 15:42:03 +00:00
|
|
|
"""Support for Buienradar.nl weather service."""
|
2017-06-05 06:48:11 +00:00
|
|
|
import logging
|
2018-02-11 17:20:28 +00:00
|
|
|
|
2019-10-18 23:02:54 +00:00
|
|
|
from buienradar.constants import (
|
|
|
|
CONDCODE,
|
|
|
|
CONDITION,
|
|
|
|
DATETIME,
|
|
|
|
MAX_TEMP,
|
|
|
|
MIN_TEMP,
|
|
|
|
RAIN,
|
|
|
|
WINDAZIMUTH,
|
|
|
|
WINDSPEED,
|
|
|
|
)
|
2018-02-11 17:20:28 +00:00
|
|
|
|
2017-06-05 06:48:11 +00:00
|
|
|
from homeassistant.components.weather import (
|
2020-11-20 20:04:03 +00:00
|
|
|
ATTR_CONDITION_CLOUDY,
|
|
|
|
ATTR_CONDITION_EXCEPTIONAL,
|
|
|
|
ATTR_CONDITION_FOG,
|
|
|
|
ATTR_CONDITION_HAIL,
|
|
|
|
ATTR_CONDITION_LIGHTNING,
|
|
|
|
ATTR_CONDITION_LIGHTNING_RAINY,
|
|
|
|
ATTR_CONDITION_PARTLYCLOUDY,
|
|
|
|
ATTR_CONDITION_POURING,
|
|
|
|
ATTR_CONDITION_RAINY,
|
|
|
|
ATTR_CONDITION_SNOWY,
|
|
|
|
ATTR_CONDITION_SNOWY_RAINY,
|
|
|
|
ATTR_CONDITION_SUNNY,
|
|
|
|
ATTR_CONDITION_WINDY,
|
|
|
|
ATTR_CONDITION_WINDY_VARIANT,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_CONDITION,
|
2019-10-18 23:02:54 +00:00
|
|
|
ATTR_FORECAST_PRECIPITATION,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_TEMP,
|
|
|
|
ATTR_FORECAST_TEMP_LOW,
|
|
|
|
ATTR_FORECAST_TIME,
|
|
|
|
ATTR_FORECAST_WIND_BEARING,
|
|
|
|
ATTR_FORECAST_WIND_SPEED,
|
2019-10-18 23:02:54 +00:00
|
|
|
WeatherEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-05-04 11:49:16 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, TEMP_CELSIUS
|
2021-05-04 11:49:16 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2017-06-05 06:48:11 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
# Reuse data and API logic from the sensor implementation
|
2021-05-04 11:49:16 +00:00
|
|
|
from .const import DEFAULT_TIMEFRAME, DOMAIN
|
2019-12-08 13:59:21 +00:00
|
|
|
from .util import BrData
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-06-05 06:48:11 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_FORECAST = "forecast"
|
2017-06-05 06:48:11 +00:00
|
|
|
|
2021-05-04 11:49:16 +00:00
|
|
|
DATA_CONDITION = "buienradar_condition"
|
2017-08-16 06:07:04 +00:00
|
|
|
|
|
|
|
CONDITION_CLASSES = {
|
2021-05-04 11:49:16 +00:00
|
|
|
ATTR_CONDITION_CLOUDY: ("c", "p"),
|
|
|
|
ATTR_CONDITION_FOG: ("d", "n"),
|
|
|
|
ATTR_CONDITION_HAIL: (),
|
|
|
|
ATTR_CONDITION_LIGHTNING: ("g",),
|
|
|
|
ATTR_CONDITION_LIGHTNING_RAINY: ("s",),
|
|
|
|
ATTR_CONDITION_PARTLYCLOUDY: (
|
|
|
|
"b",
|
|
|
|
"j",
|
|
|
|
"o",
|
|
|
|
"r",
|
|
|
|
),
|
|
|
|
ATTR_CONDITION_POURING: ("l", "q"),
|
|
|
|
ATTR_CONDITION_RAINY: ("f", "h", "k", "m"),
|
|
|
|
ATTR_CONDITION_SNOWY: ("u", "i", "v", "t"),
|
|
|
|
ATTR_CONDITION_SNOWY_RAINY: ("w",),
|
|
|
|
ATTR_CONDITION_SUNNY: ("a",),
|
|
|
|
ATTR_CONDITION_WINDY: (),
|
|
|
|
ATTR_CONDITION_WINDY_VARIANT: (),
|
|
|
|
ATTR_CONDITION_EXCEPTIONAL: (),
|
2017-08-16 06:07:04 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 11:49:16 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2017-06-05 06:48:11 +00:00
|
|
|
"""Set up the buienradar platform."""
|
2021-05-04 11:49:16 +00:00
|
|
|
config = entry.data
|
|
|
|
|
2017-06-05 06:48:11 +00:00
|
|
|
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
|
|
|
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
|
|
|
|
|
|
|
if None in (latitude, longitude):
|
|
|
|
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
2021-05-04 11:49:16 +00:00
|
|
|
return
|
2017-06-05 06:48:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
coordinates = {CONF_LATITUDE: float(latitude), CONF_LONGITUDE: float(longitude)}
|
2017-06-05 06:48:11 +00:00
|
|
|
|
|
|
|
# create weather data:
|
2017-06-24 07:12:52 +00:00
|
|
|
data = BrData(hass, coordinates, DEFAULT_TIMEFRAME, None)
|
2017-06-05 06:48:11 +00:00
|
|
|
# create weather device:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Initializing buienradar weather: coordinates %s", coordinates)
|
2017-08-16 06:07:04 +00:00
|
|
|
|
|
|
|
# create condition helper
|
2021-05-04 11:49:16 +00:00
|
|
|
if DATA_CONDITION not in hass.data[DOMAIN]:
|
2017-08-16 06:07:04 +00:00
|
|
|
cond_keys = [str(chr(x)) for x in range(97, 123)]
|
2021-05-04 11:49:16 +00:00
|
|
|
hass.data[DOMAIN][DATA_CONDITION] = dict.fromkeys(cond_keys)
|
2017-08-16 06:07:04 +00:00
|
|
|
for cond, condlst in CONDITION_CLASSES.items():
|
|
|
|
for condi in condlst:
|
2021-05-04 11:49:16 +00:00
|
|
|
hass.data[DOMAIN][DATA_CONDITION][condi] = cond
|
2017-08-16 06:07:04 +00:00
|
|
|
|
2020-08-01 07:13:17 +00:00
|
|
|
async_add_entities([BrWeather(data, config, coordinates)])
|
2017-06-05 06:48:11 +00:00
|
|
|
|
|
|
|
# schedule the first update in 1 minute from now:
|
2018-10-01 06:52:42 +00:00
|
|
|
await data.schedule_update(1)
|
2017-06-05 06:48:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BrWeather(WeatherEntity):
|
|
|
|
"""Representation of a weather condition."""
|
|
|
|
|
2021-07-19 19:22:20 +00:00
|
|
|
_attr_temperature_unit = TEMP_CELSIUS
|
|
|
|
|
2020-08-01 07:13:17 +00:00
|
|
|
def __init__(self, data, config, coordinates):
|
2021-07-19 19:22:20 +00:00
|
|
|
"""Initialize the platform with a data instance and station name."""
|
2021-05-04 11:49:16 +00:00
|
|
|
self._stationname = config.get(CONF_NAME, "Buienradar")
|
2021-07-19 19:22:20 +00:00
|
|
|
self._attr_name = (
|
|
|
|
self._stationname or f"BR {data.stationname or '(unknown station)'}"
|
|
|
|
)
|
2017-06-05 06:48:11 +00:00
|
|
|
self._data = data
|
|
|
|
|
2021-07-19 19:22:20 +00:00
|
|
|
self._attr_unique_id = "{:2.6f}{:2.6f}".format(
|
2020-08-01 07:13:17 +00:00
|
|
|
coordinates[CONF_LATITUDE], coordinates[CONF_LONGITUDE]
|
|
|
|
)
|
|
|
|
|
2017-06-05 06:48:11 +00:00
|
|
|
@property
|
|
|
|
def attribution(self):
|
|
|
|
"""Return the attribution."""
|
|
|
|
return self._data.attribution
|
|
|
|
|
|
|
|
@property
|
|
|
|
def condition(self):
|
2017-08-16 06:07:04 +00:00
|
|
|
"""Return the current condition."""
|
2021-10-20 18:31:00 +00:00
|
|
|
if (
|
|
|
|
self._data
|
|
|
|
and self._data.condition
|
|
|
|
and (ccode := self._data.condition.get(CONDCODE))
|
|
|
|
and (conditions := self.hass.data[DOMAIN].get(DATA_CONDITION))
|
|
|
|
):
|
|
|
|
return conditions.get(ccode)
|
2017-08-16 06:07:04 +00:00
|
|
|
|
2017-06-05 06:48:11 +00:00
|
|
|
@property
|
|
|
|
def temperature(self):
|
2017-08-16 06:07:04 +00:00
|
|
|
"""Return the current temperature."""
|
2017-06-05 06:48:11 +00:00
|
|
|
return self._data.temperature
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pressure(self):
|
2017-08-16 06:07:04 +00:00
|
|
|
"""Return the current pressure."""
|
2017-06-05 06:48:11 +00:00
|
|
|
return self._data.pressure
|
|
|
|
|
|
|
|
@property
|
|
|
|
def humidity(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._data.humidity
|
|
|
|
|
2017-08-16 06:07:04 +00:00
|
|
|
@property
|
|
|
|
def visibility(self):
|
2019-07-28 20:08:20 +00:00
|
|
|
"""Return the current visibility in km."""
|
|
|
|
if self._data.visibility is None:
|
|
|
|
return None
|
|
|
|
return round(self._data.visibility / 1000, 1)
|
2017-08-16 06:07:04 +00:00
|
|
|
|
2017-06-05 06:48:11 +00:00
|
|
|
@property
|
|
|
|
def wind_speed(self):
|
2019-07-28 20:08:20 +00:00
|
|
|
"""Return the current windspeed in km/h."""
|
|
|
|
if self._data.wind_speed is None:
|
|
|
|
return None
|
|
|
|
return round(self._data.wind_speed * 3.6, 1)
|
2017-06-05 06:48:11 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_bearing(self):
|
2017-08-16 06:07:04 +00:00
|
|
|
"""Return the current wind bearing (degrees)."""
|
2017-06-05 06:48:11 +00:00
|
|
|
return self._data.wind_bearing
|
|
|
|
|
|
|
|
@property
|
|
|
|
def forecast(self):
|
2017-08-16 06:07:04 +00:00
|
|
|
"""Return the forecast array."""
|
2019-07-28 20:08:20 +00:00
|
|
|
fcdata_out = []
|
2021-05-04 11:49:16 +00:00
|
|
|
cond = self.hass.data[DOMAIN][DATA_CONDITION]
|
2019-07-28 20:08:20 +00:00
|
|
|
|
|
|
|
if not self._data.forecast:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for data_in in self._data.forecast:
|
|
|
|
# remap keys from external library to
|
|
|
|
# keys understood by the weather component:
|
|
|
|
condcode = data_in.get(CONDITION, []).get(CONDCODE)
|
|
|
|
data_out = {
|
2021-03-23 19:19:47 +00:00
|
|
|
ATTR_FORECAST_TIME: data_in.get(DATETIME).isoformat(),
|
2019-07-28 20:08:20 +00:00
|
|
|
ATTR_FORECAST_CONDITION: cond[condcode],
|
|
|
|
ATTR_FORECAST_TEMP_LOW: data_in.get(MIN_TEMP),
|
|
|
|
ATTR_FORECAST_TEMP: data_in.get(MAX_TEMP),
|
|
|
|
ATTR_FORECAST_PRECIPITATION: data_in.get(RAIN),
|
|
|
|
ATTR_FORECAST_WIND_BEARING: data_in.get(WINDAZIMUTH),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_WIND_SPEED: round(data_in.get(WINDSPEED) * 3.6, 1),
|
2019-07-28 20:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fcdata_out.append(data_out)
|
|
|
|
|
|
|
|
return fcdata_out
|