Use entity class attributes for accuweather (#52431)

This commit is contained in:
Robert Hillis 2021-07-05 05:36:04 -04:00 committed by GitHub
parent 1cc8280959
commit 5367a036d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 73 deletions

View File

@ -13,7 +13,6 @@ from homeassistant.const import (
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TEMPERATURE,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
@ -84,32 +83,27 @@ class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
else: else:
self._description = FORECAST_SENSOR_TYPES[kind] self._description = FORECAST_SENSOR_TYPES[kind]
self._unit_system = API_METRIC if coordinator.is_metric else API_IMPERIAL self._unit_system = API_METRIC if coordinator.is_metric else API_IMPERIAL
self._name = name
self.kind = kind self.kind = kind
self._device_class = None
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION} self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
self.forecast_day = forecast_day self.forecast_day = forecast_day
self._attr_state_class = self._description.get(ATTR_STATE_CLASS) self._attr_state_class = self._description.get(ATTR_STATE_CLASS)
self._attr_icon = self._description[ATTR_ICON]
@property self._attr_device_class = self._description[ATTR_DEVICE_CLASS]
def name(self) -> str: self._attr_entity_registry_enabled_default = self._description[ATTR_ENABLED]
"""Return the name."""
if self.forecast_day is not None: if self.forecast_day is not None:
return f"{self._name} {self._description[ATTR_LABEL]} {self.forecast_day}d" self._attr_name = f"{name} {self._description[ATTR_LABEL]} {forecast_day}d"
return f"{self._name} {self._description[ATTR_LABEL]}" self._attr_unique_id = (
f"{coordinator.location_key}-{kind}-{forecast_day}".lower()
@property )
def unique_id(self) -> str: else:
"""Return a unique_id for this entity.""" self._attr_name = f"{name} {self._description[ATTR_LABEL]}"
if self.forecast_day is not None: self._attr_unique_id = f"{coordinator.location_key}-{kind}".lower()
return f"{self.coordinator.location_key}-{self.kind}-{self.forecast_day}".lower() if coordinator.is_metric:
return f"{self.coordinator.location_key}-{self.kind}".lower() self._attr_unit_of_measurement = self._description[ATTR_UNIT_METRIC]
else:
@property self._attr_unit_of_measurement = self._description[ATTR_UNIT_IMPERIAL]
def device_info(self) -> DeviceInfo: self._attr_device_info = {
"""Return the device info.""" "identifiers": {(DOMAIN, coordinator.location_key)},
return {
"identifiers": {(DOMAIN, self.coordinator.location_key)},
"name": NAME, "name": NAME,
"manufacturer": MANUFACTURER, "manufacturer": MANUFACTURER,
"entry_type": "service", "entry_type": "service",
@ -139,23 +133,6 @@ class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
return cast(StateType, self._sensor_data["Speed"]["Value"]) return cast(StateType, self._sensor_data["Speed"]["Value"])
return cast(StateType, self._sensor_data) return cast(StateType, self._sensor_data)
@property
def icon(self) -> str | None:
"""Return the icon."""
return self._description[ATTR_ICON]
@property
def device_class(self) -> str | None:
"""Return the device_class."""
return self._description[ATTR_DEVICE_CLASS]
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit the value is expressed in."""
if self.coordinator.is_metric:
return self._description[ATTR_UNIT_METRIC]
return self._description[ATTR_UNIT_IMPERIAL]
@property @property
def extra_state_attributes(self) -> dict[str, Any]: def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes.""" """Return the state attributes."""
@ -171,11 +148,6 @@ class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
self._attrs["type"] = self.coordinator.data["PrecipitationType"] self._attrs["type"] = self.coordinator.data["PrecipitationType"]
return self._attrs return self._attrs
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self._description[ATTR_ENABLED]
@callback @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:
"""Handle data update.""" """Handle data update."""

View File

@ -19,7 +19,6 @@ from homeassistant.components.weather import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.const import CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
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 homeassistant.util.dt import utc_from_timestamp from homeassistant.util.dt import utc_from_timestamp
@ -60,29 +59,15 @@ class AccuWeatherEntity(CoordinatorEntity, WeatherEntity):
) -> None: ) -> None:
"""Initialize.""" """Initialize."""
super().__init__(coordinator) super().__init__(coordinator)
self._name = name self._unit_system = API_METRIC if coordinator.is_metric else API_IMPERIAL
self._unit_system = API_METRIC if self.coordinator.is_metric else API_IMPERIAL self._attr_name = name
self._attr_unique_id = coordinator.location_key
@property self._attr_temperature_unit = (
def name(self) -> str: TEMP_CELSIUS if coordinator.is_metric else TEMP_FAHRENHEIT
"""Return the name.""" )
return self._name self._attr_attribution = ATTRIBUTION
self._attr_device_info = {
@property "identifiers": {(DOMAIN, coordinator.location_key)},
def attribution(self) -> str:
"""Return the attribution."""
return ATTRIBUTION
@property
def unique_id(self) -> str:
"""Return a unique_id for this entity."""
return self.coordinator.location_key
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return {
"identifiers": {(DOMAIN, self.coordinator.location_key)},
"name": NAME, "name": NAME,
"manufacturer": MANUFACTURER, "manufacturer": MANUFACTURER,
"entry_type": "service", "entry_type": "service",
@ -107,11 +92,6 @@ class AccuWeatherEntity(CoordinatorEntity, WeatherEntity):
float, self.coordinator.data["Temperature"][self._unit_system]["Value"] float, self.coordinator.data["Temperature"][self._unit_system]["Value"]
) )
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
return TEMP_CELSIUS if self.coordinator.is_metric else TEMP_FAHRENHEIT
@property @property
def pressure(self) -> float: def pressure(self) -> float:
"""Return the pressure.""" """Return the pressure."""