mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Use EntityDescription - foobot (#54996)
This commit is contained in:
parent
d5c26aece1
commit
3bd309299e
@ -1,4 +1,6 @@
|
|||||||
"""Support for the Foobot indoor air quality monitor."""
|
"""Support for the Foobot indoor air quality monitor."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
@ -7,7 +9,7 @@ import aiohttp
|
|||||||
from foobot_async import FoobotClient
|
from foobot_async import FoobotClient
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_TEMPERATURE,
|
ATTR_TEMPERATURE,
|
||||||
ATTR_TIME,
|
ATTR_TIME,
|
||||||
@ -36,25 +38,49 @@ ATTR_CARBON_DIOXIDE = "CO2"
|
|||||||
ATTR_VOLATILE_ORGANIC_COMPOUNDS = "VOC"
|
ATTR_VOLATILE_ORGANIC_COMPOUNDS = "VOC"
|
||||||
ATTR_FOOBOT_INDEX = "index"
|
ATTR_FOOBOT_INDEX = "index"
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"time": [ATTR_TIME, TIME_SECONDS, None, None],
|
SensorEntityDescription(
|
||||||
"pm": [ATTR_PM2_5, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, "mdi:cloud", None],
|
key="time",
|
||||||
"tmp": [ATTR_TEMPERATURE, TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE],
|
name=ATTR_TIME,
|
||||||
"hum": [ATTR_HUMIDITY, PERCENTAGE, "mdi:water-percent", None],
|
native_unit_of_measurement=TIME_SECONDS,
|
||||||
"co2": [
|
),
|
||||||
ATTR_CARBON_DIOXIDE,
|
SensorEntityDescription(
|
||||||
CONCENTRATION_PARTS_PER_MILLION,
|
key="pm",
|
||||||
"mdi:molecule-co2",
|
name=ATTR_PM2_5,
|
||||||
None,
|
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||||
],
|
icon="mdi:cloud",
|
||||||
"voc": [
|
),
|
||||||
ATTR_VOLATILE_ORGANIC_COMPOUNDS,
|
SensorEntityDescription(
|
||||||
CONCENTRATION_PARTS_PER_BILLION,
|
key="tmp",
|
||||||
"mdi:cloud",
|
name=ATTR_TEMPERATURE,
|
||||||
None,
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
],
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
"allpollu": [ATTR_FOOBOT_INDEX, PERCENTAGE, "mdi:percent", None],
|
),
|
||||||
}
|
SensorEntityDescription(
|
||||||
|
key="hum",
|
||||||
|
name=ATTR_HUMIDITY,
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
icon="mdi:water-percent",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="co2",
|
||||||
|
name=ATTR_CARBON_DIOXIDE,
|
||||||
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION,
|
||||||
|
icon="mdi:molecule-co2",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="voc",
|
||||||
|
name=ATTR_VOLATILE_ORGANIC_COMPOUNDS,
|
||||||
|
native_unit_of_measurement=CONCENTRATION_PARTS_PER_BILLION,
|
||||||
|
icon="mdi:cloud",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="allpollu",
|
||||||
|
name=ATTR_FOOBOT_INDEX,
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
icon="mdi:percent",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(minutes=10)
|
SCAN_INTERVAL = timedelta(minutes=10)
|
||||||
PARALLEL_UPDATES = 1
|
PARALLEL_UPDATES = 1
|
||||||
@ -74,17 +100,19 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
client = FoobotClient(
|
client = FoobotClient(
|
||||||
token, username, async_get_clientsession(hass), timeout=TIMEOUT
|
token, username, async_get_clientsession(hass), timeout=TIMEOUT
|
||||||
)
|
)
|
||||||
dev = []
|
entities = []
|
||||||
try:
|
try:
|
||||||
devices = await client.get_devices()
|
devices = await client.get_devices()
|
||||||
_LOGGER.debug("The following devices were found: %s", devices)
|
_LOGGER.debug("The following devices were found: %s", devices)
|
||||||
for device in devices:
|
for device in devices:
|
||||||
foobot_data = FoobotData(client, device["uuid"])
|
foobot_data = FoobotData(client, device["uuid"])
|
||||||
for sensor_type in SENSOR_TYPES:
|
entities.extend(
|
||||||
if sensor_type == "time":
|
[
|
||||||
continue
|
FoobotSensor(foobot_data, device, description)
|
||||||
foobot_sensor = FoobotSensor(foobot_data, device, sensor_type)
|
for description in SENSOR_TYPES
|
||||||
dev.append(foobot_sensor)
|
if description.key != "time"
|
||||||
|
]
|
||||||
|
)
|
||||||
except (
|
except (
|
||||||
aiohttp.client_exceptions.ClientConnectorError,
|
aiohttp.client_exceptions.ClientConnectorError,
|
||||||
asyncio.TimeoutError,
|
asyncio.TimeoutError,
|
||||||
@ -96,54 +124,29 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
except FoobotClient.ClientError:
|
except FoobotClient.ClientError:
|
||||||
_LOGGER.error("Failed to fetch data from foobot servers")
|
_LOGGER.error("Failed to fetch data from foobot servers")
|
||||||
return
|
return
|
||||||
async_add_entities(dev, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class FoobotSensor(SensorEntity):
|
class FoobotSensor(SensorEntity):
|
||||||
"""Implementation of a Foobot sensor."""
|
"""Implementation of a Foobot sensor."""
|
||||||
|
|
||||||
def __init__(self, data, device, sensor_type):
|
def __init__(self, data, device, description: SensorEntityDescription):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._uuid = device["uuid"]
|
self.entity_description = description
|
||||||
self.foobot_data = data
|
self.foobot_data = data
|
||||||
self._name = f"Foobot {device['name']} {SENSOR_TYPES[sensor_type][0]}"
|
|
||||||
self.type = sensor_type
|
|
||||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
||||||
|
|
||||||
@property
|
self._attr_name = f"Foobot {device['name']} {description.name}"
|
||||||
def name(self):
|
self._attr_unique_id = f"{device['uuid']}_{description.key}"
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
||||||
return SENSOR_TYPES[self.type][3]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Icon to use in the frontend."""
|
|
||||||
return SENSOR_TYPES[self.type][2]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
try:
|
try:
|
||||||
data = self.foobot_data.data[self.type]
|
data = self.foobot_data.data[self.entity_description.key]
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
data = None
|
data = None
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique id of this entity."""
|
|
||||||
return f"{self._uuid}_{self.type}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement of this entity."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Get the latest data."""
|
"""Get the latest data."""
|
||||||
await self.foobot_data.async_update()
|
await self.foobot_data.async_update()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user