2019-02-14 15:42:03 +00:00
|
|
|
"""Support for Met.no weather service."""
|
2021-11-01 18:37:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-09-20 08:32:14 +00:00
|
|
|
import logging
|
2021-11-01 18:37:03 +00:00
|
|
|
from types import MappingProxyType
|
|
|
|
from typing import Any
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-09-02 12:11:13 +00:00
|
|
|
from homeassistant.components.weather import (
|
|
|
|
ATTR_FORECAST_CONDITION,
|
2020-09-21 19:56:08 +00:00
|
|
|
ATTR_FORECAST_TEMP,
|
|
|
|
ATTR_FORECAST_TIME,
|
2020-09-02 12:11:13 +00:00
|
|
|
ATTR_WEATHER_HUMIDITY,
|
|
|
|
ATTR_WEATHER_PRESSURE,
|
|
|
|
ATTR_WEATHER_TEMPERATURE,
|
|
|
|
ATTR_WEATHER_WIND_BEARING,
|
|
|
|
ATTR_WEATHER_WIND_SPEED,
|
|
|
|
PLATFORM_SCHEMA,
|
2021-11-01 18:37:03 +00:00
|
|
|
Forecast,
|
2020-09-02 12:11:13 +00:00
|
|
|
WeatherEntity,
|
|
|
|
)
|
2021-11-01 18:37:03 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2019-02-14 15:42:03 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ELEVATION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_NAME,
|
2020-12-11 15:53:21 +00:00
|
|
|
LENGTH_INCHES,
|
|
|
|
LENGTH_MILLIMETERS,
|
2020-03-16 10:04:47 +00:00
|
|
|
PRESSURE_HPA,
|
|
|
|
PRESSURE_INHG,
|
2021-11-09 07:12:28 +00:00
|
|
|
SPEED_KILOMETERS_PER_HOUR,
|
|
|
|
SPEED_MILES_PER_HOUR,
|
2019-12-09 10:43:00 +00:00
|
|
|
TEMP_CELSIUS,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-11-01 18:37:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-09-20 08:32:14 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2021-10-25 21:26:40 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-11-01 18:37:03 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
T,
|
|
|
|
)
|
2020-03-16 10:04:47 +00:00
|
|
|
from homeassistant.util.distance import convert as convert_distance
|
|
|
|
from homeassistant.util.pressure import convert as convert_pressure
|
2021-11-09 07:12:28 +00:00
|
|
|
from homeassistant.util.speed import convert as convert_speed
|
2018-09-20 08:32:14 +00:00
|
|
|
|
2020-12-11 15:53:21 +00:00
|
|
|
from .const import (
|
|
|
|
ATTR_FORECAST_PRECIPITATION,
|
|
|
|
ATTR_MAP,
|
|
|
|
CONDITIONS_MAP,
|
|
|
|
CONF_TRACK_HOME,
|
|
|
|
DOMAIN,
|
|
|
|
FORECAST_MAP,
|
|
|
|
)
|
2019-06-19 21:41:27 +00:00
|
|
|
|
2018-09-20 08:32:14 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTRIBUTION = (
|
|
|
|
"Weather forecast from met.no, delivered by the Norwegian "
|
|
|
|
"Meteorological Institute."
|
|
|
|
)
|
2018-09-20 08:32:14 +00:00
|
|
|
DEFAULT_NAME = "Met.no"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Inclusive(
|
|
|
|
CONF_LATITUDE, "coordinates", "Latitude and longitude must exist together"
|
|
|
|
): cv.latitude,
|
|
|
|
vol.Inclusive(
|
|
|
|
CONF_LONGITUDE, "coordinates", "Latitude and longitude must exist together"
|
|
|
|
): cv.longitude,
|
|
|
|
vol.Optional(CONF_ELEVATION): int,
|
|
|
|
}
|
|
|
|
)
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
|
2021-11-01 18:37:03 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Set up the Met.no weather platform."""
|
2019-06-18 23:44:41 +00:00
|
|
|
_LOGGER.warning("Loading Met.no via platform config is deprecated")
|
|
|
|
|
2019-06-19 21:41:27 +00:00
|
|
|
# Add defaults.
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {CONF_ELEVATION: hass.config.elevation, **config}
|
2018-09-20 08:32:14 +00:00
|
|
|
|
2019-06-19 21:41:27 +00:00
|
|
|
if config.get(CONF_LATITUDE) is None:
|
|
|
|
config[CONF_TRACK_HOME] = True
|
2018-09-20 08:32:14 +00:00
|
|
|
|
2020-08-09 18:18:02 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
|
|
|
)
|
|
|
|
)
|
2019-06-18 23:44:41 +00:00
|
|
|
|
|
|
|
|
2021-11-01 18:37:03 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-06-18 23:44:41 +00:00
|
|
|
"""Add a weather entity from a config_entry."""
|
2020-08-09 18:18:02 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
2020-08-11 01:22:39 +00:00
|
|
|
[
|
|
|
|
MetWeather(
|
|
|
|
coordinator, config_entry.data, hass.config.units.is_metric, False
|
|
|
|
),
|
|
|
|
MetWeather(
|
|
|
|
coordinator, config_entry.data, hass.config.units.is_metric, True
|
|
|
|
),
|
|
|
|
]
|
2020-08-09 18:18:02 +00:00
|
|
|
)
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
|
2020-09-02 12:11:13 +00:00
|
|
|
def format_condition(condition: str) -> str:
|
|
|
|
"""Return condition from dict CONDITIONS_MAP."""
|
|
|
|
for key, value in CONDITIONS_MAP.items():
|
|
|
|
if condition in value:
|
|
|
|
return key
|
|
|
|
return condition
|
|
|
|
|
|
|
|
|
2020-08-30 18:14:09 +00:00
|
|
|
class MetWeather(CoordinatorEntity, WeatherEntity):
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Implementation of a Met.no weather condition."""
|
|
|
|
|
2021-11-01 18:37:03 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator[T],
|
|
|
|
config: MappingProxyType[str, Any],
|
|
|
|
is_metric: bool,
|
|
|
|
hourly: bool,
|
|
|
|
) -> None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Initialise the platform with a data instance and site."""
|
2020-08-30 18:14:09 +00:00
|
|
|
super().__init__(coordinator)
|
2019-06-19 21:41:27 +00:00
|
|
|
self._config = config
|
2020-03-16 10:04:47 +00:00
|
|
|
self._is_metric = is_metric
|
2020-08-11 01:22:39 +00:00
|
|
|
self._hourly = hourly
|
2018-09-20 08:32:14 +00:00
|
|
|
|
2019-06-19 21:41:27 +00:00
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def track_home(self) -> (Any | bool):
|
2019-06-19 21:41:27 +00:00
|
|
|
"""Return if we are tracking home."""
|
|
|
|
return self._config.get(CONF_TRACK_HOME, False)
|
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def unique_id(self) -> str:
|
2019-06-19 21:41:27 +00:00
|
|
|
"""Return unique ID."""
|
2020-09-10 05:58:40 +00:00
|
|
|
name_appendix = ""
|
|
|
|
if self._hourly:
|
|
|
|
name_appendix = "-hourly"
|
2019-06-19 21:41:27 +00:00
|
|
|
if self.track_home:
|
2020-09-10 05:58:40 +00:00
|
|
|
return f"home{name_appendix}"
|
2019-06-19 21:41:27 +00:00
|
|
|
|
2020-09-10 05:58:40 +00:00
|
|
|
return f"{self._config[CONF_LATITUDE]}-{self._config[CONF_LONGITUDE]}{name_appendix}"
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def name(self) -> str:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the name of the sensor."""
|
2019-06-19 21:41:27 +00:00
|
|
|
name = self._config.get(CONF_NAME)
|
2020-09-10 05:58:40 +00:00
|
|
|
name_appendix = ""
|
|
|
|
if self._hourly:
|
|
|
|
name_appendix = " Hourly"
|
2019-06-19 21:41:27 +00:00
|
|
|
|
|
|
|
if name is not None:
|
2020-09-10 05:58:40 +00:00
|
|
|
return f"{name}{name_appendix}"
|
2019-06-19 21:41:27 +00:00
|
|
|
|
|
|
|
if self.track_home:
|
2020-09-10 05:58:40 +00:00
|
|
|
return f"{self.hass.config.location_name}{name_appendix}"
|
2019-06-19 21:41:27 +00:00
|
|
|
|
2020-09-10 05:58:40 +00:00
|
|
|
return f"{DEFAULT_NAME}{name_appendix}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self) -> bool:
|
|
|
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
|
|
|
return not self._hourly
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def condition(self) -> str | None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the current condition."""
|
2020-09-02 12:11:13 +00:00
|
|
|
condition = self.coordinator.data.current_weather_data.get("condition")
|
|
|
|
return format_condition(condition)
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def temperature(self) -> float | None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the temperature."""
|
2020-09-02 12:11:13 +00:00
|
|
|
return self.coordinator.data.current_weather_data.get(
|
|
|
|
ATTR_MAP[ATTR_WEATHER_TEMPERATURE]
|
|
|
|
)
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def temperature_unit(self) -> str:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return TEMP_CELSIUS
|
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def pressure(self) -> float | None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the pressure."""
|
2020-09-02 12:11:13 +00:00
|
|
|
pressure_hpa = self.coordinator.data.current_weather_data.get(
|
|
|
|
ATTR_MAP[ATTR_WEATHER_PRESSURE]
|
|
|
|
)
|
2020-03-16 10:04:47 +00:00
|
|
|
if self._is_metric or pressure_hpa is None:
|
|
|
|
return pressure_hpa
|
|
|
|
|
|
|
|
return round(convert_pressure(pressure_hpa, PRESSURE_HPA, PRESSURE_INHG), 2)
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def humidity(self) -> float | None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the humidity."""
|
2020-09-02 12:11:13 +00:00
|
|
|
return self.coordinator.data.current_weather_data.get(
|
|
|
|
ATTR_MAP[ATTR_WEATHER_HUMIDITY]
|
|
|
|
)
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def wind_speed(self) -> float | None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the wind speed."""
|
2020-09-02 12:11:13 +00:00
|
|
|
speed_km_h = self.coordinator.data.current_weather_data.get(
|
|
|
|
ATTR_MAP[ATTR_WEATHER_WIND_SPEED]
|
|
|
|
)
|
|
|
|
if self._is_metric or speed_km_h is None:
|
|
|
|
return speed_km_h
|
2020-03-16 10:04:47 +00:00
|
|
|
|
2021-11-09 07:12:28 +00:00
|
|
|
speed_mi_h = convert_speed(
|
|
|
|
speed_km_h, SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR
|
|
|
|
)
|
2020-03-16 10:04:47 +00:00
|
|
|
return int(round(speed_mi_h))
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def wind_bearing(self) -> float | str | None:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the wind direction."""
|
2020-09-02 12:11:13 +00:00
|
|
|
return self.coordinator.data.current_weather_data.get(
|
|
|
|
ATTR_MAP[ATTR_WEATHER_WIND_BEARING]
|
|
|
|
)
|
2018-09-20 08:32:14 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def attribution(self) -> str:
|
2018-09-20 08:32:14 +00:00
|
|
|
"""Return the attribution."""
|
2019-02-14 21:09:22 +00:00
|
|
|
return ATTRIBUTION
|
2018-10-07 19:00:12 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def forecast(self) -> list[Forecast] | None:
|
2018-10-07 19:00:12 +00:00
|
|
|
"""Return the forecast array."""
|
2020-08-11 01:22:39 +00:00
|
|
|
if self._hourly:
|
2020-09-02 12:11:13 +00:00
|
|
|
met_forecast = self.coordinator.data.hourly_forecast
|
|
|
|
else:
|
|
|
|
met_forecast = self.coordinator.data.daily_forecast
|
2020-09-21 19:56:08 +00:00
|
|
|
required_keys = {ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME}
|
2021-11-01 18:37:03 +00:00
|
|
|
ha_forecast: list[Forecast] = []
|
2020-09-02 12:11:13 +00:00
|
|
|
for met_item in met_forecast:
|
2020-09-21 19:56:08 +00:00
|
|
|
if not set(met_item).issuperset(required_keys):
|
|
|
|
continue
|
2020-09-02 12:11:13 +00:00
|
|
|
ha_item = {
|
2020-11-27 07:07:49 +00:00
|
|
|
k: met_item[v]
|
|
|
|
for k, v in FORECAST_MAP.items()
|
|
|
|
if met_item.get(v) is not None
|
2020-09-02 12:11:13 +00:00
|
|
|
}
|
2021-03-27 09:03:15 +00:00
|
|
|
if not self._is_metric and ATTR_FORECAST_PRECIPITATION in ha_item:
|
2021-11-01 18:37:03 +00:00
|
|
|
if ha_item[ATTR_FORECAST_PRECIPITATION] is not None:
|
|
|
|
precip_inches = convert_distance(
|
|
|
|
ha_item[ATTR_FORECAST_PRECIPITATION],
|
|
|
|
LENGTH_MILLIMETERS,
|
|
|
|
LENGTH_INCHES,
|
|
|
|
)
|
2021-03-27 09:03:15 +00:00
|
|
|
ha_item[ATTR_FORECAST_PRECIPITATION] = round(precip_inches, 2)
|
2020-09-20 21:56:04 +00:00
|
|
|
if ha_item.get(ATTR_FORECAST_CONDITION):
|
|
|
|
ha_item[ATTR_FORECAST_CONDITION] = format_condition(
|
|
|
|
ha_item[ATTR_FORECAST_CONDITION]
|
|
|
|
)
|
2021-11-01 18:37:03 +00:00
|
|
|
ha_forecast.append(ha_item) # type: ignore[arg-type]
|
2020-09-02 12:11:13 +00:00
|
|
|
return ha_forecast
|
2020-10-10 15:30:27 +00:00
|
|
|
|
|
|
|
@property
|
2021-11-01 18:37:03 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-10-10 15:30:27 +00:00
|
|
|
"""Device info."""
|
2021-10-25 21:26:40 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
default_name="Forecast",
|
|
|
|
entry_type="service",
|
2021-11-01 18:37:03 +00:00
|
|
|
identifiers={(DOMAIN,)}, # type: ignore[arg-type]
|
2021-10-25 21:26:40 +00:00
|
|
|
manufacturer="Met.no",
|
|
|
|
model="Forecast",
|
|
|
|
)
|