From caff81f6132384b063f01f4168e44a7742386b36 Mon Sep 17 00:00:00 2001 From: Glenn Waters Date: Wed, 6 Apr 2022 19:55:37 -0400 Subject: [PATCH] Add service to set type of radar to retrieve. (#68252) --- .../components/environment_canada/camera.py | 35 +++++++++++++------ .../environment_canada/services.yaml | 19 ++++++++++ 2 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 homeassistant/components/environment_canada/services.yaml diff --git a/homeassistant/components/environment_canada/camera.py b/homeassistant/components/environment_canada/camera.py index e09ae99e006..e415bab977b 100644 --- a/homeassistant/components/environment_canada/camera.py +++ b/homeassistant/components/environment_canada/camera.py @@ -1,14 +1,24 @@ """Support for the Environment Canada radar imagery.""" from __future__ import annotations +import voluptuous as vol + from homeassistant.components.camera import Camera from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.entity_platform import ( + AddEntitiesCallback, + async_get_current_platform, +) from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ATTR_OBSERVATION_TIME, DOMAIN +SERVICE_SET_RADAR_TYPE = "set_radar_type" +SET_RADAR_TYPE_SCHEMA = { + vol.Required("radar_type"): vol.In(["Auto", "Rain", "Snow"]), +} + async def async_setup_entry( hass: HomeAssistant, @@ -19,6 +29,13 @@ async def async_setup_entry( coordinator = hass.data[DOMAIN][config_entry.entry_id]["radar_coordinator"] async_add_entities([ECCamera(coordinator)]) + platform = async_get_current_platform() + platform.async_register_entity_service( + SERVICE_SET_RADAR_TYPE, + SET_RADAR_TYPE_SCHEMA, + "async_set_radar_type", + ) + class ECCamera(CoordinatorEntity, Camera): """Implementation of an Environment Canada radar camera.""" @@ -35,19 +52,17 @@ class ECCamera(CoordinatorEntity, Camera): self._attr_entity_registry_enabled_default = False self.content_type = "image/gif" - self.image = None - self.observation_time = None def camera_image( self, width: int | None = None, height: int | None = None ) -> bytes | None: """Return bytes of camera image.""" - if not hasattr(self.radar_object, "timestamp"): - return None - self.observation_time = self.radar_object.timestamp + self._attr_extra_state_attributes = { + ATTR_OBSERVATION_TIME: self.radar_object.timestamp, + } return self.radar_object.image - @property - def extra_state_attributes(self): - """Return the state attributes of the device.""" - return {ATTR_OBSERVATION_TIME: self.observation_time} + async def async_set_radar_type(self, radar_type: str): + """Set the type of radar to retrieve.""" + self.radar_object.precip_type = radar_type.lower() + await self.radar_object.update() diff --git a/homeassistant/components/environment_canada/services.yaml b/homeassistant/components/environment_canada/services.yaml new file mode 100644 index 00000000000..09f95f16a44 --- /dev/null +++ b/homeassistant/components/environment_canada/services.yaml @@ -0,0 +1,19 @@ +set_radar_type: + name: Set radar type + description: Set the type of radar image to retrieve. + target: + entity: + integration: environment_canada + domain: camera + fields: + radar_type: + name: Radar type + description: The type of radar image to display. + required: true + example: Snow + selector: + select: + options: + - "Auto" + - "Rain" + - "Snow"