Use EntityDescription - envirophat (#54944)

This commit is contained in:
Marc Mueller 2021-08-21 20:53:25 +02:00 committed by GitHub
parent f9ebb29541
commit 4916016648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,11 +1,17 @@
"""Support for Enviro pHAT sensors.""" """Support for Enviro pHAT sensors."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import importlib import importlib
import logging import logging
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_DISPLAY_OPTIONS, CONF_DISPLAY_OPTIONS,
CONF_NAME, CONF_NAME,
@ -24,29 +30,104 @@ CONF_USE_LEDS = "use_leds"
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
SENSOR_TYPES = { SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
"light": ["light", " ", "mdi:weather-sunny", None], SensorEntityDescription(
"light_red": ["light_red", " ", "mdi:invert-colors", None], key="light",
"light_green": ["light_green", " ", "mdi:invert-colors", None], name="light",
"light_blue": ["light_blue", " ", "mdi:invert-colors", None], icon="mdi:weather-sunny",
"accelerometer_x": ["accelerometer_x", "G", "mdi:earth", None], ),
"accelerometer_y": ["accelerometer_y", "G", "mdi:earth", None], SensorEntityDescription(
"accelerometer_z": ["accelerometer_z", "G", "mdi:earth", None], key="light_red",
"magnetometer_x": ["magnetometer_x", " ", "mdi:magnet", None], name="light_red",
"magnetometer_y": ["magnetometer_y", " ", "mdi:magnet", None], icon="mdi:invert-colors",
"magnetometer_z": ["magnetometer_z", " ", "mdi:magnet", None], ),
"temperature": ["temperature", TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE], SensorEntityDescription(
"pressure": ["pressure", PRESSURE_HPA, "mdi:gauge", None], key="light_green",
"voltage_0": ["voltage_0", ELECTRIC_POTENTIAL_VOLT, "mdi:flash", None], name="light_green",
"voltage_1": ["voltage_1", ELECTRIC_POTENTIAL_VOLT, "mdi:flash", None], icon="mdi:invert-colors",
"voltage_2": ["voltage_2", ELECTRIC_POTENTIAL_VOLT, "mdi:flash", None], ),
"voltage_3": ["voltage_3", ELECTRIC_POTENTIAL_VOLT, "mdi:flash", None], SensorEntityDescription(
} key="light_blue",
name="light_blue",
icon="mdi:invert-colors",
),
SensorEntityDescription(
key="accelerometer_x",
name="accelerometer_x",
native_unit_of_measurement="G",
icon="mdi:earth",
),
SensorEntityDescription(
key="accelerometer_y",
name="accelerometer_y",
native_unit_of_measurement="G",
icon="mdi:earth",
),
SensorEntityDescription(
key="accelerometer_z",
name="accelerometer_z",
native_unit_of_measurement="G",
icon="mdi:earth",
),
SensorEntityDescription(
key="magnetometer_x",
name="magnetometer_x",
icon="mdi:magnet",
),
SensorEntityDescription(
key="magnetometer_y",
name="magnetometer_y",
icon="mdi:magnet",
),
SensorEntityDescription(
key="magnetometer_z",
name="magnetometer_z",
icon="mdi:magnet",
),
SensorEntityDescription(
key="temperature",
name="temperature",
native_unit_of_measurement=TEMP_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE,
),
SensorEntityDescription(
key="pressure",
name="pressure",
native_unit_of_measurement=PRESSURE_HPA,
icon="mdi:gauge",
),
SensorEntityDescription(
key="voltage_0",
name="voltage_0",
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
icon="mdi:flash",
),
SensorEntityDescription(
key="voltage_1",
name="voltage_1",
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
icon="mdi:flash",
),
SensorEntityDescription(
key="voltage_2",
name="voltage_2",
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
icon="mdi:flash",
),
SensorEntityDescription(
key="voltage_3",
name="voltage_3",
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
icon="mdi:flash",
),
)
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_DISPLAY_OPTIONS, default=list(SENSOR_TYPES)): [ vol.Required(CONF_DISPLAY_OPTIONS, default=list(SENSOR_KEYS)): [
vol.In(SENSOR_TYPES) vol.In(SENSOR_KEYS)
], ],
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_USE_LEDS, default=False): cv.boolean, vol.Optional(CONF_USE_LEDS, default=False): cv.boolean,
@ -64,85 +145,60 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
data = EnvirophatData(envirophat, config.get(CONF_USE_LEDS)) data = EnvirophatData(envirophat, config.get(CONF_USE_LEDS))
dev = [] display_options = config[CONF_DISPLAY_OPTIONS]
for variable in config[CONF_DISPLAY_OPTIONS]: entities = [
dev.append(EnvirophatSensor(data, variable)) EnvirophatSensor(data, description)
for description in SENSOR_TYPES
add_entities(dev, True) if description.key in display_options
]
add_entities(entities, True)
class EnvirophatSensor(SensorEntity): class EnvirophatSensor(SensorEntity):
"""Representation of an Enviro pHAT sensor.""" """Representation of an Enviro pHAT sensor."""
def __init__(self, data, sensor_types): def __init__(self, data, description: SensorEntityDescription):
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description
self.data = data self.data = data
self._name = SENSOR_TYPES[sensor_types][0]
self._unit_of_measurement = SENSOR_TYPES[sensor_types][1]
self.type = sensor_types
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@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, if any."""
return SENSOR_TYPES[self.type][2]
@property
def native_unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit_of_measurement
def update(self): def update(self):
"""Get the latest data and updates the states.""" """Get the latest data and updates the states."""
self.data.update() self.data.update()
if self.type == "light": sensor_type = self.entity_description.key
self._state = self.data.light if sensor_type == "light":
if self.type == "light_red": self._attr_native_value = self.data.light
self._state = self.data.light_red elif sensor_type == "light_red":
if self.type == "light_green": self._attr_native_value = self.data.light_red
self._state = self.data.light_green elif sensor_type == "light_green":
if self.type == "light_blue": self._attr_native_value = self.data.light_green
self._state = self.data.light_blue elif sensor_type == "light_blue":
if self.type == "accelerometer_x": self._attr_native_value = self.data.light_blue
self._state = self.data.accelerometer_x elif sensor_type == "accelerometer_x":
if self.type == "accelerometer_y": self._attr_native_value = self.data.accelerometer_x
self._state = self.data.accelerometer_y elif sensor_type == "accelerometer_y":
if self.type == "accelerometer_z": self._attr_native_value = self.data.accelerometer_y
self._state = self.data.accelerometer_z elif sensor_type == "accelerometer_z":
if self.type == "magnetometer_x": self._attr_native_value = self.data.accelerometer_z
self._state = self.data.magnetometer_x elif sensor_type == "magnetometer_x":
if self.type == "magnetometer_y": self._attr_native_value = self.data.magnetometer_x
self._state = self.data.magnetometer_y elif sensor_type == "magnetometer_y":
if self.type == "magnetometer_z": self._attr_native_value = self.data.magnetometer_y
self._state = self.data.magnetometer_z elif sensor_type == "magnetometer_z":
if self.type == "temperature": self._attr_native_value = self.data.magnetometer_z
self._state = self.data.temperature elif sensor_type == "temperature":
if self.type == "pressure": self._attr_native_value = self.data.temperature
self._state = self.data.pressure elif sensor_type == "pressure":
if self.type == "voltage_0": self._attr_native_value = self.data.pressure
self._state = self.data.voltage_0 elif sensor_type == "voltage_0":
if self.type == "voltage_1": self._attr_native_value = self.data.voltage_0
self._state = self.data.voltage_1 elif sensor_type == "voltage_1":
if self.type == "voltage_2": self._attr_native_value = self.data.voltage_1
self._state = self.data.voltage_2 elif sensor_type == "voltage_2":
if self.type == "voltage_3": self._attr_native_value = self.data.voltage_2
self._state = self.data.voltage_3 elif sensor_type == "voltage_3":
self._attr_native_value = self.data.voltage_3
class EnvirophatData: class EnvirophatData: