2019-04-03 15:40:03 +00:00
|
|
|
"""Support for 1-Wire environment sensors."""
|
2019-12-09 13:26:53 +00:00
|
|
|
from glob import glob
|
|
|
|
import logging
|
2015-03-29 12:38:10 +00:00
|
|
|
import os
|
|
|
|
import time
|
2017-08-08 18:21:33 +00:00
|
|
|
|
2016-09-06 20:50:02 +00:00
|
|
|
import voluptuous as vol
|
2017-08-08 18:21:33 +00:00
|
|
|
|
2019-12-09 13:26:53 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import TEMP_CELSIUS
|
2016-09-06 20:50:02 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-08-08 18:21:33 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-09-06 20:50:02 +00:00
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MOUNT_DIR = "mount_dir"
|
|
|
|
CONF_NAMES = "names"
|
|
|
|
|
|
|
|
DEFAULT_MOUNT_DIR = "/sys/bus/w1/devices/"
|
|
|
|
DEVICE_SENSORS = {
|
|
|
|
"10": {"temperature": "temperature"},
|
|
|
|
"12": {"temperature": "TAI8570/temperature", "pressure": "TAI8570/pressure"},
|
|
|
|
"22": {"temperature": "temperature"},
|
|
|
|
"26": {
|
|
|
|
"temperature": "temperature",
|
|
|
|
"humidity": "humidity",
|
|
|
|
"pressure": "B1-R1-A/pressure",
|
|
|
|
"illuminance": "S3-R1-A/illuminance",
|
|
|
|
},
|
|
|
|
"28": {"temperature": "temperature"},
|
|
|
|
"3B": {"temperature": "temperature"},
|
|
|
|
"42": {"temperature": "temperature"},
|
|
|
|
}
|
2017-09-14 05:14:38 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"temperature": ["temperature", TEMP_CELSIUS],
|
|
|
|
"humidity": ["humidity", "%"],
|
|
|
|
"pressure": ["pressure", "mb"],
|
|
|
|
"illuminance": ["illuminance", "lux"],
|
2017-09-14 05:14:38 +00:00
|
|
|
}
|
2016-09-06 20:50:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAMES): {cv.string: cv.string},
|
|
|
|
vol.Optional(CONF_MOUNT_DIR, default=DEFAULT_MOUNT_DIR): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2015-03-29 12:38:10 +00:00
|
|
|
|
2015-09-22 10:53:44 +00:00
|
|
|
|
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 one wire Sensors."""
|
2016-09-06 20:50:02 +00:00
|
|
|
base_dir = config.get(CONF_MOUNT_DIR)
|
2017-09-14 05:14:38 +00:00
|
|
|
devs = []
|
|
|
|
device_names = {}
|
2019-07-31 19:25:30 +00:00
|
|
|
if "names" in config:
|
|
|
|
if isinstance(config["names"], dict):
|
|
|
|
device_names = config["names"]
|
2017-09-14 05:14:38 +00:00
|
|
|
|
2017-02-19 09:59:44 +00:00
|
|
|
if base_dir == DEFAULT_MOUNT_DIR:
|
2017-09-14 05:14:38 +00:00
|
|
|
for device_family in DEVICE_SENSORS:
|
2019-07-31 19:25:30 +00:00
|
|
|
for device_folder in glob(os.path.join(base_dir, device_family + "[.-]*")):
|
2017-09-14 05:14:38 +00:00
|
|
|
sensor_id = os.path.split(device_folder)[1]
|
2019-07-31 19:25:30 +00:00
|
|
|
device_file = os.path.join(device_folder, "w1_slave")
|
|
|
|
devs.append(
|
|
|
|
OneWireDirect(
|
|
|
|
device_names.get(sensor_id, sensor_id),
|
|
|
|
device_file,
|
|
|
|
"temperature",
|
|
|
|
)
|
|
|
|
)
|
2017-02-19 09:59:44 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
for family_file_path in glob(os.path.join(base_dir, "*", "family")):
|
2018-01-31 10:30:48 +00:00
|
|
|
with open(family_file_path, "r") as family_file:
|
|
|
|
family = family_file.read()
|
2017-09-14 05:14:38 +00:00
|
|
|
if family in DEVICE_SENSORS:
|
|
|
|
for sensor_key, sensor_value in DEVICE_SENSORS[family].items():
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor_id = os.path.split(os.path.split(family_file_path)[0])[1]
|
2017-09-14 05:14:38 +00:00
|
|
|
device_file = os.path.join(
|
2019-07-31 19:25:30 +00:00
|
|
|
os.path.split(family_file_path)[0], sensor_value
|
|
|
|
)
|
|
|
|
devs.append(
|
|
|
|
OneWireOWFS(
|
|
|
|
device_names.get(sensor_id, sensor_id),
|
|
|
|
device_file,
|
|
|
|
sensor_key,
|
|
|
|
)
|
|
|
|
)
|
2017-09-14 05:14:38 +00:00
|
|
|
|
|
|
|
if devs == []:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"No onewire sensor found. Check if dtoverlay=w1-gpio "
|
|
|
|
"is in your /boot/config.txt. "
|
|
|
|
"Check the mount_dir parameter if it's defined"
|
|
|
|
)
|
2015-03-29 12:38:10 +00:00
|
|
|
return
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devs, True)
|
2015-03-29 12:38:10 +00:00
|
|
|
|
2015-09-22 11:27:08 +00:00
|
|
|
|
2015-03-29 12:38:10 +00:00
|
|
|
class OneWire(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of an One wire Sensor."""
|
2015-03-29 12:38:10 +00:00
|
|
|
|
2017-09-14 05:14:38 +00:00
|
|
|
def __init__(self, name, device_file, sensor_type):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = name + " " + sensor_type.capitalize()
|
2015-03-29 12:38:10 +00:00
|
|
|
self._device_file = device_file
|
2017-09-14 05:14:38 +00:00
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
2017-08-08 18:21:33 +00:00
|
|
|
self._state = None
|
2015-03-29 12:38:10 +00:00
|
|
|
|
2017-09-14 05:14:38 +00:00
|
|
|
def _read_value_raw(self):
|
|
|
|
"""Read the value as it is returned by the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(self._device_file, "r") as ds_device_file:
|
2017-10-04 14:35:58 +00:00
|
|
|
lines = ds_device_file.readlines()
|
2015-03-29 12:38:10 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-03-29 12:38:10 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-01-27 05:07:53 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit the value is expressed in."""
|
2017-09-14 05:14:38 +00:00
|
|
|
return self._unit_of_measurement
|
2016-01-27 05:07:53 +00:00
|
|
|
|
2017-10-04 14:35:58 +00:00
|
|
|
|
|
|
|
class OneWireDirect(OneWire):
|
|
|
|
"""Implementation of an One wire Sensor directly connected to RPI GPIO."""
|
|
|
|
|
2016-01-27 05:07:53 +00:00
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from the device."""
|
2017-09-14 05:14:38 +00:00
|
|
|
value = None
|
2017-10-04 14:35:58 +00:00
|
|
|
lines = self._read_value_raw()
|
2019-07-31 19:25:30 +00:00
|
|
|
while lines[0].strip()[-3:] != "YES":
|
2017-10-04 14:35:58 +00:00
|
|
|
time.sleep(0.2)
|
2017-09-14 05:14:38 +00:00
|
|
|
lines = self._read_value_raw()
|
2019-07-31 19:25:30 +00:00
|
|
|
equals_pos = lines[1].find("t=")
|
2017-10-04 14:35:58 +00:00
|
|
|
if equals_pos != -1:
|
2019-07-31 19:25:30 +00:00
|
|
|
value_string = lines[1][equals_pos + 2 :]
|
2017-10-04 14:35:58 +00:00
|
|
|
value = round(float(value_string) / 1000.0, 1)
|
|
|
|
self._state = value
|
|
|
|
|
|
|
|
|
|
|
|
class OneWireOWFS(OneWire):
|
|
|
|
"""Implementation of an One wire Sensor through owfs."""
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from the device."""
|
|
|
|
value = None
|
|
|
|
try:
|
|
|
|
value_read = self._read_value_raw()
|
|
|
|
if len(value_read) == 1:
|
|
|
|
value = round(float(value_read[0]), 1)
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.warning("Invalid value read from %s", self._device_file)
|
|
|
|
except FileNotFoundError:
|
|
|
|
_LOGGER.warning("Cannot read from sensor: %s", self._device_file)
|
2016-06-29 01:39:16 +00:00
|
|
|
|
2017-09-14 05:14:38 +00:00
|
|
|
self._state = value
|