Use attrs instead of properties in Airly integration (#51712)

This commit is contained in:
Maciej Bieniek 2021-06-10 19:32:41 +02:00 committed by GitHub
parent 453da10b62
commit 9d64b64d34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 53 deletions

View file

@ -64,18 +64,9 @@ class AirlyAirQuality(CoordinatorEntity, AirQualityEntity):
def __init__(self, coordinator: AirlyDataUpdateCoordinator, name: str) -> None:
"""Initialize."""
super().__init__(coordinator)
self._name = name
self._icon = "mdi:blur"
@property
def name(self) -> str:
"""Return the name."""
return self._name
@property
def icon(self) -> str:
"""Return the icon."""
return self._icon
self._attr_icon = "mdi:blur"
self._attr_name = name
self._attr_unique_id = f"{coordinator.latitude}-{coordinator.longitude}"
@property
def air_quality_index(self) -> float | None:
@ -97,11 +88,6 @@ class AirlyAirQuality(CoordinatorEntity, AirQualityEntity):
"""Return the attribution."""
return ATTRIBUTION
@property
def unique_id(self) -> str:
"""Return a unique_id for this entity."""
return f"{self.coordinator.latitude}-{self.coordinator.longitude}"
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""

View file

@ -1,7 +1,7 @@
"""Support for the Airly sensor service."""
from __future__ import annotations
from typing import Any, cast
from typing import cast
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorEntity
from homeassistant.config_entries import ConfigEntry
@ -60,18 +60,18 @@ class AirlySensor(CoordinatorEntity, SensorEntity):
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._name = name
self._description = SENSOR_TYPES[kind]
description = SENSOR_TYPES[kind]
self._attr_device_class = description[ATTR_DEVICE_CLASS]
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
self._attr_icon = description[ATTR_ICON]
self._attr_name = f"{name} {description[ATTR_LABEL]}"
self._attr_state_class = description[ATTR_STATE_CLASS]
self._attr_unique_id = (
f"{coordinator.latitude}-{coordinator.longitude}-{kind.lower()}"
)
self._attr_unit_of_measurement = description[ATTR_UNIT]
self.kind = kind
self._state = None
self._unit_of_measurement = None
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
self._attr_state_class = self._description[ATTR_STATE_CLASS]
@property
def name(self) -> str:
"""Return the name."""
return f"{self._name} {self._description[ATTR_LABEL]}"
@property
def state(self) -> StateType:
@ -81,26 +81,6 @@ class AirlySensor(CoordinatorEntity, SensorEntity):
return round(cast(float, self._state))
return round(cast(float, self._state), 1)
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
return self._attrs
@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 unique_id(self) -> str:
"""Return a unique_id for this entity."""
return f"{self.coordinator.latitude}-{self.coordinator.longitude}-{self.kind.lower()}"
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
@ -115,8 +95,3 @@ class AirlySensor(CoordinatorEntity, SensorEntity):
"manufacturer": MANUFACTURER,
"entry_type": "service",
}
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit the value is expressed in."""
return self._description[ATTR_UNIT]