2016-01-27 06:28:18 +00:00
|
|
|
"""
|
2016-09-06 20:50:02 +00:00
|
|
|
Support for 1-Wire temperature sensors.
|
2016-01-27 06:28:18 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.onewire/
|
|
|
|
"""
|
2015-03-29 12:38:10 +00:00
|
|
|
import os
|
|
|
|
import time
|
2016-09-06 20:50:02 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
from glob import glob
|
2016-09-06 20:50:02 +00:00
|
|
|
import voluptuous as vol
|
2016-01-27 05:07:53 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-09-06 20:50:02 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.const import STATE_UNKNOWN, TEMP_CELSIUS
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
|
|
|
|
CONF_MOUNT_DIR = 'mount_dir'
|
|
|
|
CONF_NAMES = 'names'
|
|
|
|
DEFAULT_MOUNT_DIR = '/sys/bus/w1/devices/'
|
|
|
|
DEVICE_FAMILIES = ('10', '22', '28', '3B', '42')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-09-22 10:53:44 +00:00
|
|
|
|
2015-03-29 12:38:10 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Setup the one wire Sensors."""
|
2016-09-06 20:50:02 +00:00
|
|
|
base_dir = config.get(CONF_MOUNT_DIR)
|
2016-06-29 01:39:16 +00:00
|
|
|
sensor_ids = []
|
|
|
|
device_files = []
|
2016-09-06 20:50:02 +00:00
|
|
|
for device_family in DEVICE_FAMILIES:
|
|
|
|
for device_folder in glob(os.path.join(base_dir, device_family +
|
|
|
|
'[.-]*')):
|
|
|
|
sensor_ids.append(os.path.split(device_folder)[1])
|
|
|
|
if base_dir == DEFAULT_MOUNT_DIR:
|
|
|
|
device_files.append(os.path.join(device_folder, 'w1_slave'))
|
|
|
|
else:
|
|
|
|
device_files.append(os.path.join(device_folder, 'temperature'))
|
2016-06-29 01:39:16 +00:00
|
|
|
|
|
|
|
if device_files == []:
|
2016-09-06 20:50:02 +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
|
|
|
|
|
|
|
|
devs = []
|
2016-06-29 01:39:16 +00:00
|
|
|
names = sensor_ids
|
2015-03-29 12:38:10 +00:00
|
|
|
|
2015-09-16 12:17:41 +00:00
|
|
|
for key in config.keys():
|
2015-09-22 10:32:45 +00:00
|
|
|
if key == "names":
|
2015-09-22 10:53:44 +00:00
|
|
|
# only one name given
|
2015-09-22 10:32:45 +00:00
|
|
|
if isinstance(config['names'], str):
|
|
|
|
names = [config['names']]
|
2015-09-22 10:53:44 +00:00
|
|
|
# map names and sensors in given order
|
2015-09-22 10:32:45 +00:00
|
|
|
elif isinstance(config['names'], list):
|
|
|
|
names = config['names']
|
2015-09-22 10:53:44 +00:00
|
|
|
# map names to ids.
|
2015-09-22 10:32:45 +00:00
|
|
|
elif isinstance(config['names'], dict):
|
2015-09-22 11:27:08 +00:00
|
|
|
names = []
|
2016-06-29 01:39:16 +00:00
|
|
|
for sensor_id in sensor_ids:
|
2015-09-22 11:27:08 +00:00
|
|
|
names.append(config['names'].get(sensor_id, sensor_id))
|
2016-06-29 01:39:16 +00:00
|
|
|
for device_file, name in zip(device_files, names):
|
2016-01-27 05:07:53 +00:00
|
|
|
devs.append(OneWire(name, device_file))
|
2015-03-29 12:38:10 +00:00
|
|
|
add_devices(devs)
|
|
|
|
|
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
|
|
|
|
2016-01-27 05:07:53 +00:00
|
|
|
def __init__(self, name, device_file):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-03-29 12:38:10 +00:00
|
|
|
self._name = name
|
|
|
|
self._device_file = device_file
|
2016-01-27 05:07:53 +00:00
|
|
|
self._state = STATE_UNKNOWN
|
|
|
|
self.update()
|
2015-03-29 12:38:10 +00:00
|
|
|
|
|
|
|
def _read_temp_raw(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Read the temperature as it is returned by the sensor."""
|
2015-09-22 10:32:45 +00:00
|
|
|
ds_device_file = open(self._device_file, 'r')
|
|
|
|
lines = ds_device_file.readlines()
|
|
|
|
ds_device_file.close()
|
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."""
|
2016-04-20 03:30:44 +00:00
|
|
|
return TEMP_CELSIUS
|
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."""
|
2016-06-29 01:39:16 +00:00
|
|
|
temp = -99
|
2016-09-06 20:50:02 +00:00
|
|
|
if self._device_file.startswith(DEFAULT_MOUNT_DIR):
|
2015-03-29 12:38:10 +00:00
|
|
|
lines = self._read_temp_raw()
|
2016-06-29 01:39:16 +00:00
|
|
|
while lines[0].strip()[-3:] != 'YES':
|
|
|
|
time.sleep(0.2)
|
|
|
|
lines = self._read_temp_raw()
|
|
|
|
equals_pos = lines[1].find('t=')
|
|
|
|
if equals_pos != -1:
|
|
|
|
temp_string = lines[1][equals_pos+2:]
|
|
|
|
temp = round(float(temp_string) / 1000.0, 1)
|
|
|
|
else:
|
2016-09-06 20:50:02 +00:00
|
|
|
try:
|
|
|
|
ds_device_file = open(self._device_file, 'r')
|
|
|
|
temp_read = ds_device_file.readlines()
|
|
|
|
ds_device_file.close()
|
|
|
|
if len(temp_read) == 1:
|
2016-06-29 01:39:16 +00:00
|
|
|
temp = round(float(temp_read[0]), 1)
|
2016-09-06 20:50:02 +00:00
|
|
|
except ValueError:
|
|
|
|
_LOGGER.warning('Invalid temperature value read from ' +
|
|
|
|
self._device_file)
|
|
|
|
except FileNotFoundError:
|
|
|
|
_LOGGER.warning('Cannot read from sensor: ' +
|
|
|
|
self._device_file)
|
2016-06-29 01:39:16 +00:00
|
|
|
|
|
|
|
if temp < -55 or temp > 125:
|
|
|
|
return
|
|
|
|
self._state = temp
|