Support more types of 1wire sensors and bus masters (#2384)
* Support more types of 1wire sensors and bus masters - Added support for DS18S20, DS1822, DS1825 and DS28EA00 temperature sensors - Added support for bus masters which use fuse to mount device tree. Mount can be specified by 'mount_dir' configuration parameter. * Correct the lint problem
This commit is contained in:
parent
31d2a5d2d1
commit
78e7e17484
1 changed files with 38 additions and 22 deletions
|
@ -12,21 +12,24 @@ from glob import glob
|
||||||
from homeassistant.const import STATE_UNKNOWN, TEMP_CELSIUS
|
from homeassistant.const import STATE_UNKNOWN, TEMP_CELSIUS
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
BASE_DIR = '/sys/bus/w1/devices/'
|
|
||||||
DEVICE_FOLDERS = glob(os.path.join(BASE_DIR, '28*'))
|
|
||||||
SENSOR_IDS = []
|
|
||||||
DEVICE_FILES = []
|
|
||||||
for device_folder in DEVICE_FOLDERS:
|
|
||||||
SENSOR_IDS.append(os.path.split(device_folder)[1])
|
|
||||||
DEVICE_FILES.append(os.path.join(device_folder, 'w1_slave'))
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the one wire Sensors."""
|
"""Setup the one wire Sensors."""
|
||||||
if DEVICE_FILES == []:
|
base_dir = config.get('mount_dir', '/sys/bus/w1/devices/')
|
||||||
|
device_folders = glob(os.path.join(base_dir, '[10,22,28,3B,42]*'))
|
||||||
|
sensor_ids = []
|
||||||
|
device_files = []
|
||||||
|
for device_folder in device_folders:
|
||||||
|
sensor_ids.append(os.path.split(device_folder)[1])
|
||||||
|
if base_dir.startswith('/sys/bus/w1/devices'):
|
||||||
|
device_files.append(os.path.join(device_folder, 'w1_slave'))
|
||||||
|
else:
|
||||||
|
device_files.append(os.path.join(device_folder, 'temperature'))
|
||||||
|
|
||||||
|
if device_files == []:
|
||||||
_LOGGER.error('No onewire sensor found.')
|
_LOGGER.error('No onewire sensor found.')
|
||||||
_LOGGER.error('Check if dtoverlay=w1-gpio,gpiopin=4.')
|
_LOGGER.error('Check if dtoverlay=w1-gpio,gpiopin=4.')
|
||||||
_LOGGER.error('is in your /boot/config.txt and')
|
_LOGGER.error('is in your /boot/config.txt and')
|
||||||
|
@ -34,7 +37,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
return
|
return
|
||||||
|
|
||||||
devs = []
|
devs = []
|
||||||
names = SENSOR_IDS
|
names = sensor_ids
|
||||||
|
|
||||||
for key in config.keys():
|
for key in config.keys():
|
||||||
if key == "names":
|
if key == "names":
|
||||||
|
@ -47,9 +50,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
# map names to ids.
|
# map names to ids.
|
||||||
elif isinstance(config['names'], dict):
|
elif isinstance(config['names'], dict):
|
||||||
names = []
|
names = []
|
||||||
for sensor_id in SENSOR_IDS:
|
for sensor_id in sensor_ids:
|
||||||
names.append(config['names'].get(sensor_id, sensor_id))
|
names.append(config['names'].get(sensor_id, sensor_id))
|
||||||
for device_file, name in zip(DEVICE_FILES, names):
|
for device_file, name in zip(device_files, names):
|
||||||
devs.append(OneWire(name, device_file))
|
devs.append(OneWire(name, device_file))
|
||||||
add_devices(devs)
|
add_devices(devs)
|
||||||
|
|
||||||
|
@ -88,6 +91,8 @@ class OneWire(Entity):
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from the device."""
|
"""Get the latest data from the device."""
|
||||||
|
temp = -99
|
||||||
|
if self._device_file.startswith('/sys/bus/w1/devices'):
|
||||||
lines = self._read_temp_raw()
|
lines = self._read_temp_raw()
|
||||||
while lines[0].strip()[-3:] != 'YES':
|
while lines[0].strip()[-3:] != 'YES':
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
|
@ -96,6 +101,17 @@ class OneWire(Entity):
|
||||||
if equals_pos != -1:
|
if equals_pos != -1:
|
||||||
temp_string = lines[1][equals_pos+2:]
|
temp_string = lines[1][equals_pos+2:]
|
||||||
temp = round(float(temp_string) / 1000.0, 1)
|
temp = round(float(temp_string) / 1000.0, 1)
|
||||||
|
else:
|
||||||
|
ds_device_file = open(self._device_file, 'r')
|
||||||
|
temp_read = ds_device_file.readlines()
|
||||||
|
ds_device_file.close()
|
||||||
|
if len(temp_read) == 1:
|
||||||
|
try:
|
||||||
|
temp = round(float(temp_read[0]), 1)
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.warning('Invalid temperature value read from ' +
|
||||||
|
self._device_file)
|
||||||
|
|
||||||
if temp < -55 or temp > 125:
|
if temp < -55 or temp > 125:
|
||||||
return
|
return
|
||||||
self._state = temp
|
self._state = temp
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue