2020-02-20 23:29:46 +00:00
|
|
|
"""Platform for retrieving meteorological data from Environment Canada."""
|
2019-06-06 18:47:27 +00:00
|
|
|
import datetime
|
|
|
|
import re
|
|
|
|
|
2021-03-02 08:02:04 +00:00
|
|
|
from env_canada import ECData
|
2019-06-06 18:47:27 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
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,
|
2020-06-17 05:39:33 +00:00
|
|
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FORECAST_TEMP,
|
|
|
|
ATTR_FORECAST_TEMP_LOW,
|
|
|
|
ATTR_FORECAST_TIME,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
WeatherEntity,
|
|
|
|
)
|
|
|
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, TEMP_CELSIUS
|
2019-06-06 18:47:27 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-12-05 05:18:52 +00:00
|
|
|
import homeassistant.util.dt as dt
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_FORECAST = "forecast"
|
2019-06-06 18:47:27 +00:00
|
|
|
CONF_ATTRIBUTION = "Data provided by Environment Canada"
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_STATION = "station"
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_station(station):
|
|
|
|
"""Check that the station ID is well-formed."""
|
|
|
|
if station is None:
|
|
|
|
return
|
2019-07-31 19:25:30 +00:00
|
|
|
if not re.fullmatch(r"[A-Z]{2}/s0000\d{3}", station):
|
2019-06-06 18:47:27 +00:00
|
|
|
raise vol.error.Invalid('Station ID must be of the form "XX/s0000###"')
|
|
|
|
return station
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_STATION): validate_station,
|
|
|
|
vol.Inclusive(CONF_LATITUDE, "latlon"): cv.latitude,
|
|
|
|
vol.Inclusive(CONF_LONGITUDE, "latlon"): cv.longitude,
|
|
|
|
vol.Optional(CONF_FORECAST, default="daily"): vol.In(["daily", "hourly"]),
|
|
|
|
}
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
"""Set up the Environment Canada weather."""
|
|
|
|
if config.get(CONF_STATION):
|
|
|
|
ec_data = ECData(station_id=config[CONF_STATION])
|
|
|
|
else:
|
2019-07-13 16:14:29 +00:00
|
|
|
lat = config.get(CONF_LATITUDE, hass.config.latitude)
|
|
|
|
lon = config.get(CONF_LONGITUDE, hass.config.longitude)
|
|
|
|
ec_data = ECData(coordinates=(lat, lon))
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
add_devices([ECWeather(ec_data, config)])
|
|
|
|
|
|
|
|
|
|
|
|
class ECWeather(WeatherEntity):
|
|
|
|
"""Representation of a weather condition."""
|
|
|
|
|
|
|
|
def __init__(self, ec_data, config):
|
|
|
|
"""Initialize Environment Canada weather."""
|
|
|
|
self.ec_data = ec_data
|
|
|
|
self.platform_name = config.get(CONF_NAME)
|
|
|
|
self.forecast_type = config[CONF_FORECAST]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def attribution(self):
|
|
|
|
"""Return the attribution."""
|
|
|
|
return CONF_ATTRIBUTION
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the weather entity."""
|
|
|
|
if self.platform_name:
|
|
|
|
return self.platform_name
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.ec_data.metadata.get("location")
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature(self):
|
|
|
|
"""Return the temperature."""
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("temperature", {}).get("value"):
|
2019-07-31 19:25:30 +00:00
|
|
|
return float(self.ec_data.conditions["temperature"]["value"])
|
|
|
|
if self.ec_data.hourly_forecasts[0].get("temperature"):
|
|
|
|
return float(self.ec_data.hourly_forecasts[0]["temperature"])
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def temperature_unit(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return TEMP_CELSIUS
|
|
|
|
|
|
|
|
@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
|
|
|
|
def wind_speed(self):
|
|
|
|
"""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
|
|
|
|
def pressure(self):
|
|
|
|
"""Return the pressure."""
|
2020-07-19 09:52:46 +00:00
|
|
|
if self.ec_data.conditions.get("pressure", {}).get("value"):
|
2019-07-31 19:25:30 +00:00
|
|
|
return 10 * float(self.ec_data.conditions["pressure"]["value"])
|
2019-06-06 18:47:27 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def visibility(self):
|
|
|
|
"""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"]
|
|
|
|
elif self.ec_data.hourly_forecasts[0].get("icon_code"):
|
|
|
|
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
|
|
|
|
def forecast(self):
|
|
|
|
"""Return the forecast array."""
|
|
|
|
return get_forecast(self.ec_data, self.forecast_type)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from Environment Canada."""
|
|
|
|
self.ec_data.update()
|
|
|
|
|
|
|
|
|
|
|
|
def get_forecast(ec_data, forecast_type):
|
|
|
|
"""Build the forecast array."""
|
|
|
|
forecast_array = []
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if forecast_type == "daily":
|
2019-06-06 18:47:27 +00:00
|
|
|
half_days = ec_data.daily_forecasts
|
2021-02-02 07:23:26 +00:00
|
|
|
|
|
|
|
today = {
|
|
|
|
ATTR_FORECAST_TIME: dt.now().isoformat(),
|
|
|
|
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
|
|
|
{
|
|
|
|
ATTR_FORECAST_TEMP: int(half_days[0]["temperature"]),
|
|
|
|
ATTR_FORECAST_TEMP_LOW: int(half_days[1]["temperature"]),
|
|
|
|
}
|
|
|
|
)
|
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(
|
|
|
|
{
|
2021-06-08 01:23:44 +00:00
|
|
|
ATTR_FORECAST_TEMP: None,
|
2021-02-02 07:23:26 +00:00
|
|
|
ATTR_FORECAST_TEMP_LOW: int(half_days[0]["temperature"]),
|
|
|
|
}
|
|
|
|
)
|
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: (
|
|
|
|
dt.now() + datetime.timedelta(days=day)
|
|
|
|
).isoformat(),
|
|
|
|
ATTR_FORECAST_TEMP: int(half_days[high]["temperature"]),
|
|
|
|
ATTR_FORECAST_TEMP_LOW: int(half_days[low]["temperature"]),
|
|
|
|
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
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
elif forecast_type == "hourly":
|
2019-06-06 18:47:27 +00:00
|
|
|
hours = ec_data.hourly_forecasts
|
|
|
|
for hour in range(0, 24):
|
2019-07-31 19:25:30 +00:00
|
|
|
forecast_array.append(
|
|
|
|
{
|
|
|
|
ATTR_FORECAST_TIME: dt.as_local(
|
|
|
|
datetime.datetime.strptime(hours[hour]["period"], "%Y%m%d%H%M")
|
|
|
|
).isoformat(),
|
|
|
|
ATTR_FORECAST_TEMP: int(hours[hour]["temperature"]),
|
|
|
|
ATTR_FORECAST_CONDITION: icon_code_to_condition(
|
|
|
|
int(hours[hour]["icon_code"])
|
|
|
|
),
|
2020-06-17 05:39:33 +00:00
|
|
|
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
|
|
|
|
hours[hour]["precip_probability"]
|
|
|
|
),
|
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
|