Modify import of template and PEP257
This commit is contained in:
parent
213cc920d0
commit
582394bc3b
25 changed files with 152 additions and 182 deletions
|
@ -1,7 +1,5 @@
|
|||
"""
|
||||
homeassistant.components.switch.mqtt
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Allows to configure a MQTT switch.
|
||||
Support for MQTT switches.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.mqtt/
|
||||
|
@ -11,7 +9,7 @@ import logging
|
|||
import homeassistant.components.mqtt as mqtt
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.const import CONF_VALUE_TEMPLATE
|
||||
from homeassistant.util import template
|
||||
from homeassistant.helpers import template
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -27,7 +25,7 @@ DEPENDENCIES = ['mqtt']
|
|||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
""" Add MQTT Switch. """
|
||||
"""Add MQTT switch."""
|
||||
|
||||
if config.get('command_topic') is None:
|
||||
_LOGGER.error("Missing required variable: command_topic")
|
||||
|
@ -48,7 +46,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
|
||||
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
||||
class MqttSwitch(SwitchDevice):
|
||||
""" Represents a switch that can be toggled using MQTT. """
|
||||
"""Represents a switch that can be toggled using MQTT."""
|
||||
def __init__(self, hass, name, state_topic, command_topic, qos, retain,
|
||||
payload_on, payload_off, optimistic, value_template):
|
||||
self._state = False
|
||||
|
@ -63,7 +61,7 @@ class MqttSwitch(SwitchDevice):
|
|||
self._optimistic = optimistic
|
||||
|
||||
def message_received(topic, payload, qos):
|
||||
""" A new MQTT message has been received. """
|
||||
"""A new MQTT message has been received."""
|
||||
if value_template is not None:
|
||||
payload = template.render_with_possible_json_value(
|
||||
hass, value_template, payload)
|
||||
|
@ -75,26 +73,25 @@ class MqttSwitch(SwitchDevice):
|
|||
self.update_ha_state()
|
||||
|
||||
if self._state_topic is None:
|
||||
# force optimistic mode
|
||||
# Force into optimistic mode.
|
||||
self._optimistic = True
|
||||
else:
|
||||
# subscribe the state_topic
|
||||
mqtt.subscribe(hass, self._state_topic, message_received,
|
||||
self._qos)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
""" No polling needed. """
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" The name of the switch. """
|
||||
"""The name of the switch."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
""" True if device is on. """
|
||||
"""True if device is on."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
|
@ -103,19 +100,19 @@ class MqttSwitch(SwitchDevice):
|
|||
return self._optimistic
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
""" Turn the device on. """
|
||||
"""Turn the device on."""
|
||||
mqtt.publish(self.hass, self._command_topic, self._payload_on,
|
||||
self._qos, self._retain)
|
||||
if self._optimistic:
|
||||
# optimistically assume that switch has changed state
|
||||
# Optimistically assume that switch has changed state.
|
||||
self._state = True
|
||||
self.update_ha_state()
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
""" Turn the device off. """
|
||||
"""Turn the device off."""
|
||||
mqtt.publish(self.hass, self._command_topic, self._payload_off,
|
||||
self._qos, self._retain)
|
||||
if self._optimistic:
|
||||
# optimistically assume that switch has changed state
|
||||
# Optimistically assume that switch has changed state.
|
||||
self._state = False
|
||||
self.update_ha_state()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue