Use attrs instead of properties in Nettigo Air Monitor integration (#51705)

* Use attrs instead of properties

* Remove unused self

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Maciej Bieniek 2021-06-10 14:42:28 +02:00 committed by GitHub
parent fca0446ff8
commit 220ad2baea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 54 deletions

View File

@ -4,7 +4,6 @@ from __future__ import annotations
from homeassistant.components.air_quality import AirQualityEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
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
@ -44,13 +43,11 @@ class NAMAirQuality(CoordinatorEntity, AirQualityEntity):
def __init__(self, coordinator: NAMDataUpdateCoordinator, sensor_type: str) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_device_info = coordinator.device_info
self._attr_name = f"{DEFAULT_NAME} {AIR_QUALITY_SENSORS[sensor_type]}"
self._attr_unique_id = f"{coordinator.unique_id}-{sensor_type}"
self.sensor_type = sensor_type
@property
def name(self) -> str:
"""Return the name."""
return f"{DEFAULT_NAME} {AIR_QUALITY_SENSORS[self.sensor_type]}"
@property
def particulate_matter_2_5(self) -> StateType:
"""Return the particulate matter 2.5 level."""
@ -72,16 +69,6 @@ class NAMAirQuality(CoordinatorEntity, AirQualityEntity):
getattr(self.coordinator.data, ATTR_MHZ14A_CARBON_DIOXIDE, None)
)
@property
def unique_id(self) -> str:
"""Return a unique_id for this entity."""
return f"{self.coordinator.unique_id}-{self.sensor_type}"
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return self.coordinator.device_info
@property
def available(self) -> bool:
"""Return if entity is available."""

View File

@ -8,7 +8,6 @@ from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ICON
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.dt import utcnow
@ -44,50 +43,22 @@ class NAMSensor(CoordinatorEntity, SensorEntity):
def __init__(self, coordinator: NAMDataUpdateCoordinator, sensor_type: str) -> None:
"""Initialize."""
super().__init__(coordinator)
description = SENSORS[sensor_type]
self._attr_device_class = description[ATTR_DEVICE_CLASS]
self._attr_device_info = coordinator.device_info
self._attr_entity_registry_enabled_default = description[ATTR_ENABLED]
self._attr_icon = description[ATTR_ICON]
self._attr_name = description[ATTR_LABEL]
self._attr_state_class = description[ATTR_STATE_CLASS]
self._attr_unique_id = f"{coordinator.unique_id}-{sensor_type}"
self._attr_unit_of_measurement = description[ATTR_UNIT]
self.sensor_type = sensor_type
self._description = SENSORS[sensor_type]
self._attr_state_class = SENSORS[sensor_type][ATTR_STATE_CLASS]
@property
def name(self) -> str:
"""Return the name."""
return self._description[ATTR_LABEL]
@property
def state(self) -> Any:
"""Return the state."""
return getattr(self.coordinator.data, self.sensor_type)
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit the value is expressed in."""
return self._description[ATTR_UNIT]
@property
def device_class(self) -> str | None:
"""Return the class of this sensor."""
return self._description[ATTR_DEVICE_CLASS]
@property
def icon(self) -> str | None:
"""Return the icon."""
return self._description[ATTR_ICON]
@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]
@property
def unique_id(self) -> str:
"""Return a unique_id for this entity."""
return f"{self.coordinator.unique_id}-{self.sensor_type}"
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return self.coordinator.device_info
@property
def available(self) -> bool:
"""Return if entity is available."""