2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Sense HAT sensors."""
|
2021-08-23 20:35:59 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-12-11 23:46:55 +00:00
|
|
|
from datetime import timedelta
|
2019-12-01 05:23:09 +00:00
|
|
|
import logging
|
2020-11-23 19:56:50 +00:00
|
|
|
from pathlib import Path
|
2016-12-11 23:46:55 +00:00
|
|
|
|
2019-12-01 05:23:09 +00:00
|
|
|
from sense_hat import SenseHat
|
2016-12-11 23:46:55 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-08-23 20:35:59 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2020-02-28 19:46:48 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_DISPLAY_OPTIONS,
|
|
|
|
CONF_NAME,
|
2021-07-12 16:53:52 +00:00
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-02-28 19:46:48 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2016-12-11 23:46:55 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "sensehat"
|
|
|
|
CONF_IS_HAT_ATTACHED = "is_hat_attached"
|
2016-12-11 23:46:55 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
|
|
|
|
2021-08-23 20:35:59 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="temperature",
|
|
|
|
name="temperature",
|
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="humidity",
|
|
|
|
name="humidity",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="pressure",
|
|
|
|
name="pressure",
|
|
|
|
native_unit_of_measurement="mb",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
2016-12-11 23:46:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
2021-08-23 20:35:59 +00:00
|
|
|
vol.Required(CONF_DISPLAY_OPTIONS, default=SENSOR_KEYS): [vol.In(SENSOR_KEYS)],
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_IS_HAT_ATTACHED, default=True): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2016-12-11 23:46:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_cpu_temp():
|
|
|
|
"""Get CPU temperature."""
|
2021-09-18 11:52:59 +00:00
|
|
|
t_cpu = (
|
|
|
|
Path("/sys/class/thermal/thermal_zone0/temp")
|
|
|
|
.read_text(encoding="utf-8")
|
|
|
|
.strip()
|
|
|
|
)
|
2020-11-23 19:56:50 +00:00
|
|
|
return float(t_cpu) * 0.001
|
2016-12-11 23:46:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_average(temp_base):
|
|
|
|
"""Use moving average to get better readings."""
|
|
|
|
if not hasattr(get_average, "temp"):
|
|
|
|
get_average.temp = [temp_base, temp_base, temp_base]
|
|
|
|
get_average.temp[2] = get_average.temp[1]
|
|
|
|
get_average.temp[1] = get_average.temp[0]
|
|
|
|
get_average.temp[0] = temp_base
|
2019-07-31 19:25:30 +00:00
|
|
|
temp_avg = (get_average.temp[0] + get_average.temp[1] + get_average.temp[2]) / 3
|
2016-12-11 23:46:55 +00:00
|
|
|
return temp_avg
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Sense HAT sensor platform."""
|
2017-02-15 10:11:55 +00:00
|
|
|
data = SenseHatData(config.get(CONF_IS_HAT_ATTACHED))
|
2021-08-23 20:35:59 +00:00
|
|
|
display_options = config[CONF_DISPLAY_OPTIONS]
|
|
|
|
entities = [
|
|
|
|
SenseHatSensor(data, description)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key in display_options
|
|
|
|
]
|
2016-12-11 23:46:55 +00:00
|
|
|
|
2021-08-23 20:35:59 +00:00
|
|
|
add_entities(entities, True)
|
2016-12-11 23:46:55 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class SenseHatSensor(SensorEntity):
|
2016-12-18 10:39:33 +00:00
|
|
|
"""Representation of a Sense HAT sensor."""
|
2016-12-11 23:46:55 +00:00
|
|
|
|
2021-08-23 20:35:59 +00:00
|
|
|
def __init__(self, data, description: SensorEntityDescription):
|
2016-12-11 23:46:55 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-08-23 20:35:59 +00:00
|
|
|
self.entity_description = description
|
2016-12-11 23:46:55 +00:00
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the states."""
|
|
|
|
self.data.update()
|
|
|
|
if not self.data.humidity:
|
2016-12-18 10:39:33 +00:00
|
|
|
_LOGGER.error("Don't receive data")
|
2016-12-11 23:46:55 +00:00
|
|
|
return
|
|
|
|
|
2021-08-23 20:35:59 +00:00
|
|
|
sensor_type = self.entity_description.key
|
|
|
|
if sensor_type == "temperature":
|
|
|
|
self._attr_native_value = self.data.temperature
|
|
|
|
elif sensor_type == "humidity":
|
|
|
|
self._attr_native_value = self.data.humidity
|
|
|
|
elif sensor_type == "pressure":
|
|
|
|
self._attr_native_value = self.data.pressure
|
2016-12-11 23:46:55 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class SenseHatData:
|
2016-12-11 23:46:55 +00:00
|
|
|
"""Get the latest data and update."""
|
|
|
|
|
2017-02-15 10:11:55 +00:00
|
|
|
def __init__(self, is_hat_attached):
|
2016-12-11 23:46:55 +00:00
|
|
|
"""Initialize the data object."""
|
|
|
|
self.temperature = None
|
|
|
|
self.humidity = None
|
|
|
|
self.pressure = None
|
2017-02-15 10:11:55 +00:00
|
|
|
self.is_hat_attached = is_hat_attached
|
2016-12-11 23:46:55 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-12-18 10:39:33 +00:00
|
|
|
"""Get the latest data from Sense HAT."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-12-11 23:46:55 +00:00
|
|
|
sense = SenseHat()
|
|
|
|
temp_from_h = sense.get_temperature_from_humidity()
|
|
|
|
temp_from_p = sense.get_temperature_from_pressure()
|
2016-12-18 10:39:33 +00:00
|
|
|
t_total = (temp_from_h + temp_from_p) / 2
|
2017-02-15 10:11:55 +00:00
|
|
|
|
|
|
|
if self.is_hat_attached:
|
|
|
|
t_cpu = get_cpu_temp()
|
|
|
|
t_correct = t_total - ((t_cpu - t_total) / 1.5)
|
|
|
|
t_correct = get_average(t_correct)
|
|
|
|
else:
|
|
|
|
t_correct = get_average(t_total)
|
|
|
|
|
2016-12-11 23:46:55 +00:00
|
|
|
self.temperature = t_correct
|
|
|
|
self.humidity = sense.get_humidity()
|
|
|
|
self.pressure = sense.get_pressure()
|