mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Move purpleair base entity to separate module (#126511)
This commit is contained in:
parent
c8e3e2ce1b
commit
78d80fefc5
@ -2,21 +2,9 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Mapping
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from aiopurpleair.models.sensors import SensorModel
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import Platform
|
||||||
ATTR_LATITUDE,
|
|
||||||
ATTR_LONGITUDE,
|
|
||||||
CONF_SHOW_ON_MAP,
|
|
||||||
Platform,
|
|
||||||
)
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import PurpleAirDataUpdateCoordinator
|
from .coordinator import PurpleAirDataUpdateCoordinator
|
||||||
@ -48,53 +36,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
class PurpleAirEntity(CoordinatorEntity[PurpleAirDataUpdateCoordinator]):
|
|
||||||
"""Define a base PurpleAir entity."""
|
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
coordinator: PurpleAirDataUpdateCoordinator,
|
|
||||||
entry: ConfigEntry,
|
|
||||||
sensor_index: int,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize."""
|
|
||||||
super().__init__(coordinator)
|
|
||||||
|
|
||||||
self._sensor_index = sensor_index
|
|
||||||
|
|
||||||
self._attr_device_info = DeviceInfo(
|
|
||||||
configuration_url=self.coordinator.async_get_map_url(sensor_index),
|
|
||||||
hw_version=self.sensor_data.hardware,
|
|
||||||
identifiers={(DOMAIN, str(sensor_index))},
|
|
||||||
manufacturer="PurpleAir, Inc.",
|
|
||||||
model=self.sensor_data.model,
|
|
||||||
name=self.sensor_data.name,
|
|
||||||
sw_version=self.sensor_data.firmware_version,
|
|
||||||
)
|
|
||||||
self._entry = entry
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self) -> Mapping[str, Any]:
|
|
||||||
"""Return entity specific state attributes."""
|
|
||||||
attrs = {}
|
|
||||||
|
|
||||||
# Displaying the geography on the map relies upon putting the latitude/longitude
|
|
||||||
# in the entity attributes with "latitude" and "longitude" as the keys.
|
|
||||||
# Conversely, we can hide the location on the map by using other keys, like
|
|
||||||
# "lati" and "long":
|
|
||||||
if self._entry.options.get(CONF_SHOW_ON_MAP):
|
|
||||||
attrs[ATTR_LATITUDE] = self.sensor_data.latitude
|
|
||||||
attrs[ATTR_LONGITUDE] = self.sensor_data.longitude
|
|
||||||
else:
|
|
||||||
attrs["lati"] = self.sensor_data.latitude
|
|
||||||
attrs["long"] = self.sensor_data.longitude
|
|
||||||
return attrs
|
|
||||||
|
|
||||||
@property
|
|
||||||
def sensor_data(self) -> SensorModel:
|
|
||||||
"""Define a property to get this entity's SensorModel object."""
|
|
||||||
return self.coordinator.data.data[self._sensor_index]
|
|
||||||
|
66
homeassistant/components/purpleair/entity.py
Normal file
66
homeassistant/components/purpleair/entity.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
"""The PurpleAir integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from aiopurpleair.models.sensors import SensorModel
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, CONF_SHOW_ON_MAP
|
||||||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .coordinator import PurpleAirDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
|
class PurpleAirEntity(CoordinatorEntity[PurpleAirDataUpdateCoordinator]):
|
||||||
|
"""Define a base PurpleAir entity."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: PurpleAirDataUpdateCoordinator,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
sensor_index: int,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
|
||||||
|
self._sensor_index = sensor_index
|
||||||
|
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
configuration_url=self.coordinator.async_get_map_url(sensor_index),
|
||||||
|
hw_version=self.sensor_data.hardware,
|
||||||
|
identifiers={(DOMAIN, str(sensor_index))},
|
||||||
|
manufacturer="PurpleAir, Inc.",
|
||||||
|
model=self.sensor_data.model,
|
||||||
|
name=self.sensor_data.name,
|
||||||
|
sw_version=self.sensor_data.firmware_version,
|
||||||
|
)
|
||||||
|
self._entry = entry
|
||||||
|
|
||||||
|
@property
|
||||||
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
||||||
|
"""Return entity specific state attributes."""
|
||||||
|
attrs = {}
|
||||||
|
|
||||||
|
# Displaying the geography on the map relies upon putting the latitude/longitude
|
||||||
|
# in the entity attributes with "latitude" and "longitude" as the keys.
|
||||||
|
# Conversely, we can hide the location on the map by using other keys, like
|
||||||
|
# "lati" and "long":
|
||||||
|
if self._entry.options.get(CONF_SHOW_ON_MAP):
|
||||||
|
attrs[ATTR_LATITUDE] = self.sensor_data.latitude
|
||||||
|
attrs[ATTR_LONGITUDE] = self.sensor_data.longitude
|
||||||
|
else:
|
||||||
|
attrs["lati"] = self.sensor_data.latitude
|
||||||
|
attrs["long"] = self.sensor_data.longitude
|
||||||
|
return attrs
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sensor_data(self) -> SensorModel:
|
||||||
|
"""Define a property to get this entity's SensorModel object."""
|
||||||
|
return self.coordinator.data.data[self._sensor_index]
|
@ -27,9 +27,9 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import PurpleAirEntity
|
|
||||||
from .const import CONF_SENSOR_INDICES, DOMAIN
|
from .const import CONF_SENSOR_INDICES, DOMAIN
|
||||||
from .coordinator import PurpleAirDataUpdateCoordinator
|
from .coordinator import PurpleAirDataUpdateCoordinator
|
||||||
|
from .entity import PurpleAirEntity
|
||||||
|
|
||||||
CONCENTRATION_PARTICLES_PER_100_MILLILITERS = f"particles/100{UnitOfVolume.MILLILITERS}"
|
CONCENTRATION_PARTICLES_PER_100_MILLILITERS = f"particles/100{UnitOfVolume.MILLILITERS}"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user