Migrate Environment Canada to new entity naming style (#75024)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Glenn Waters 2022-07-12 14:45:38 -04:00 committed by GitHub
parent 397f94ee50
commit 96ecbe4388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 22 deletions

View File

@ -9,6 +9,8 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, Platform from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import CONF_LANGUAGE, CONF_STATION, DOMAIN from .const import CONF_LANGUAGE, CONF_STATION, DOMAIN
@ -87,6 +89,17 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
return unload_ok return unload_ok
def device_info(config_entry: ConfigEntry) -> DeviceInfo:
"""Build and return the device info for EC."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, config_entry.entry_id)},
manufacturer="Environment Canada",
name=config_entry.title,
configuration_url="https://weather.gc.ca/",
)
class ECDataUpdateCoordinator(DataUpdateCoordinator): class ECDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching EC data.""" """Class to manage fetching EC data."""

View File

@ -12,6 +12,7 @@ from homeassistant.helpers.entity_platform import (
) )
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import device_info
from .const import ATTR_OBSERVATION_TIME, DOMAIN from .const import ATTR_OBSERVATION_TIME, DOMAIN
SERVICE_SET_RADAR_TYPE = "set_radar_type" SERVICE_SET_RADAR_TYPE = "set_radar_type"
@ -40,16 +41,19 @@ async def async_setup_entry(
class ECCamera(CoordinatorEntity, Camera): class ECCamera(CoordinatorEntity, Camera):
"""Implementation of an Environment Canada radar camera.""" """Implementation of an Environment Canada radar camera."""
_attr_has_entity_name = True
_attr_name = "Radar"
def __init__(self, coordinator): def __init__(self, coordinator):
"""Initialize the camera.""" """Initialize the camera."""
super().__init__(coordinator) super().__init__(coordinator)
Camera.__init__(self) Camera.__init__(self)
self.radar_object = coordinator.ec_data 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_unique_id = f"{coordinator.config_entry.unique_id}-radar"
self._attr_attribution = self.radar_object.metadata["attribution"] self._attr_attribution = self.radar_object.metadata["attribution"]
self._attr_entity_registry_enabled_default = False self._attr_entity_registry_enabled_default = False
self._attr_device_info = device_info(coordinator.config_entry)
self.content_type = "image/gif" self.content_type = "image/gif"

View File

@ -27,6 +27,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import device_info
from .const import ATTR_STATION, DOMAIN from .const import ATTR_STATION, DOMAIN
ATTR_TIME = "alert time" ATTR_TIME = "alert time"
@ -51,12 +52,12 @@ class ECSensorEntityDescription(
SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = (
ECSensorEntityDescription( ECSensorEntityDescription(
key="condition", key="condition",
name="Current Condition", name="Current condition",
value_fn=lambda data: data.conditions.get("condition", {}).get("value"), value_fn=lambda data: data.conditions.get("condition", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="dewpoint", key="dewpoint",
name="Dew Point", name="Dew point",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -64,7 +65,7 @@ SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = (
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="high_temp", key="high_temp",
name="High Temperature", name="High temperature",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -88,12 +89,12 @@ SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = (
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="icon_code", key="icon_code",
name="Icon Code", name="Icon code",
value_fn=lambda data: data.conditions.get("icon_code", {}).get("value"), value_fn=lambda data: data.conditions.get("icon_code", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="low_temp", key="low_temp",
name="Low Temperature", name="Low temperature",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -101,34 +102,34 @@ SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = (
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="normal_high", key="normal_high",
name="Normal High Temperature", name="Normal high temperature",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
value_fn=lambda data: data.conditions.get("normal_high", {}).get("value"), value_fn=lambda data: data.conditions.get("normal_high", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="normal_low", key="normal_low",
name="Normal Low Temperature", name="Normal low temperature",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
value_fn=lambda data: data.conditions.get("normal_low", {}).get("value"), value_fn=lambda data: data.conditions.get("normal_low", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="pop", key="pop",
name="Chance of Precipitation", name="Chance of precipitation",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
value_fn=lambda data: data.conditions.get("pop", {}).get("value"), value_fn=lambda data: data.conditions.get("pop", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="precip_yesterday", key="precip_yesterday",
name="Precipitation Yesterday", name="Precipitation yesterday",
native_unit_of_measurement=LENGTH_MILLIMETERS, native_unit_of_measurement=LENGTH_MILLIMETERS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.conditions.get("precip_yesterday", {}).get("value"), value_fn=lambda data: data.conditions.get("precip_yesterday", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="pressure", key="pressure",
name="Barometric Pressure", name="Barometric pressure",
device_class=SensorDeviceClass.PRESSURE, device_class=SensorDeviceClass.PRESSURE,
native_unit_of_measurement=PRESSURE_KPA, native_unit_of_measurement=PRESSURE_KPA,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -156,13 +157,13 @@ SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = (
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="timestamp", key="timestamp",
name="Observation Time", name="Observation time",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
value_fn=lambda data: data.metadata.get("timestamp"), value_fn=lambda data: data.metadata.get("timestamp"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="uv_index", key="uv_index",
name="UV Index", name="UV index",
native_unit_of_measurement=UV_INDEX, native_unit_of_measurement=UV_INDEX,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.conditions.get("uv_index", {}).get("value"), value_fn=lambda data: data.conditions.get("uv_index", {}).get("value"),
@ -176,13 +177,13 @@ SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = (
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="wind_bearing", key="wind_bearing",
name="Wind Bearing", name="Wind bearing",
native_unit_of_measurement=DEGREE, native_unit_of_measurement=DEGREE,
value_fn=lambda data: data.conditions.get("wind_bearing", {}).get("value"), value_fn=lambda data: data.conditions.get("wind_bearing", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="wind_chill", key="wind_chill",
name="Wind Chill", name="Wind chill",
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -190,19 +191,19 @@ SENSOR_TYPES: tuple[ECSensorEntityDescription, ...] = (
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="wind_dir", key="wind_dir",
name="Wind Direction", name="Wind direction",
value_fn=lambda data: data.conditions.get("wind_dir", {}).get("value"), value_fn=lambda data: data.conditions.get("wind_dir", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="wind_gust", key="wind_gust",
name="Wind Gust", name="Wind gust",
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR, native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.conditions.get("wind_gust", {}).get("value"), value_fn=lambda data: data.conditions.get("wind_gust", {}).get("value"),
), ),
ECSensorEntityDescription( ECSensorEntityDescription(
key="wind_speed", key="wind_speed",
name="Wind Speed", name="Wind speed",
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR, native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data.conditions.get("wind_speed", {}).get("value"), value_fn=lambda data: data.conditions.get("wind_speed", {}).get("value"),
@ -285,6 +286,7 @@ class ECBaseSensor(CoordinatorEntity, SensorEntity):
"""Environment Canada sensor base.""" """Environment Canada sensor base."""
entity_description: ECSensorEntityDescription entity_description: ECSensorEntityDescription
_attr_has_entity_name = True
def __init__(self, coordinator, description): def __init__(self, coordinator, description):
"""Initialize the base sensor.""" """Initialize the base sensor."""
@ -292,8 +294,8 @@ class ECBaseSensor(CoordinatorEntity, SensorEntity):
self.entity_description = description self.entity_description = description
self._ec_data = coordinator.ec_data self._ec_data = coordinator.ec_data
self._attr_attribution = self._ec_data.metadata["attribution"] self._attr_attribution = self._ec_data.metadata["attribution"]
self._attr_name = f"{coordinator.config_entry.title} {description.name}"
self._attr_unique_id = f"{coordinator.config_entry.title}-{description.key}" self._attr_unique_id = f"{coordinator.config_entry.title}-{description.key}"
self._attr_device_info = device_info(coordinator.config_entry)
@property @property
def native_value(self): def native_value(self):

View File

@ -35,6 +35,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt from homeassistant.util import dt
from . import device_info
from .const import DOMAIN from .const import DOMAIN
# Icon codes from http://dd.weatheroffice.ec.gc.ca/citypage_weather/ # Icon codes from http://dd.weatheroffice.ec.gc.ca/citypage_weather/
@ -68,6 +69,7 @@ async def async_setup_entry(
class ECWeather(CoordinatorEntity, WeatherEntity): class ECWeather(CoordinatorEntity, WeatherEntity):
"""Representation of a weather condition.""" """Representation of a weather condition."""
_attr_has_entity_name = True
_attr_native_pressure_unit = PRESSURE_KPA _attr_native_pressure_unit = PRESSURE_KPA
_attr_native_temperature_unit = TEMP_CELSIUS _attr_native_temperature_unit = TEMP_CELSIUS
_attr_native_visibility_unit = LENGTH_KILOMETERS _attr_native_visibility_unit = LENGTH_KILOMETERS
@ -78,14 +80,13 @@ class ECWeather(CoordinatorEntity, WeatherEntity):
super().__init__(coordinator) super().__init__(coordinator)
self.ec_data = coordinator.ec_data self.ec_data = coordinator.ec_data
self._attr_attribution = self.ec_data.metadata["attribution"] self._attr_attribution = self.ec_data.metadata["attribution"]
self._attr_name = ( self._attr_name = "Hourly forecast" if hourly else "Forecast"
f"{coordinator.config_entry.title}{' Hourly' if hourly else ''}"
)
self._attr_unique_id = ( self._attr_unique_id = (
f"{coordinator.config_entry.unique_id}{'-hourly' if hourly else '-daily'}" f"{coordinator.config_entry.unique_id}{'-hourly' if hourly else '-daily'}"
) )
self._attr_entity_registry_enabled_default = not hourly self._attr_entity_registry_enabled_default = not hourly
self._hourly = hourly self._hourly = hourly
self._attr_device_info = device_info(coordinator.config_entry)
@property @property
def native_temperature(self): def native_temperature(self):