mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Use EntityDescription - bme680 (#55185)
This commit is contained in:
parent
839b9563ad
commit
5e44498f1c
@ -1,4 +1,6 @@
|
|||||||
"""Support for BME680 Sensor over SMBus."""
|
"""Support for BME680 Sensor over SMBus."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
from time import monotonic, sleep
|
from time import monotonic, sleep
|
||||||
@ -7,7 +9,11 @@ import bme680 # pylint: disable=import-error
|
|||||||
from smbus import SMBus
|
from smbus import SMBus
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
PLATFORM_SCHEMA,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_MONITORED_CONDITIONS,
|
CONF_MONITORED_CONDITIONS,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
@ -54,13 +60,37 @@ SENSOR_HUMID = "humidity"
|
|||||||
SENSOR_PRESS = "pressure"
|
SENSOR_PRESS = "pressure"
|
||||||
SENSOR_GAS = "gas"
|
SENSOR_GAS = "gas"
|
||||||
SENSOR_AQ = "airquality"
|
SENSOR_AQ = "airquality"
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
SENSOR_TEMP: ["Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
|
SensorEntityDescription(
|
||||||
SENSOR_HUMID: ["Humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
|
key=SENSOR_TEMP,
|
||||||
SENSOR_PRESS: ["Pressure", "mb", DEVICE_CLASS_PRESSURE],
|
name="Temperature",
|
||||||
SENSOR_GAS: ["Gas Resistance", "Ohms", None],
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
SENSOR_AQ: ["Air Quality", PERCENTAGE, None],
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
}
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_HUMID,
|
||||||
|
name="Humidity",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_PRESS,
|
||||||
|
name="Pressure",
|
||||||
|
native_unit_of_measurement="mb",
|
||||||
|
device_class=DEVICE_CLASS_PRESSURE,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_GAS,
|
||||||
|
name="Gas Resistance",
|
||||||
|
native_unit_of_measurement="Ohms",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_AQ,
|
||||||
|
name="Air Quality",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||||
DEFAULT_MONITORED = [SENSOR_TEMP, SENSOR_HUMID, SENSOR_PRESS, SENSOR_AQ]
|
DEFAULT_MONITORED = [SENSOR_TEMP, SENSOR_HUMID, SENSOR_PRESS, SENSOR_AQ]
|
||||||
OVERSAMPLING_VALUES = {0, 1, 2, 4, 8, 16}
|
OVERSAMPLING_VALUES = {0, 1, 2, 4, 8, 16}
|
||||||
FILTER_VALUES = {0, 1, 3, 7, 15, 31, 63, 127}
|
FILTER_VALUES = {0, 1, 3, 7, 15, 31, 63, 127}
|
||||||
@ -70,7 +100,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_I2C_ADDRESS, default=DEFAULT_I2C_ADDRESS): cv.positive_int,
|
vol.Optional(CONF_I2C_ADDRESS, default=DEFAULT_I2C_ADDRESS): cv.positive_int,
|
||||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=DEFAULT_MONITORED): vol.All(
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=DEFAULT_MONITORED): vol.All(
|
||||||
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||||
),
|
),
|
||||||
vol.Optional(CONF_I2C_BUS, default=DEFAULT_I2C_BUS): cv.positive_int,
|
vol.Optional(CONF_I2C_BUS, default=DEFAULT_I2C_BUS): cv.positive_int,
|
||||||
vol.Optional(
|
vol.Optional(
|
||||||
@ -115,12 +145,14 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
if sensor_handler is None:
|
if sensor_handler is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
dev = []
|
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||||
for variable in config[CONF_MONITORED_CONDITIONS]:
|
entities = [
|
||||||
dev.append(BME680Sensor(sensor_handler, variable, name))
|
BME680Sensor(sensor_handler, name, description)
|
||||||
|
for description in SENSOR_TYPES
|
||||||
|
if description.key in monitored_conditions
|
||||||
|
]
|
||||||
|
|
||||||
async_add_entities(dev)
|
async_add_entities(entities)
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def _setup_bme680(config):
|
def _setup_bme680(config):
|
||||||
@ -317,30 +349,29 @@ class BME680Handler:
|
|||||||
class BME680Sensor(SensorEntity):
|
class BME680Sensor(SensorEntity):
|
||||||
"""Implementation of the BME680 sensor."""
|
"""Implementation of the BME680 sensor."""
|
||||||
|
|
||||||
def __init__(self, bme680_client, sensor_type, name):
|
def __init__(self, bme680_client, name, description: SensorEntityDescription):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._attr_name = f"{name} {SENSOR_TYPES[sensor_type][0]}"
|
self.entity_description = description
|
||||||
|
self._attr_name = f"{name} {description.name}"
|
||||||
self.bme680_client = bme680_client
|
self.bme680_client = bme680_client
|
||||||
self.type = sensor_type
|
|
||||||
self._attr_native_unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
||||||
self._attr_device_class = SENSOR_TYPES[sensor_type][2]
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Get the latest data from the BME680 and update the states."""
|
"""Get the latest data from the BME680 and update the states."""
|
||||||
await self.hass.async_add_executor_job(self.bme680_client.update)
|
await self.hass.async_add_executor_job(self.bme680_client.update)
|
||||||
if self.type == SENSOR_TEMP:
|
sensor_type = self.entity_description.key
|
||||||
|
if sensor_type == SENSOR_TEMP:
|
||||||
self._attr_native_value = round(
|
self._attr_native_value = round(
|
||||||
self.bme680_client.sensor_data.temperature, 1
|
self.bme680_client.sensor_data.temperature, 1
|
||||||
)
|
)
|
||||||
elif self.type == SENSOR_HUMID:
|
elif sensor_type == SENSOR_HUMID:
|
||||||
self._attr_native_value = round(self.bme680_client.sensor_data.humidity, 1)
|
self._attr_native_value = round(self.bme680_client.sensor_data.humidity, 1)
|
||||||
elif self.type == SENSOR_PRESS:
|
elif sensor_type == SENSOR_PRESS:
|
||||||
self._attr_native_value = round(self.bme680_client.sensor_data.pressure, 1)
|
self._attr_native_value = round(self.bme680_client.sensor_data.pressure, 1)
|
||||||
elif self.type == SENSOR_GAS:
|
elif sensor_type == SENSOR_GAS:
|
||||||
self._attr_native_value = int(
|
self._attr_native_value = int(
|
||||||
round(self.bme680_client.sensor_data.gas_resistance, 0)
|
round(self.bme680_client.sensor_data.gas_resistance, 0)
|
||||||
)
|
)
|
||||||
elif self.type == SENSOR_AQ:
|
elif sensor_type == SENSOR_AQ:
|
||||||
aq_score = self.bme680_client.sensor_data.air_quality
|
aq_score = self.bme680_client.sensor_data.air_quality
|
||||||
if aq_score is not None:
|
if aq_score is not None:
|
||||||
self._attr_native_value = round(aq_score, 1)
|
self._attr_native_value = round(aq_score, 1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user