2020-02-20 23:29:46 +00:00
|
|
|
"""Support for the Environment Canada weather service."""
|
2019-06-06 18:47:27 +00:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-12-19 12:40:39 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
|
|
|
from homeassistant.const import ATTR_LOCATION, TEMP_CELSIUS
|
2021-10-26 21:23:43 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-12-19 12:40:39 +00:00
|
|
|
from .const import ATTR_OBSERVATION_TIME, ATTR_STATION, DOMAIN
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TIME = "alert time"
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_station(station):
|
|
|
|
"""Check that the station ID is well-formed."""
|
|
|
|
if station is None:
|
2021-10-11 15:33:29 +00:00
|
|
|
return None
|
2019-07-31 19:25:30 +00:00
|
|
|
if not re.fullmatch(r"[A-Z]{2}/s0000\d{3}", station):
|
2021-10-26 21:23:43 +00:00
|
|
|
raise vol.Invalid('Station ID must be of the form "XX/s0000###"')
|
2019-06-06 18:47:27 +00:00
|
|
|
return station
|
|
|
|
|
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""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"]
|
|
|
|
weather_data = coordinator.ec_data
|
|
|
|
|
|
|
|
sensors = list(weather_data.conditions)
|
|
|
|
labels = [weather_data.conditions[sensor]["label"] for sensor in sensors]
|
|
|
|
alerts_list = list(weather_data.alerts)
|
|
|
|
labels = labels + [weather_data.alerts[sensor]["label"] for sensor in alerts_list]
|
|
|
|
sensors = sensors + alerts_list
|
2021-10-11 15:33:29 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
2021-10-26 21:23:43 +00:00
|
|
|
ECSensor(coordinator, sensor, label)
|
|
|
|
for sensor, label in zip(sensors, labels)
|
2021-10-11 15:33:29 +00:00
|
|
|
],
|
|
|
|
True,
|
|
|
|
)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
class ECSensor(CoordinatorEntity, SensorEntity):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Implementation of an Environment Canada sensor."""
|
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
def __init__(self, coordinator, sensor, label):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-10-26 21:23:43 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
self.sensor_type = sensor
|
|
|
|
self.ec_data = coordinator.ec_data
|
2019-07-13 16:14:29 +00:00
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
self._attr_attribution = self.ec_data.metadata["attribution"]
|
|
|
|
self._attr_name = f"{coordinator.config_entry.title} {label}"
|
|
|
|
self._attr_unique_id = f"{self.ec_data.metadata['location']}-{sensor}"
|
2019-06-06 18:47:27 +00:00
|
|
|
self._attr = None
|
2019-07-13 16:14:29 +00:00
|
|
|
self._unit = None
|
2021-07-12 20:45:29 +00:00
|
|
|
self._device_class = None
|
2019-07-13 16:14:29 +00:00
|
|
|
|
2019-06-06 18:47:27 +00:00
|
|
|
@property
|
2021-03-11 15:51:03 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
return self._attr
|
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:12 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return the units of measurement."""
|
2019-07-13 16:14:29 +00:00
|
|
|
return self._unit
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-07-12 20:45:29 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
|
|
return self._device_class
|
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
@property
|
|
|
|
def native_value(self):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Update current conditions."""
|
2019-07-13 16:14:29 +00:00
|
|
|
metadata = self.ec_data.metadata
|
2021-10-26 21:23:43 +00:00
|
|
|
sensor_data = self.ec_data.conditions.get(self.sensor_type)
|
|
|
|
if not sensor_data:
|
|
|
|
sensor_data = self.ec_data.alerts.get(self.sensor_type)
|
2019-07-13 16:14:29 +00:00
|
|
|
|
2019-06-06 18:47:27 +00:00
|
|
|
self._attr = {}
|
2019-07-31 19:25:30 +00:00
|
|
|
value = sensor_data.get("value")
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2019-07-13 16:14:29 +00:00
|
|
|
if isinstance(value, list):
|
2021-10-26 21:23:43 +00:00
|
|
|
state = " | ".join([str(s.get("title")) for s in value])[:255]
|
2019-07-31 19:25:30 +00:00
|
|
|
self._attr.update(
|
2020-02-23 04:32:19 +00:00
|
|
|
{ATTR_TIME: " | ".join([str(s.get("date")) for s in value])}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-08-18 04:19:44 +00:00
|
|
|
elif self.sensor_type == "tendency":
|
2021-10-26 21:23:43 +00:00
|
|
|
state = str(value).capitalize()
|
|
|
|
elif isinstance(value, str) and len(value) > 255:
|
|
|
|
state = value[:255]
|
2021-10-11 15:33:29 +00:00
|
|
|
_LOGGER.info(
|
|
|
|
"Value for %s truncated to 255 characters", self._attr_unique_id
|
|
|
|
)
|
2019-11-29 03:13:21 +00:00
|
|
|
else:
|
2021-10-26 21:23:43 +00:00
|
|
|
state = value
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-07-29 23:20:03 +00:00
|
|
|
if sensor_data.get("unit") == "C" or self.sensor_type in (
|
2019-08-18 04:19:44 +00:00
|
|
|
"wind_chill",
|
|
|
|
"humidex",
|
2021-07-29 23:20:03 +00:00
|
|
|
):
|
2019-07-13 16:14:29 +00:00
|
|
|
self._unit = TEMP_CELSIUS
|
2021-12-10 08:14:06 +00:00
|
|
|
self._device_class = SensorDeviceClass.TEMPERATURE
|
2019-06-06 18:47:27 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._unit = sensor_data.get("unit")
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._attr.update(
|
|
|
|
{
|
2021-10-26 21:23:43 +00:00
|
|
|
ATTR_OBSERVATION_TIME: metadata.get("timestamp"),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_LOCATION: metadata.get("location"),
|
|
|
|
ATTR_STATION: metadata.get("station"),
|
|
|
|
}
|
|
|
|
)
|
2021-10-26 21:23:43 +00:00
|
|
|
return state
|