mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Improve 1-Wire device family detection and error checking. Use volupt… (#3233)
* Improve 1-Wire device family detection and error checking. Use voluptuous * Fix detection of gpio connected devices
This commit is contained in:
parent
79fa9963da
commit
fa8ed4de41
@ -1,16 +1,28 @@
|
|||||||
"""
|
"""
|
||||||
Support for DS18B20 One Wire Sensors.
|
Support for 1-Wire temperature sensors.
|
||||||
|
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.onewire/
|
https://home-assistant.io/components/sensor.onewire/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
from glob import glob
|
from glob import glob
|
||||||
|
import voluptuous as vol
|
||||||
from homeassistant.const import STATE_UNKNOWN, TEMP_CELSIUS
|
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
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,
|
||||||
|
})
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -18,22 +30,22 @@ _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."""
|
||||||
base_dir = config.get('mount_dir', '/sys/bus/w1/devices/')
|
base_dir = config.get(CONF_MOUNT_DIR)
|
||||||
device_folders = glob(os.path.join(base_dir, '[10,22,28,3B,42]*'))
|
|
||||||
sensor_ids = []
|
sensor_ids = []
|
||||||
device_files = []
|
device_files = []
|
||||||
for device_folder in device_folders:
|
for device_family in DEVICE_FAMILIES:
|
||||||
sensor_ids.append(os.path.split(device_folder)[1])
|
for device_folder in glob(os.path.join(base_dir, device_family +
|
||||||
if base_dir.startswith('/sys/bus/w1/devices'):
|
'[.-]*')):
|
||||||
device_files.append(os.path.join(device_folder, 'w1_slave'))
|
sensor_ids.append(os.path.split(device_folder)[1])
|
||||||
else:
|
if base_dir == DEFAULT_MOUNT_DIR:
|
||||||
device_files.append(os.path.join(device_folder, 'temperature'))
|
device_files.append(os.path.join(device_folder, 'w1_slave'))
|
||||||
|
else:
|
||||||
|
device_files.append(os.path.join(device_folder, 'temperature'))
|
||||||
|
|
||||||
if device_files == []:
|
if device_files == []:
|
||||||
_LOGGER.error('No onewire sensor found.')
|
_LOGGER.error('No onewire sensor found. Check if '
|
||||||
_LOGGER.error('Check if dtoverlay=w1-gpio,gpiopin=4.')
|
'dtoverlay=w1-gpio is in your /boot/config.txt. '
|
||||||
_LOGGER.error('is in your /boot/config.txt and')
|
'Check the mount_dir parameter if it\'s defined.')
|
||||||
_LOGGER.error('the correct gpiopin number is set.')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
devs = []
|
devs = []
|
||||||
@ -92,7 +104,7 @@ 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
|
temp = -99
|
||||||
if self._device_file.startswith('/sys/bus/w1/devices'):
|
if self._device_file.startswith(DEFAULT_MOUNT_DIR):
|
||||||
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)
|
||||||
@ -102,15 +114,18 @@ class OneWire(Entity):
|
|||||||
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:
|
else:
|
||||||
ds_device_file = open(self._device_file, 'r')
|
try:
|
||||||
temp_read = ds_device_file.readlines()
|
ds_device_file = open(self._device_file, 'r')
|
||||||
ds_device_file.close()
|
temp_read = ds_device_file.readlines()
|
||||||
if len(temp_read) == 1:
|
ds_device_file.close()
|
||||||
try:
|
if len(temp_read) == 1:
|
||||||
temp = round(float(temp_read[0]), 1)
|
temp = round(float(temp_read[0]), 1)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.warning('Invalid temperature value read from ' +
|
_LOGGER.warning('Invalid temperature value read from ' +
|
||||||
self._device_file)
|
self._device_file)
|
||||||
|
except FileNotFoundError:
|
||||||
|
_LOGGER.warning('Cannot read from sensor: ' +
|
||||||
|
self._device_file)
|
||||||
|
|
||||||
if temp < -55 or temp > 125:
|
if temp < -55 or temp > 125:
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user