Use EntityDescription - picnic (#55682)

* Use EntityDescription - picnic

* Change _attr_extra_state_attributes to be static

* Fix tests
This commit is contained in:
Marc Mueller 2021-09-03 22:35:59 +02:00 committed by GitHub
parent 9db13a3e74
commit ce6921d73c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 150 additions and 134 deletions

View file

@ -13,7 +13,14 @@ from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
)
from .const import ADDRESS, ATTRIBUTION, CONF_COORDINATOR, DOMAIN, SENSOR_TYPES
from .const import (
ADDRESS,
ATTRIBUTION,
CONF_COORDINATOR,
DOMAIN,
SENSOR_TYPES,
PicnicSensorEntityDescription,
)
async def async_setup_entry(
@ -24,8 +31,8 @@ async def async_setup_entry(
# Add an entity for each sensor type
async_add_entities(
PicnicSensor(picnic_coordinator, config_entry, sensor_type, props)
for sensor_type, props in SENSOR_TYPES.items()
PicnicSensor(picnic_coordinator, config_entry, description)
for description in SENSOR_TYPES
)
return True
@ -34,71 +41,40 @@ async def async_setup_entry(
class PicnicSensor(SensorEntity, CoordinatorEntity):
"""The CoordinatorEntity subclass representing Picnic sensors."""
entity_description: PicnicSensorEntityDescription
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
def __init__(
self,
coordinator: DataUpdateCoordinator[Any],
config_entry: ConfigEntry,
sensor_type,
properties,
):
description: PicnicSensorEntityDescription,
) -> None:
"""Init a Picnic sensor."""
super().__init__(coordinator)
self.entity_description = description
self.sensor_type = sensor_type
self.properties = properties
self.entity_id = f"sensor.picnic_{sensor_type}"
self.entity_id = f"sensor.picnic_{description.key}"
self._service_unique_id = config_entry.unique_id
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit this state is expressed in."""
return self.properties.get("unit")
@property
def unique_id(self) -> str | None:
"""Return a unique ID."""
return f"{self._service_unique_id}.{self.sensor_type}"
@property
def name(self) -> str | None:
"""Return the name of the entity."""
return self._to_capitalized_name(self.sensor_type)
self._attr_name = self._to_capitalized_name(description.key)
self._attr_unique_id = f"{config_entry.unique_id}.{description.key}"
@property
def native_value(self) -> StateType:
"""Return the state of the entity."""
data_set = (
self.coordinator.data.get(self.properties["data_type"], {})
self.coordinator.data.get(self.entity_description.data_type, {})
if self.coordinator.data is not None
else {}
)
return self.properties["state"](data_set)
@property
def device_class(self) -> str | None:
"""Return the class of this device, from component DEVICE_CLASSES."""
return self.properties.get("class")
@property
def icon(self) -> str | None:
"""Return the icon to use in the frontend, if any."""
return self.properties["icon"]
return self.entity_description.state(data_set)
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self.coordinator.last_update_success and self.state is not None
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return self.properties.get("default_enabled", False)
@property
def extra_state_attributes(self):
"""Return the sensor specific state attributes."""
return {ATTR_ATTRIBUTION: ATTRIBUTION}
@property
def device_info(self):
"""Return device info."""