2020-02-20 23:29:46 +00:00
|
|
|
"""Support for the Environment Canada radar imagery."""
|
2021-08-11 00:33:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-06-06 18:47:27 +00:00
|
|
|
import datetime
|
2021-10-11 15:33:29 +00:00
|
|
|
import logging
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
from env_canada import get_station_coords
|
|
|
|
from requests.exceptions import ConnectionError as RequestsConnectionError
|
2019-06-06 18:47:27 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera
|
2019-06-06 18:47:27 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-05 05:18:52 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
2019-12-05 05:18:52 +00:00
|
|
|
CONF_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-06-06 18:47:27 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-12-05 05:18:52 +00:00
|
|
|
from homeassistant.util import Throttle
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
from . import trigger_import
|
|
|
|
from .const import CONF_ATTRIBUTION, CONF_STATION, DOMAIN
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_LOOP = "loop"
|
|
|
|
CONF_PRECIP_TYPE = "precip_type"
|
2021-10-11 15:33:29 +00:00
|
|
|
ATTR_UPDATED = "updated"
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = datetime.timedelta(minutes=10)
|
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_LOOP, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
2019-08-18 04:19:44 +00:00
|
|
|
vol.Optional(CONF_STATION): cv.matches_regex(r"^C[A-Z]{4}$|^[A-Z]{3}$"),
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Inclusive(CONF_LATITUDE, "latlon"): cv.latitude,
|
|
|
|
vol.Inclusive(CONF_LONGITUDE, "latlon"): cv.longitude,
|
2020-11-16 19:09:18 +00:00
|
|
|
vol.Optional(CONF_PRECIP_TYPE): vol.In(["RAIN", "SNOW"]),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Set up the Environment Canada camera."""
|
|
|
|
if config.get(CONF_STATION):
|
2021-10-11 15:33:29 +00:00
|
|
|
lat, lon = await hass.async_add_executor_job(
|
|
|
|
get_station_coords, config[CONF_STATION]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-06-06 18:47:27 +00:00
|
|
|
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)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
config[CONF_LATITUDE] = lat
|
|
|
|
config[CONF_LONGITUDE] = lon
|
|
|
|
|
|
|
|
trigger_import(hass, config)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Add a weather entity from a config_entry."""
|
|
|
|
radar_data = hass.data[DOMAIN][config_entry.entry_id]["radar_data"]
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
ECCamera(
|
|
|
|
radar_data,
|
|
|
|
f"{config_entry.title} Radar",
|
|
|
|
f"{config_entry.unique_id}-radar",
|
|
|
|
),
|
|
|
|
]
|
2020-08-16 14:28:40 +00:00
|
|
|
)
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ECCamera(Camera):
|
|
|
|
"""Implementation of an Environment Canada radar camera."""
|
|
|
|
|
2021-10-11 15:33:29 +00:00
|
|
|
def __init__(self, radar_object, camera_name, unique_id):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Initialize the camera."""
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.radar_object = radar_object
|
2021-10-11 15:33:29 +00:00
|
|
|
self._attr_name = camera_name
|
|
|
|
self._attr_unique_id = unique_id
|
2019-07-31 19:25:30 +00:00
|
|
|
self.content_type = "image/gif"
|
2019-06-06 18:47:27 +00:00
|
|
|
self.image = None
|
2019-08-18 04:19:44 +00:00
|
|
|
self.timestamp = None
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
def camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Return bytes of camera image."""
|
|
|
|
self.update()
|
|
|
|
return self.image
|
|
|
|
|
|
|
|
@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."""
|
2020-10-05 10:51:48 +00:00
|
|
|
return {ATTR_ATTRIBUTION: CONF_ATTRIBUTION, ATTR_UPDATED: self.timestamp}
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
|
|
|
"""Update radar image."""
|
2021-10-11 15:33:29 +00:00
|
|
|
try:
|
2019-06-06 18:47:27 +00:00
|
|
|
self.image = self.radar_object.get_loop()
|
2021-10-11 15:33:29 +00:00
|
|
|
except RequestsConnectionError:
|
|
|
|
_LOGGER.warning("Radar data update failed due to rate limiting")
|
|
|
|
return
|
|
|
|
|
2020-05-23 20:25:00 +00:00
|
|
|
self.timestamp = self.radar_object.timestamp
|