Use EntityDescription - bme680 (#55185)

This commit is contained in:
Marc Mueller 2021-08-25 10:36:41 +02:00 committed by GitHub
parent 839b9563ad
commit 5e44498f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
"""Support for BME680 Sensor over SMBus."""
from __future__ import annotations
import logging
import threading
from time import monotonic, sleep
@ -7,7 +9,11 @@ import bme680 # pylint: disable=import-error
from smbus import SMBus
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 (
CONF_MONITORED_CONDITIONS,
CONF_NAME,
@ -54,13 +60,37 @@ SENSOR_HUMID = "humidity"
SENSOR_PRESS = "pressure"
SENSOR_GAS = "gas"
SENSOR_AQ = "airquality"
SENSOR_TYPES = {
SENSOR_TEMP: ["Temperature", TEMP_CELSIUS, DEVICE_CLASS_TEMPERATURE],
SENSOR_HUMID: ["Humidity", PERCENTAGE, DEVICE_CLASS_HUMIDITY],
SENSOR_PRESS: ["Pressure", "mb", DEVICE_CLASS_PRESSURE],
SENSOR_GAS: ["Gas Resistance", "Ohms", None],
SENSOR_AQ: ["Air Quality", PERCENTAGE, None],
}
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key=SENSOR_TEMP,
name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS,
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]
OVERSAMPLING_VALUES = {0, 1, 2, 4, 8, 16}
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_I2C_ADDRESS, default=DEFAULT_I2C_ADDRESS): cv.positive_int,
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(
@ -115,12 +145,14 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
if sensor_handler is None:
return
dev = []
for variable in config[CONF_MONITORED_CONDITIONS]:
dev.append(BME680Sensor(sensor_handler, variable, name))
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
entities = [
BME680Sensor(sensor_handler, name, description)
for description in SENSOR_TYPES
if description.key in monitored_conditions
]
async_add_entities(dev)
return
async_add_entities(entities)
def _setup_bme680(config):
@ -317,30 +349,29 @@ class BME680Handler:
class BME680Sensor(SensorEntity):
"""Implementation of the BME680 sensor."""
def __init__(self, bme680_client, sensor_type, name):
def __init__(self, bme680_client, name, description: SensorEntityDescription):
"""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.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):
"""Get the latest data from the BME680 and update the states."""
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.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)
elif self.type == SENSOR_PRESS:
elif sensor_type == SENSOR_PRESS:
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(
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
if aq_score is not None:
self._attr_native_value = round(aq_score, 1)