Improved onewire configuration

This commit is contained in:
Malte Deiseroth 2015-09-16 14:17:41 +02:00
parent 8842e4e94f
commit ce501ae627

View File

@ -17,35 +17,26 @@ _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):
""" Sets up the one wire Sensors""" """ Sets up the one wire Sensors"""
# TODO check if kernel modules are loaded
# TODO implment config fore the name, but also default solution
if DEVICE_FILES == []: if DEVICE_FILES == []:
_LOGGER.error('No onewire sensor found') _LOGGER.error('No onewire sensor found. Check if dtoverlay=w1-gpio,gpiopin=4 is in your /boot/config.txt and the correct gpiopin number is set.')
return return
devs = [] devs = []
names = [] names = SENSOR_IDS
try:
## only one name given
if type(config['names']) == str:
names = config[names]
## map names and sensors in given order
elif type(config['names']) == list:
names = config['names']
## map names with ids for key in config.keys():
elif type(config['names']) == dict: if key=="names":
for sensor_id in SENSOR_IDS: ## only one name given
names.append(config['names'][sensor_id]) if isinstance(config['names'], str):
names = [config['names']]
except KeyError: ## map names and sensors in given order
## use id as name elif isinstance(config['names'], list):
if not config['names']: names = config['names']
for sensor_id in SENSOR_IDS: ## map names to ids.
names.append(sensor_id) elif isinstance(config['names'], dict):
names = [config['names'].get(sensor_id, sensor_id) for sensor_id in SENSOR_IDS]
for device_file, name in zip(DEVICE_FILES, names): for device_file, name in zip(DEVICE_FILES, names):
devs.append(OneWire(name, device_file, TEMP_CELCIUS)) devs.append(OneWire(name, device_file, TEMP_CELCIUS))
add_devices(devs) add_devices(devs)
@ -90,4 +81,4 @@ class OneWire(Entity):
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
return self._unit_of_measurement return self._unit_of_measurement