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
|
|
|
|
|
2021-12-19 12:40:39 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
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, DOMAIN
|
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]["radar_coordinator"]
|
|
|
|
async_add_entities([ECCamera(coordinator)])
|
2019-06-06 18:47:27 +00:00
|
|
|
|
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
class ECCamera(CoordinatorEntity, Camera):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Implementation of an Environment Canada radar camera."""
|
|
|
|
|
2021-10-26 21:23:43 +00:00
|
|
|
def __init__(self, coordinator):
|
2019-06-06 18:47:27 +00:00
|
|
|
"""Initialize the camera."""
|
2021-10-26 21:23:43 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
Camera.__init__(self)
|
|
|
|
|
|
|
|
self.radar_object = coordinator.ec_data
|
|
|
|
self._attr_name = f"{coordinator.config_entry.title} Radar"
|
|
|
|
self._attr_unique_id = f"{coordinator.config_entry.unique_id}-radar"
|
|
|
|
self._attr_attribution = self.radar_object.metadata["attribution"]
|
2021-11-22 11:40:25 +00:00
|
|
|
self._attr_entity_registry_enabled_default = False
|
2019-06-06 18:47:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.content_type = "image/gif"
|
2019-06-06 18:47:27 +00:00
|
|
|
self.image = None
|
2021-10-26 21:23:43 +00:00
|
|
|
self.observation_time = 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."""
|
2021-10-26 21:23:43 +00:00
|
|
|
self.observation_time = self.radar_object.timestamp
|
|
|
|
return self.radar_object.image
|
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."""
|
2021-10-26 21:23:43 +00:00
|
|
|
return {ATTR_OBSERVATION_TIME: self.observation_time}
|