From 9219d65c3e03cf6f6c3af37a9d1f06c8b6db5160 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 25 Aug 2016 06:35:09 +0200 Subject: [PATCH] Migrate to voluptuous (#2958) --- .../components/binary_sensor/enocean.py | 36 ++++++++++++++----- homeassistant/components/enocean.py | 19 ++++++++-- homeassistant/components/light/enocean.py | 29 +++++++++------ homeassistant/components/sensor/enocean.py | 22 +++++++++--- homeassistant/components/switch/enocean.py | 20 +++++++---- homeassistant/const.py | 2 ++ 6 files changed, 94 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/binary_sensor/enocean.py b/homeassistant/components/binary_sensor/enocean.py index 12f073f9e85..631ed0021e1 100644 --- a/homeassistant/components/binary_sensor/enocean.py +++ b/homeassistant/components/binary_sensor/enocean.py @@ -4,27 +4,41 @@ Support for EnOcean binary sensors. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/binary_sensor.enocean/ """ +import logging -from homeassistant.components.binary_sensor import BinarySensorDevice +import voluptuous as vol + +from homeassistant.components.binary_sensor import ( + BinarySensorDevice, PLATFORM_SCHEMA, SENSOR_CLASSES_SCHEMA) from homeassistant.components import enocean -from homeassistant.const import CONF_NAME +from homeassistant.const import (CONF_NAME, CONF_ID, CONF_SENSOR_CLASS) +import homeassistant.helpers.config_validation as cv -DEPENDENCIES = ["enocean"] +_LOGGER = logging.getLogger(__name__) -CONF_ID = "id" +DEPENDENCIES = ['enocean'] +DEFAULT_NAME = 'EnOcean binary sensor' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_ID): cv.string, + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Optional(CONF_SENSOR_CLASS, default=None): SENSOR_CLASSES_SCHEMA, +}) def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Binary Sensor platform fo EnOcean.""" - dev_id = config.get(CONF_ID, None) - devname = config.get(CONF_NAME, "EnOcean binary sensor") - add_devices([EnOceanBinarySensor(dev_id, devname)]) + dev_id = config.get(CONF_ID) + devname = config.get(CONF_NAME) + sensor_class = config.get(CONF_SENSOR_CLASS) + + add_devices([EnOceanBinarySensor(dev_id, devname, sensor_class)]) class EnOceanBinarySensor(enocean.EnOceanDevice, BinarySensorDevice): """Representation of EnOcean binary sensors such as wall switches.""" - def __init__(self, dev_id, devname): + def __init__(self, dev_id, devname, sensor_class): """Initialize the EnOcean binary sensor.""" enocean.EnOceanDevice.__init__(self) self.stype = "listener" @@ -32,12 +46,18 @@ class EnOceanBinarySensor(enocean.EnOceanDevice, BinarySensorDevice): self.which = -1 self.onoff = -1 self.devname = devname + self._sensor_class = sensor_class @property def name(self): """The default name for the binary sensor.""" return self.devname + @property + def sensor_class(self): + """Return the class of this sensor.""" + return self._sensor_class + def value_changed(self, value, value2): """Fire an event with the data that have changed. diff --git a/homeassistant/components/enocean.py b/homeassistant/components/enocean.py index 1e70e537c59..7c36e173510 100644 --- a/homeassistant/components/enocean.py +++ b/homeassistant/components/enocean.py @@ -4,23 +4,36 @@ EnOcean Component. For more details about this component, please refer to the documentation at https://home-assistant.io/components/EnOcean/ """ +import logging -DOMAIN = "enocean" +import voluptuous as vol + +from homeassistant.const import CONF_DEVICE +import homeassistant.helpers.config_validation as cv REQUIREMENTS = ['enocean==0.31'] -CONF_DEVICE = "device" +_LOGGER = logging.getLogger(__name__) + +DOMAIN = 'enocean' ENOCEAN_DONGLE = None +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + vol.Required(CONF_DEVICE): cv.string, + }), +}, extra=vol.ALLOW_EXTRA) + def setup(hass, config): """Setup the EnOcean component.""" global ENOCEAN_DONGLE - serial_dev = config[DOMAIN].get(CONF_DEVICE, "/dev/ttyUSB0") + serial_dev = config[DOMAIN].get(CONF_DEVICE) ENOCEAN_DONGLE = EnOceanDongle(hass, serial_dev) + return True diff --git a/homeassistant/components/light/enocean.py b/homeassistant/components/light/enocean.py index 6bd132202fc..772cb55c4e4 100644 --- a/homeassistant/components/light/enocean.py +++ b/homeassistant/components/light/enocean.py @@ -7,27 +7,34 @@ https://home-assistant.io/components/light.enocean/ import logging import math -from homeassistant.components.light import (Light, ATTR_BRIGHTNESS, - SUPPORT_BRIGHTNESS) -from homeassistant.const import CONF_NAME -from homeassistant.components import enocean +import voluptuous as vol +from homeassistant.components.light import ( + Light, ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, PLATFORM_SCHEMA) +from homeassistant.const import (CONF_NAME, CONF_ID) +from homeassistant.components import enocean +import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) -DEPENDENCIES = ["enocean"] - -CONF_ID = "id" -CONF_SENDER_ID = "sender_id" +DEPENDENCIES = ['enocean'] +DEFAULT_NAME = 'EnOcean Light' +CONF_SENDER_ID = 'sender_id' SUPPORT_ENOCEAN = SUPPORT_BRIGHTNESS +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_ID): cv.string, + vol.Required(CONF_SENDER_ID): cv.string, + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, +}) + def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the EnOcean light platform.""" - sender_id = config.get(CONF_SENDER_ID, None) - devname = config.get(CONF_NAME, "Enocean actuator") - dev_id = config.get(CONF_ID, [0x00, 0x00, 0x00, 0x00]) + sender_id = config.get(CONF_SENDER_ID) + devname = config.get(CONF_NAME) + dev_id = config.get(CONF_ID) add_devices([EnOceanLight(sender_id, devname, dev_id)]) diff --git a/homeassistant/components/sensor/enocean.py b/homeassistant/components/sensor/enocean.py index 23a59fb5ece..e998b5c9c46 100644 --- a/homeassistant/components/sensor/enocean.py +++ b/homeassistant/components/sensor/enocean.py @@ -4,20 +4,32 @@ Support for EnOcean sensors. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.enocean/ """ +import logging -from homeassistant.const import CONF_NAME +import voluptuous as vol + +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.const import (CONF_NAME, CONF_ID) from homeassistant.helpers.entity import Entity +import homeassistant.helpers.config_validation as cv from homeassistant.components import enocean -DEPENDENCIES = ["enocean"] +_LOGGER = logging.getLogger(__name__) -CONF_ID = "id" +DEFAULT_NAME = 'EnOcean sensor' +DEPENDENCIES = ['enocean'] + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_ID): cv.string, + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, +}) def setup_platform(hass, config, add_devices, discovery_info=None): """Setup an EnOcean sensor device.""" - dev_id = config.get(CONF_ID, None) - devname = config.get(CONF_NAME, None) + dev_id = config.get(CONF_ID) + devname = config.get(CONF_NAME) + add_devices([EnOceanSensor(dev_id, devname)]) diff --git a/homeassistant/components/switch/enocean.py b/homeassistant/components/switch/enocean.py index f0ae26100c3..87a89d148ab 100644 --- a/homeassistant/components/switch/enocean.py +++ b/homeassistant/components/switch/enocean.py @@ -4,25 +4,31 @@ Support for EnOcean switches. For more details about this platform, please refer to the documentation at https://home-assistant.io/components/switch.enocean/ """ - import logging -from homeassistant.const import CONF_NAME +import voluptuous as vol + +from homeassistant.components.switch import PLATFORM_SCHEMA +from homeassistant.const import (CONF_NAME, CONF_ID) from homeassistant.components import enocean from homeassistant.helpers.entity import ToggleEntity - +import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) -DEPENDENCIES = ["enocean"] +DEFAULT_NAME = 'EnOcean Switch' +DEPENDENCIES = ['enocean'] -CONF_ID = "id" +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_ID): cv.string, + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, +}) def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the EnOcean switch platform.""" - dev_id = config.get(CONF_ID, None) - devname = config.get(CONF_NAME, "Enocean actuator") + dev_id = config.get(CONF_ID) + devname = config.get(CONF_NAME) add_devices([EnOceanSwitch(dev_id, devname)]) diff --git a/homeassistant/const.py b/homeassistant/const.py index 77c682868d1..b296a4959b1 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -29,6 +29,7 @@ CONF_BLACKLIST = 'blacklist' CONF_CODE = 'code' CONF_CONDITION = 'condition' CONF_CUSTOMIZE = 'customize' +CONF_DEVICE = 'device' CONF_DISARM_AFTER_TRIGGER = 'disarm_after_trigger' CONF_DISPLAY_OPTIONS = 'display_options' CONF_ELEVATION = 'elevation' @@ -39,6 +40,7 @@ CONF_FILENAME = 'filename' CONF_HOST = 'host' CONF_HOSTS = 'hosts' CONF_ICON = 'icon' +CONF_ID = 'id' CONF_LATITUDE = 'latitude' CONF_LONGITUDE = 'longitude' CONF_METHOD = 'method'