2020-02-20 23:29:46 +00:00
|
|
|
"""Platform for retrieving meteorological data from Environment Canada."""
|
2021-10-26 21:23:43 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-06-06 18:47:27 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from homeassistant.components.weather import (
|
2020-11-20 20:04:03 +00:00
|
|
|
ATTR_CONDITION_CLEAR_NIGHT,
|
|
|
|
ATTR_CONDITION_CLOUDY,
|
|
|
|
ATTR_CONDITION_FOG,
|
|
|
|
ATTR_CONDITION_HAIL,
|
|
|
|
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,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_CONDITION,
|
2022-06-28 09:01:14 +00:00
|
|
|
ATTR_FORECAST_NATIVE_TEMP,
|
|
|
|
ATTR_FORECAST_NATIVE_TEMP_LOW,
|
2020-06-17 05:39:33 +00:00
|
|
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_TIME,
|
2023-08-16 20:10:48 +00:00
|
|
|
DOMAIN as WEATHER_DOMAIN,
|
|
|
|
Forecast,
|
2023-08-24 09:28:20 +00:00
|
|
|
SingleCoordinatorWeatherEntity,
|
2023-08-16 20:10:48 +00:00
|
|
|
WeatherEntityFeature,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-03 15:27:33 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-06-28 09:01:14 +00:00
|
|
|
from homeassistant.const import (
|
2022-11-29 17:13:05 +00:00
|
|
|
UnitOfLength,
|
|
|
|
UnitOfPressure,
|
|
|
|
UnitOfSpeed,
|
|
|
|
UnitOfTemperature,
|
2022-06-28 09:01:14 +00:00
|
|
|
)
|
2023-08-24 09:28:20 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2023-08-16 20:10:48 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2022-01-03 15:27:33 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-05-29 21:00:11 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2022-07-12 18:45:38 +00:00
|
|
|
from . import device_info
|
2021-12-19 12:40:39 +00:00
|
|
|
from .const import DOMAIN
|
2021-10-11 15:33:29 +00:00
|
|
|
|
2019-06-06 18:47:27 +00:00
|
|
|
# Icon codes from http://dd.weatheroffice.ec.gc.ca/citypage_weather/
|
|
|
|
# docs/current_conditions_icon_code_descriptions_e.csv
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON_CONDITION_MAP = {
|
2020-11-20 20:04:03 +00:00
|
|
|
ATTR_CONDITION_SUNNY: [0, 1],
|
|
|
|
ATTR_CONDITION_CLEAR_NIGHT: [30, 31],
|
|
|
|
ATTR_CONDITION_PARTLYCLOUDY: [2, 3, 4, 5, 22, 32, 33, 34, 35],
|
|
|
|
ATTR_CONDITION_CLOUDY: [10],
|
|
|
|
ATTR_CONDITION_RAINY: [6, 9, 11, 12, 28, 36],
|
|
|
|
ATTR_CONDITION_LIGHTNING_RAINY: [19, 39, 46, 47],
|
|
|
|
ATTR_CONDITION_POURING: [13],
|
|
|
|
ATTR_CONDITION_SNOWY_RAINY: [7, 14, 15, 27, 37],
|
|
|
|
ATTR_CONDITION_SNOWY: [8, 16, 17, 18, 25, 26, 38, 40],
|
|
|
|
ATTR_CONDITION_WINDY: [43],
|
|
|
|
ATTR_CONDITION_FOG: [20, 21, 23, 24, 44],
|
|
|
|
ATTR_CONDITION_HAIL: [26, 27],
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
2022-01-03 15:27:33 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-10-11 15:33:29 +00:00
|
|
|
"""Add a weather entity from a config_entry."""
|
2021-10-26 21:23:43 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]["weather_coordinator"]
|
2023-08-16 20:10:48 +00:00
|
|
|
entity_registry = er.async_get(hass)
|
|
|
|
|
|
|
|
entities = [ECWeather(coordinator, False)]
|
|
|
|
|
|
|
|
# Add hourly entity to legacy config entries
|
|
|
|
if entity_registry.async_get_entity_id(
|
|
|
|
WEATHER_DOMAIN,
|
|
|
|
DOMAIN,
|
|
|
|
_calculate_unique_id(config_entry.unique_id, True),
|
|
|
|
):
|
|
|
|
entities.append(ECWeather(coordinator, True))
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
def _calculate_unique_id(config_entry_unique_id: str | None, hourly: bool) -> str:
|
|
|
|
"""Calculate unique ID."""
|
|
|
|
return f"{config_entry_unique_id}{'-hourly' if hourly else '-daily'}"
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
2023-08-24 09:28:20 +00:00
|
|
|
class ECWeather(SingleCoordinatorWeatherEntity):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Representation of a weather condition."""
|
|
|
|
|
2022-07-12 18:45:38 +00:00
|
|
|
_attr_has_entity_name = True
|
2022-11-29 17:13:05 +00:00
|
|
|
_attr_native_pressure_unit = UnitOfPressure.KPA
|
|
|
|
_attr_native_temperature_unit = UnitOfTemperature.CELSIUS
|
|
|
|
_attr_native_visibility_unit = UnitOfLength.KILOMETERS
|
|
|
|
_attr_native_wind_speed_unit = UnitOfSpeed.KILOMETERS_PER_HOUR
|
2023-08-16 20:10:48 +00:00
|
|
|
_attr_supported_features = (
|
|
|
|
WeatherEntityFeature.FORECAST_DAILY | WeatherEntityFeature.FORECAST_HOURLY
|
|
|
|
)
|
2022-06-28 09:01:14 +00:00
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
def __init__(self, coordinator, hourly):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Initialize Environment Canada weather."""
|
2021-10-26 21:23:43 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
self.ec_data = coordinator.ec_data
|
|
|
|
self._attr_attribution = self.ec_data.metadata["attribution"]
|
2023-06-26 20:23:43 +00:00
|
|
|
self._attr_translation_key = "hourly_forecast" if hourly else "forecast"
|
2023-08-16 20:10:48 +00:00
|
|
|
self._attr_unique_id = _calculate_unique_id(
|
|
|
|
coordinator.config_entry.unique_id, hourly
|
2021-10-26 21:23:43 +00:00
|
|
|
)
|
2022-01-07 17:28:13 +00:00
|
|
|
self._attr_entity_registry_enabled_default = not hourly
|
2021-10-26 21:23:43 +00:00
|
|
|
self._hourly = hourly
|
2022-07-12 18:45:38 +00:00
|
|
|
self._attr_device_info = device_info(coordinator.config_entry)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
@property
|
2022-06-28 09:01:14 +00:00
|
|
|
def native_temperature(self):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return the temperature."""
|
2022-04-02 07:44:40 +00:00
|
|
|
if (
|
|
|
|
temperature := self.ec_data.conditions.get("temperature", {}).get("value")
|
|
|
|
) is not None:
|
|
|
|
return float(temperature)
|
|
|
|
if (
|
|
|
|
self.ec_data.hourly_forecasts
|
|
|
|
and (temperature := self.ec_data.hourly_forecasts[0].get("temperature"))
|
|
|
|
is not None
|
2021-10-11 15:33:29 +00:00
|
|
|
):
|
2022-04-02 07:44:40 +00:00
|
|
|
return float(temperature)
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def humidity(self):
|
|
|
|
"""Return the humidity."""
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("humidity", {}).get("value"):
|
2019-07-31 19:25:30 +00:00
|
|
|
return float(self.ec_data.conditions["humidity"]["value"])
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2022-06-28 09:01:14 +00:00
|
|
|
def native_wind_speed(self):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return the wind speed."""
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("wind_speed", {}).get("value"):
|
2019-07-31 19:25:30 +00:00
|
|
|
return float(self.ec_data.conditions["wind_speed"]["value"])
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def wind_bearing(self):
|
|
|
|
"""Return the wind bearing."""
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("wind_bearing", {}).get("value"):
|
2019-07-31 19:25:30 +00:00
|
|
|
return float(self.ec_data.conditions["wind_bearing"]["value"])
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2022-06-28 09:01:14 +00:00
|
|
|
def native_pressure(self):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return the pressure."""
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("pressure", {}).get("value"):
|
2022-06-28 09:01:14 +00:00
|
|
|
return float(self.ec_data.conditions["pressure"]["value"])
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2022-06-28 09:01:14 +00:00
|
|
|
def native_visibility(self):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return the visibility."""
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("visibility", {}).get("value"):
|
2019-07-31 19:25:30 +00:00
|
|
|
return float(self.ec_data.conditions["visibility"]["value"])
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def condition(self):
|
|
|
|
"""Return the weather condition."""
|
2019-07-13 16:14:29 +00:00
|
|
|
icon_code = None
|
|
|
|
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("icon_code", {}).get("value"):
|
2019-07-31 19:25:30 +00:00
|
|
|
icon_code = self.ec_data.conditions["icon_code"]["value"]
|
2021-10-11 15:33:29 +00:00
|
|
|
elif self.ec_data.hourly_forecasts and self.ec_data.hourly_forecasts[0].get(
|
|
|
|
"icon_code"
|
|
|
|
):
|
2019-07-31 19:25:30 +00:00
|
|
|
icon_code = self.ec_data.hourly_forecasts[0]["icon_code"]
|
2019-07-13 16:14:29 +00:00
|
|
|
|
2019-06-06 18:47:27 +00:00
|
|
|
if icon_code:
|
|
|
|
return icon_code_to_condition(int(icon_code))
|
2019-07-31 19:25:30 +00:00
|
|
|
return ""
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
@property
|
2023-08-16 20:10:48 +00:00
|
|
|
def forecast(self) -> list[Forecast] | None:
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return the forecast array."""
|
2021-10-26 21:23:43 +00:00
|
|
|
return get_forecast(self.ec_data, self._hourly)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2023-08-24 09:28:20 +00:00
|
|
|
@callback
|
|
|
|
def _async_forecast_daily(self) -> list[Forecast] | None:
|
2023-08-16 20:10:48 +00:00
|
|
|
"""Return the daily forecast in native units."""
|
|
|
|
return get_forecast(self.ec_data, False)
|
|
|
|
|
2023-08-24 09:28:20 +00:00
|
|
|
@callback
|
|
|
|
def _async_forecast_hourly(self) -> list[Forecast] | None:
|
2023-08-16 20:10:48 +00:00
|
|
|
"""Return the hourly forecast in native units."""
|
|
|
|
return get_forecast(self.ec_data, True)
|
|
|
|
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2023-08-16 20:10:48 +00:00
|
|
|
def get_forecast(ec_data, hourly) -> list[Forecast] | None:
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Build the forecast array."""
|
2023-08-16 20:10:48 +00:00
|
|
|
forecast_array: list[Forecast] = []
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
if not hourly:
|
2021-10-17 18:05:11 +00:00
|
|
|
if not (half_days := ec_data.daily_forecasts):
|
2021-10-11 15:33:29 +00:00
|
|
|
return None
|
2021-02-02 07:23:26 +00:00
|
|
|
|
2023-08-16 20:10:48 +00:00
|
|
|
today: Forecast = {
|
2023-05-29 21:00:11 +00:00
|
|
|
ATTR_FORECAST_TIME: dt_util.now().isoformat(),
|
2021-02-02 07:23:26 +00:00
|
|
|
ATTR_FORECAST_CONDITION: icon_code_to_condition(
|
|
|
|
int(half_days[0]["icon_code"])
|
|
|
|
),
|
|
|
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
|
|
|
|
half_days[0]["precip_probability"]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if half_days[0]["temperature_class"] == "high":
|
2021-02-02 07:23:26 +00:00
|
|
|
today.update(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
2022-06-28 09:01:14 +00:00
|
|
|
ATTR_FORECAST_NATIVE_TEMP: int(half_days[0]["temperature"]),
|
|
|
|
ATTR_FORECAST_NATIVE_TEMP_LOW: int(half_days[1]["temperature"]),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2021-06-08 01:23:44 +00:00
|
|
|
half_days = half_days[2:]
|
2019-06-06 18:47:27 +00:00
|
|
|
else:
|
2021-02-02 07:23:26 +00:00
|
|
|
today.update(
|
|
|
|
{
|
2022-06-28 09:01:14 +00:00
|
|
|
ATTR_FORECAST_NATIVE_TEMP: None,
|
|
|
|
ATTR_FORECAST_NATIVE_TEMP_LOW: int(half_days[0]["temperature"]),
|
2021-02-02 07:23:26 +00:00
|
|
|
}
|
|
|
|
)
|
2021-06-08 01:23:44 +00:00
|
|
|
half_days = half_days[1:]
|
2021-02-02 07:23:26 +00:00
|
|
|
|
|
|
|
forecast_array.append(today)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for day, high, low in zip(range(1, 6), range(0, 9, 2), range(1, 10, 2)):
|
|
|
|
forecast_array.append(
|
|
|
|
{
|
|
|
|
ATTR_FORECAST_TIME: (
|
2023-05-29 21:00:11 +00:00
|
|
|
dt_util.now() + datetime.timedelta(days=day)
|
2019-07-31 19:25:30 +00:00
|
|
|
).isoformat(),
|
2022-06-28 09:01:14 +00:00
|
|
|
ATTR_FORECAST_NATIVE_TEMP: int(half_days[high]["temperature"]),
|
|
|
|
ATTR_FORECAST_NATIVE_TEMP_LOW: int(half_days[low]["temperature"]),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_CONDITION: icon_code_to_condition(
|
|
|
|
int(half_days[high]["icon_code"])
|
|
|
|
),
|
2020-06-17 05:39:33 +00:00
|
|
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
|
|
|
|
half_days[high]["precip_probability"]
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
else:
|
2021-06-28 10:57:07 +00:00
|
|
|
for hour in ec_data.hourly_forecasts:
|
2019-07-31 19:25:30 +00:00
|
|
|
forecast_array.append(
|
|
|
|
{
|
2022-04-26 16:45:33 +00:00
|
|
|
ATTR_FORECAST_TIME: hour["period"].isoformat(),
|
2022-06-28 09:01:14 +00:00
|
|
|
ATTR_FORECAST_NATIVE_TEMP: int(hour["temperature"]),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_CONDITION: icon_code_to_condition(
|
2021-06-28 10:57:07 +00:00
|
|
|
int(hour["icon_code"])
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
2020-06-17 05:39:33 +00:00
|
|
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
|
2021-06-28 10:57:07 +00:00
|
|
|
hour["precip_probability"]
|
2020-06-17 05:39:33 +00:00
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
return forecast_array
|
|
|
|
|
|
|
|
|
|
|
|
def icon_code_to_condition(icon_code):
|
|
|
|
"""Return the condition corresponding to an icon code."""
|
|
|
|
for condition, codes in ICON_CONDITION_MAP.items():
|
|
|
|
if icon_code in codes:
|
|
|
|
return condition
|
|
|
|
return None
|