Skip to content

Commit

Permalink
Add base entity for Airgradient (home-assistant#117135)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored May 9, 2024
1 parent 6485973 commit b30a02d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
24 changes: 24 additions & 0 deletions homeassistant/components/airgradient/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Base class for AirGradient entities."""

from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import AirGradientDataUpdateCoordinator


class AirGradientEntity(CoordinatorEntity[AirGradientDataUpdateCoordinator]):
"""Defines a base AirGradient entity."""

_attr_has_entity_name = True

def __init__(self, coordinator: AirGradientDataUpdateCoordinator) -> None:
"""Initialize airgradient entity."""
super().__init__(coordinator)
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.data.serial_number)},
model=coordinator.data.model,
manufacturer="AirGradient",
serial_number=coordinator.data.serial_number,
sw_version=coordinator.data.firmware_version,
)
17 changes: 2 additions & 15 deletions homeassistant/components/airgradient/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import AirGradientDataUpdateCoordinator
from .const import DOMAIN
from .entity import AirGradientEntity


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -159,13 +158,9 @@ def add_entities() -> None:
add_entities()


class AirGradientSensor(
CoordinatorEntity[AirGradientDataUpdateCoordinator], SensorEntity
):
class AirGradientSensor(AirGradientEntity, SensorEntity):
"""Defines an AirGradient sensor."""

_attr_has_entity_name = True

entity_description: AirGradientSensorEntityDescription

def __init__(
Expand All @@ -175,16 +170,8 @@ def __init__(
) -> None:
"""Initialize airgradient sensor."""
super().__init__(coordinator)

self.entity_description = description
self._attr_unique_id = f"{coordinator.data.serial_number}-{description.key}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, coordinator.data.serial_number)},
model=coordinator.data.model,
manufacturer="AirGradient",
serial_number=coordinator.data.serial_number,
sw_version=coordinator.data.firmware_version,
)

@property
def native_value(self) -> StateType:
Expand Down

0 comments on commit b30a02d

Please sign in to comment.