Use entity class attributes for accuweather (#52431)
This commit is contained in:
parent
1cc8280959
commit
5367a036d2
2 changed files with 25 additions and 73 deletions
|
@ -13,7 +13,6 @@ from homeassistant.const import (
|
|||
DEVICE_CLASS_TEMPERATURE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
@ -84,32 +83,27 @@ class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
|
|||
else:
|
||||
self._description = FORECAST_SENSOR_TYPES[kind]
|
||||
self._unit_system = API_METRIC if coordinator.is_metric else API_IMPERIAL
|
||||
self._name = name
|
||||
self.kind = kind
|
||||
self._device_class = None
|
||||
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||
self.forecast_day = forecast_day
|
||||
self._attr_state_class = self._description.get(ATTR_STATE_CLASS)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name."""
|
||||
self._attr_icon = self._description[ATTR_ICON]
|
||||
self._attr_device_class = self._description[ATTR_DEVICE_CLASS]
|
||||
self._attr_entity_registry_enabled_default = self._description[ATTR_ENABLED]
|
||||
if self.forecast_day is not None:
|
||||
return f"{self._name} {self._description[ATTR_LABEL]} {self.forecast_day}d"
|
||||
return f"{self._name} {self._description[ATTR_LABEL]}"
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique_id for this entity."""
|
||||
if self.forecast_day is not None:
|
||||
return f"{self.coordinator.location_key}-{self.kind}-{self.forecast_day}".lower()
|
||||
return f"{self.coordinator.location_key}-{self.kind}".lower()
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return the device info."""
|
||||
return {
|
||||
"identifiers": {(DOMAIN, self.coordinator.location_key)},
|
||||
self._attr_name = f"{name} {self._description[ATTR_LABEL]} {forecast_day}d"
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.location_key}-{kind}-{forecast_day}".lower()
|
||||
)
|
||||
else:
|
||||
self._attr_name = f"{name} {self._description[ATTR_LABEL]}"
|
||||
self._attr_unique_id = f"{coordinator.location_key}-{kind}".lower()
|
||||
if coordinator.is_metric:
|
||||
self._attr_unit_of_measurement = self._description[ATTR_UNIT_METRIC]
|
||||
else:
|
||||
self._attr_unit_of_measurement = self._description[ATTR_UNIT_IMPERIAL]
|
||||
self._attr_device_info = {
|
||||
"identifiers": {(DOMAIN, coordinator.location_key)},
|
||||
"name": NAME,
|
||||
"manufacturer": MANUFACTURER,
|
||||
"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)
|
||||
|
||||
@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
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
"""Return the state attributes."""
|
||||
|
@ -171,11 +148,6 @@ class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
|
|||
self._attrs["type"] = self.coordinator.data["PrecipitationType"]
|
||||
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
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle data update."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue