-
-
Notifications
You must be signed in to change notification settings - Fork 28.6k
/
sensor.py
147 lines (112 loc) · 4.24 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Support for Sensirion SHT31 temperature and humidity sensor."""
from datetime import timedelta
import logging
import math
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import TEMP_CELSIUS, CONF_NAME, CONF_MONITORED_CONDITIONS
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.temperature import display_temp
from homeassistant.const import PRECISION_TENTHS
from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
CONF_I2C_ADDRESS = "i2c_address"
DEFAULT_NAME = "SHT31"
DEFAULT_I2C_ADDRESS = 0x44
SENSOR_TEMPERATURE = "temperature"
SENSOR_HUMIDITY = "humidity"
SENSOR_TYPES = (SENSOR_TEMPERATURE, SENSOR_HUMIDITY)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_I2C_ADDRESS, default=DEFAULT_I2C_ADDRESS): vol.All(
vol.Coerce(int), vol.Range(min=0x44, max=0x45)
),
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the sensor platform."""
from Adafruit_SHT31 import SHT31
i2c_address = config.get(CONF_I2C_ADDRESS)
sensor = SHT31(address=i2c_address)
try:
if sensor.read_status() is None:
raise ValueError("CRC error while reading SHT31 status")
except (OSError, ValueError):
_LOGGER.error("SHT31 sensor not detected at address %s", hex(i2c_address))
return
sensor_client = SHTClient(sensor)
sensor_classes = {
SENSOR_TEMPERATURE: SHTSensorTemperature,
SENSOR_HUMIDITY: SHTSensorHumidity,
}
devs = []
for sensor_type, sensor_class in sensor_classes.items():
name = "{} {}".format(config.get(CONF_NAME), sensor_type.capitalize())
devs.append(sensor_class(sensor_client, name))
add_entities(devs)
class SHTClient:
"""Get the latest data from the SHT sensor."""
def __init__(self, adafruit_sht):
"""Initialize the sensor."""
self.adafruit_sht = adafruit_sht
self.temperature = None
self.humidity = None
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the latest data from the SHT sensor."""
temperature, humidity = self.adafruit_sht.read_temperature_humidity()
if math.isnan(temperature) or math.isnan(humidity):
_LOGGER.warning("Bad sample from sensor SHT31")
return
self.temperature = temperature
self.humidity = humidity
class SHTSensor(Entity):
"""An abstract SHTSensor, can be either temperature or humidity."""
def __init__(self, sensor, name):
"""Initialize the sensor."""
self._sensor = sensor
self._name = name
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
def update(self):
"""Fetch temperature and humidity from the sensor."""
self._sensor.update()
class SHTSensorTemperature(SHTSensor):
"""Representation of a temperature sensor."""
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self.hass.config.units.temperature_unit
def update(self):
"""Fetch temperature from the sensor."""
super().update()
temp_celsius = self._sensor.temperature
if temp_celsius is not None:
self._state = display_temp(
self.hass, temp_celsius, TEMP_CELSIUS, PRECISION_TENTHS
)
class SHTSensorHumidity(SHTSensor):
"""Representation of a humidity sensor."""
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return "%"
def update(self):
"""Fetch humidity from the sensor."""
super().update()
humidity = self._sensor.humidity
if humidity is not None:
self._state = round(humidity)