core/homeassistant/components/met/weather.py

213 lines
6.6 KiB
Python
Raw Normal View History

2019-02-14 15:42:03 +00:00
"""Support for Met.no weather service."""
from __future__ import annotations
from types import MappingProxyType
from typing import Any
from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_TIME,
ATTR_WEATHER_HUMIDITY,
ATTR_WEATHER_PRESSURE,
ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_WIND_BEARING,
ATTR_WEATHER_WIND_SPEED,
Forecast,
WeatherEntity,
)
from homeassistant.config_entries import ConfigEntry
2019-02-14 15:42:03 +00:00
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
LENGTH_MILLIMETERS,
PRESSURE_HPA,
SPEED_KILOMETERS_PER_HOUR,
TEMP_CELSIUS,
2019-07-31 19:25:30 +00:00
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
2021-10-25 21:26:40 +00:00
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import MetDataUpdateCoordinator
2022-06-30 17:47:29 +00:00
from .const import ATTR_MAP, CONDITIONS_MAP, CONF_TRACK_HOME, DOMAIN, FORECAST_MAP
2019-07-31 19:25:30 +00:00
ATTRIBUTION = (
"Weather forecast from met.no, delivered by the Norwegian "
"Meteorological Institute."
)
DEFAULT_NAME = "Met.no"
2019-07-31 19:25:30 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add a weather entity from a config_entry."""
coordinator: MetDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +00:00
async_add_entities(
[
MetWeather(
coordinator, config_entry.data, hass.config.units.is_metric, False
),
MetWeather(
coordinator, config_entry.data, hass.config.units.is_metric, True
),
]
Add DataUpdateCoordinator to met integration (#38405) * Add DataUpdateCoordinator to met integration * isort * redundant fetch_data * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/met/weather.py Co-authored-by: Chris Talkington <chris@talkingtontech.com> * fix black, isort, flake8, hassfest, mypy * remove unused async_setup method * replace fetch_data by coordinator request_refresh * remove redundant async_update * track_home * Apply suggestions from code review * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update homeassistant/components/met/__init__.py * Apply suggestions from code review * Update test_config_flow.py * Apply suggestions from code review * Apply suggestions from code review * Update __init__.py * Create test_init.py * Update homeassistant/components/met/__init__.py * Update __init__.py * Update __init__.py * Update homeassistant/components/met/__init__.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
2020-08-09 18:18:02 +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
class MetWeather(CoordinatorEntity[MetDataUpdateCoordinator], WeatherEntity):
"""Implementation of a Met.no weather condition."""
_attr_has_entity_name = True
2022-06-30 17:47:29 +00:00
_attr_native_temperature_unit = TEMP_CELSIUS
_attr_native_precipitation_unit = LENGTH_MILLIMETERS
_attr_native_pressure_unit = PRESSURE_HPA
_attr_native_wind_speed_unit = SPEED_KILOMETERS_PER_HOUR
def __init__(
self,
coordinator: MetDataUpdateCoordinator,
config: MappingProxyType[str, Any],
is_metric: bool,
hourly: bool,
) -> None:
"""Initialise the platform with a data instance and site."""
super().__init__(coordinator)
self._config = config
self._is_metric = is_metric
self._hourly = hourly
@property
def track_home(self) -> (Any | bool):
"""Return if we are tracking home."""
return self._config.get(CONF_TRACK_HOME, False)
@property
def unique_id(self) -> str:
"""Return unique ID."""
name_appendix = ""
if self._hourly:
name_appendix = "-hourly"
if self.track_home:
return f"home{name_appendix}"
return f"{self._config[CONF_LATITUDE]}-{self._config[CONF_LONGITUDE]}{name_appendix}"
@property
def name(self) -> str:
"""Return the name of the sensor."""
name = self._config.get(CONF_NAME)
name_appendix = ""
if self._hourly:
name_appendix = " hourly"
if name is not None:
return f"{name}{name_appendix}"
if self.track_home:
return f"{self.hass.config.location_name}{name_appendix}"
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
@property
def condition(self) -> str | None:
"""Return the current condition."""
condition = self.coordinator.data.current_weather_data.get("condition")
if condition is None:
return None
return format_condition(condition)
@property
2022-06-30 17:47:29 +00:00
def native_temperature(self) -> float | None:
"""Return the temperature."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_TEMPERATURE]
)
@property
2022-06-30 17:47:29 +00:00
def native_pressure(self) -> float | None:
"""Return the pressure."""
2022-06-30 17:47:29 +00:00
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_PRESSURE]
)
@property
def humidity(self) -> float | None:
"""Return the humidity."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_HUMIDITY]
)
@property
2022-06-30 17:47:29 +00:00
def native_wind_speed(self) -> float | None:
"""Return the wind speed."""
2022-06-30 17:47:29 +00:00
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_WIND_SPEED]
)
@property
def wind_bearing(self) -> float | str | None:
"""Return the wind direction."""
return self.coordinator.data.current_weather_data.get(
ATTR_MAP[ATTR_WEATHER_WIND_BEARING]
)
@property
def attribution(self) -> str:
"""Return the attribution."""
return ATTRIBUTION
@property
def forecast(self) -> list[Forecast] | None:
"""Return the forecast array."""
if self._hourly:
met_forecast = self.coordinator.data.hourly_forecast
else:
met_forecast = self.coordinator.data.daily_forecast
2022-06-30 17:47:29 +00:00
required_keys = {"temperature", ATTR_FORECAST_TIME}
ha_forecast: list[Forecast] = []
for met_item in met_forecast:
if not set(met_item).issuperset(required_keys):
continue
ha_item = {
k: met_item[v]
for k, v in FORECAST_MAP.items()
if met_item.get(v) is not None
}
if ha_item.get(ATTR_FORECAST_CONDITION):
ha_item[ATTR_FORECAST_CONDITION] = format_condition(
ha_item[ATTR_FORECAST_CONDITION]
)
ha_forecast.append(ha_item) # type: ignore[arg-type]
return ha_forecast
2020-10-10 15:30:27 +00:00
@property
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=DeviceEntryType.SERVICE,
identifiers={(DOMAIN,)}, # type: ignore[arg-type]
2021-10-25 21:26:40 +00:00
manufacturer="Met.no",
model="Forecast",
configuration_url="https://www.met.no/en",
2021-10-25 21:26:40 +00:00
)