From 4a03d8dc4763011d0c3c05518cacd681b85c5395 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 25 Aug 2021 10:39:59 +0200 Subject: [PATCH] Use EntityDescription - bme280 (#55184) --- homeassistant/components/bme280/__init__.py | 4 +-- homeassistant/components/bme280/const.py | 29 ++++++++++++++--- homeassistant/components/bme280/sensor.py | 36 ++++++++++----------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/bme280/__init__.py b/homeassistant/components/bme280/__init__.py index 8de2b2ffe8b..eca9ac85bc9 100644 --- a/homeassistant/components/bme280/__init__.py +++ b/homeassistant/components/bme280/__init__.py @@ -30,7 +30,7 @@ from .const import ( DEFAULT_SCAN_INTERVAL, DEFAULT_T_STANDBY, DOMAIN, - SENSOR_TYPES, + SENSOR_KEYS, ) CONFIG_SCHEMA = vol.Schema( @@ -54,7 +54,7 @@ CONFIG_SCHEMA = vol.Schema( ): vol.Coerce(float), vol.Optional( CONF_MONITORED_CONDITIONS, default=DEFAULT_MONITORED - ): vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]), + ): vol.All(cv.ensure_list, [vol.In(SENSOR_KEYS)]), vol.Optional( CONF_OVERSAMPLING_TEMP, default=DEFAULT_OVERSAMPLING_TEMP ): vol.Coerce(int), diff --git a/homeassistant/components/bme280/const.py b/homeassistant/components/bme280/const.py index 36753ef0292..e217c0df29e 100644 --- a/homeassistant/components/bme280/const.py +++ b/homeassistant/components/bme280/const.py @@ -1,6 +1,9 @@ """Constants for the BME280 component.""" +from __future__ import annotations + from datetime import timedelta +from homeassistant.components.sensor import SensorEntityDescription from homeassistant.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_PRESSURE, @@ -26,11 +29,27 @@ DEFAULT_SCAN_INTERVAL = 300 SENSOR_TEMP = "temperature" SENSOR_HUMID = "humidity" SENSOR_PRESS = "pressure" -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_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, + ), +) +SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES] DEFAULT_MONITORED = [SENSOR_TEMP, SENSOR_HUMID, SENSOR_PRESS] MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=3) # SPI diff --git a/homeassistant/components/bme280/sensor.py b/homeassistant/components/bme280/sensor.py index de6d6eb0729..f49d8959076 100644 --- a/homeassistant/components/bme280/sensor.py +++ b/homeassistant/components/bme280/sensor.py @@ -6,7 +6,11 @@ from bme280spi import BME280 as BME280_spi # pylint: disable=import-error from i2csense.bme280 import BME280 as BME280_i2c # pylint: disable=import-error import smbus -from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity +from homeassistant.components.sensor import ( + DOMAIN as SENSOR_DOMAIN, + SensorEntity, + SensorEntityDescription, +) from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_SCAN_INTERVAL from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, @@ -98,38 +102,34 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= update_interval=scan_interval, ) await coordinator.async_refresh() - entities = [] - for condition in sensor_conf[CONF_MONITORED_CONDITIONS]: - entities.append( - BME280Sensor( - condition, - name, - coordinator, - ) - ) + monitored_conditions = sensor_conf[CONF_MONITORED_CONDITIONS] + entities = [ + BME280Sensor(name, coordinator, description) + for description in SENSOR_TYPES + if description.key in monitored_conditions + ] async_add_entities(entities, True) class BME280Sensor(CoordinatorEntity, SensorEntity): """Implementation of the BME280 sensor.""" - def __init__(self, sensor_type, name, coordinator): + def __init__(self, name, coordinator, description: SensorEntityDescription): """Initialize the sensor.""" super().__init__(coordinator) - self._attr_name = f"{name} {SENSOR_TYPES[sensor_type][0]}" - self.type = sensor_type - self._attr_native_unit_of_measurement = SENSOR_TYPES[sensor_type][1] - self._attr_device_class = SENSOR_TYPES[sensor_type][2] + self.entity_description = description + self._attr_name = f"{name} {description.name}" @property def native_value(self): """Return the state of the sensor.""" - if self.type == SENSOR_TEMP: + sensor_type = self.entity_description.key + if sensor_type == SENSOR_TEMP: temperature = round(self.coordinator.data.temperature, 1) state = temperature - elif self.type == SENSOR_HUMID: + elif sensor_type == SENSOR_HUMID: state = round(self.coordinator.data.humidity, 1) - elif self.type == SENSOR_PRESS: + elif sensor_type == SENSOR_PRESS: state = round(self.coordinator.data.pressure, 1) return state