From ee6c83f569a022a8c2a559322fae3f1b1fe3b2eb Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 9 Sep 2016 02:34:55 +0200 Subject: [PATCH] Use constants and update ordering (#3267) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐬 --- homeassistant/components/lock/mqtt.py | 30 ++++++++-------- homeassistant/components/switch/mqtt.py | 46 ++++++++++++------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/lock/mqtt.py b/homeassistant/components/lock/mqtt.py index 81ab179efd4..b8f8ad9c5b3 100644 --- a/homeassistant/components/lock/mqtt.py +++ b/homeassistant/components/lock/mqtt.py @@ -8,25 +8,26 @@ import logging import voluptuous as vol -import homeassistant.components.mqtt as mqtt from homeassistant.components.lock import LockDevice -from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE from homeassistant.components.mqtt import ( CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN) +from homeassistant.const import ( + CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE) from homeassistant.helpers import template +import homeassistant.components.mqtt as mqtt import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) -DEPENDENCIES = ['mqtt'] CONF_PAYLOAD_LOCK = 'payload_lock' CONF_PAYLOAD_UNLOCK = 'payload_unlock' DEFAULT_NAME = 'MQTT Lock' +DEFAULT_OPTIMISTIC = False DEFAULT_PAYLOAD_LOCK = 'LOCK' DEFAULT_PAYLOAD_UNLOCK = 'UNLOCK' -DEFAULT_OPTIMISTIC = False +DEPENDENCIES = ['mqtt'] PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, @@ -43,15 +44,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the MQTT lock.""" add_devices([MqttLock( hass, - config[CONF_NAME], + config.get(CONF_NAME), config.get(CONF_STATE_TOPIC), - config[CONF_COMMAND_TOPIC], - config[CONF_QOS], - config[CONF_RETAIN], - config[CONF_PAYLOAD_LOCK], - config[CONF_PAYLOAD_UNLOCK], - config[CONF_OPTIMISTIC], - config.get(CONF_VALUE_TEMPLATE))]) + config.get(CONF_COMMAND_TOPIC), + config.get(CONF_QOS), + config.get(CONF_RETAIN), + config.get(CONF_PAYLOAD_LOCK), + config.get(CONF_PAYLOAD_UNLOCK), + config.get(CONF_OPTIMISTIC), + config.get(CONF_VALUE_TEMPLATE) + )]) # pylint: disable=too-many-arguments, too-many-instance-attributes @@ -88,8 +90,8 @@ class MqttLock(LockDevice): # Force into optimistic mode. self._optimistic = True else: - mqtt.subscribe(hass, self._state_topic, message_received, - self._qos) + mqtt.subscribe( + hass, self._state_topic, message_received, self._qos) @property def should_poll(self): diff --git a/homeassistant/components/switch/mqtt.py b/homeassistant/components/switch/mqtt.py index 2a2b2aed547..d17ea82cd32 100644 --- a/homeassistant/components/switch/mqtt.py +++ b/homeassistant/components/switch/mqtt.py @@ -8,24 +8,23 @@ import logging import voluptuous as vol -import homeassistant.components.mqtt as mqtt -from homeassistant.components.switch import SwitchDevice -from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE from homeassistant.components.mqtt import ( CONF_STATE_TOPIC, CONF_COMMAND_TOPIC, CONF_QOS, CONF_RETAIN) -import homeassistant.helpers.config_validation as cv +from homeassistant.components.switch import SwitchDevice +from homeassistant.const import ( + CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_OFF, + CONF_PAYLOAD_ON) from homeassistant.helpers import template +import homeassistant.components.mqtt as mqtt +import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) DEPENDENCIES = ['mqtt'] -CONF_PAYLOAD_ON = 'payload_on' -CONF_PAYLOAD_OFF = 'payload_off' - -DEFAULT_NAME = "MQTT Switch" -DEFAULT_PAYLOAD_ON = "ON" -DEFAULT_PAYLOAD_OFF = "OFF" +DEFAULT_NAME = 'MQTT Switch' +DEFAULT_PAYLOAD_ON = 'ON' +DEFAULT_PAYLOAD_OFF = 'OFF' DEFAULT_OPTIMISTIC = False PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ @@ -37,19 +36,20 @@ PLATFORM_SCHEMA = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend({ # pylint: disable=unused-argument -def setup_platform(hass, config, add_devices_callback, discovery_info=None): - """Add MQTT switch.""" - add_devices_callback([MqttSwitch( +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup the MQTT switch.""" + add_devices([MqttSwitch( hass, - config[CONF_NAME], + config.get(CONF_NAME), config.get(CONF_STATE_TOPIC), - config[CONF_COMMAND_TOPIC], - config[CONF_QOS], - config[CONF_RETAIN], - config[CONF_PAYLOAD_ON], - config[CONF_PAYLOAD_OFF], - config[CONF_OPTIMISTIC], - config.get(CONF_VALUE_TEMPLATE))]) + config.get(CONF_COMMAND_TOPIC), + config.get(CONF_QOS), + config.get(CONF_RETAIN), + config.get(CONF_PAYLOAD_ON), + config.get(CONF_PAYLOAD_OFF), + config.get(CONF_OPTIMISTIC), + config.get(CONF_VALUE_TEMPLATE) + )]) # pylint: disable=too-many-arguments, too-many-instance-attributes @@ -86,8 +86,8 @@ class MqttSwitch(SwitchDevice): # Force into optimistic mode. self._optimistic = True else: - mqtt.subscribe(hass, self._state_topic, message_received, - self._qos) + mqtt.subscribe( + hass, self._state_topic, message_received, self._qos) @property def should_poll(self):