Modify import of template and PEP257
This commit is contained in:
1 parent
213cc920d0
commit
582394bc3b
25 files changed
+152
-182
No files matched your search
@@ -1,7 +1,5 @@
|
||||
"""
|
||||
homeassistant.components.switch.command_switch
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Allows to configure custom shell commands to turn a switch on/off.
|
||||
Support for custom shell commands to turn a switch on/off.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.command_switch/
|
||||
@@ -11,15 +9,14 @@ import subprocess
|
||||
|
||||
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__)
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
""" Find and return switches controlled by shell commands. """
|
||||
|
||||
"""Find and return switches controlled by shell commands."""
|
||||
switches = config.get('switches', {})
|
||||
devices = []
|
||||
|
||||
@@ -37,7 +34,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
|
||||
|
||||
class CommandSwitch(SwitchDevice):
|
||||
""" Represents a switch that can be togggled using shell commands. """
|
||||
"""Represents a switch that can be toggled using shell commands."""
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(self, hass, name, command_on, command_off,
|
||||
@@ -53,7 +50,7 @@ class CommandSwitch(SwitchDevice):
|
||||
|
||||
@staticmethod
|
||||
def _switch(command):
|
||||
""" Execute the actual commands. """
|
||||
"""Execute the actual commands."""
|
||||
_LOGGER.info('Running command: %s', command)
|
||||
|
||||
success = (subprocess.call(command, shell=True) == 0)
|
||||
@@ -65,7 +62,7 @@ class CommandSwitch(SwitchDevice):
|
||||
|
||||
@staticmethod
|
||||
def _query_state_value(command):
|
||||
""" Execute state command for return value. """
|
||||
"""Execute state command for return value."""
|
||||
_LOGGER.info('Running state command: %s', command)
|
||||
|
||||
try:
|
||||
@@ -76,27 +73,27 @@ class CommandSwitch(SwitchDevice):
|
||||
|
||||
@staticmethod
|
||||
def _query_state_code(command):
|
||||
""" Execute state command for return code. """
|
||||
"""Execute state command for return code."""
|
||||
_LOGGER.info('Running state command: %s', command)
|
||||
return subprocess.call(command, shell=True) == 0
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
""" Only poll if we have statecmd. """
|
||||
"""Only poll if we have state command."""
|
||||
return self._command_state is not None
|
||||
|
||||
@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
|
||||
|
||||
def _query_state(self):
|
||||
""" Query for state. """
|
||||
"""Query for state."""
|
||||
if not self._command_state:
|
||||
_LOGGER.error('No state command specified')
|
||||
return
|
||||
@@ -105,7 +102,7 @@ class CommandSwitch(SwitchDevice):
|
||||
return CommandSwitch._query_state_code(self._command_state)
|
||||
|
||||
def update(self):
|
||||
""" Update device state. """
|
||||
"""Update device state."""
|
||||
if self._command_state:
|
||||
payload = str(self._query_state())
|
||||
if self._value_template:
|
||||
@@ -114,14 +111,14 @@ class CommandSwitch(SwitchDevice):
|
||||
self._state = (payload.lower() == "true")
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
""" Turn the device on. """
|
||||
"""Turn the device on."""
|
||||
if (CommandSwitch._switch(self._command_on) and
|
||||
not self._command_state):
|
||||
self._state = True
|
||||
self.update_ha_state()
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
""" Turn the device off. """
|
||||
"""Turn the device off."""
|
||||
if (CommandSwitch._switch(self._command_off) and
|
||||
not self._command_state):
|
||||
self._state = False
|
||||
|
||||
@@ -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()
|
||||
@@ -1,7 +1,5 @@
|
||||
"""
|
||||
homeassistant.components.switch.template
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Allows the creation of a switch that integrates other components together
|
||||
Support for switches which integrates with other components.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.template/
|
||||
@@ -15,7 +13,8 @@ from homeassistant.core import EVENT_STATE_CHANGED
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.helpers.entity import generate_entity_id
|
||||
from homeassistant.helpers.service import call_from_config
|
||||
from homeassistant.util import slugify, template
|
||||
from homeassistant.helpers import template
|
||||
from homeassistant.util import slugify
|
||||
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
|
||||
@@ -31,7 +30,7 @@ OFF_ACTION = 'turn_off'
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
""" Sets up the switches. """
|
||||
"""Sets up the Template switch."""
|
||||
|
||||
switches = []
|
||||
if config.get(CONF_SWITCHES) is None:
|
||||
@@ -80,7 +79,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
|
||||
class SwitchTemplate(SwitchDevice):
|
||||
""" Represents a Template Switch. """
|
||||
"""Represents a Template switch."""
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(self,
|
||||
@@ -103,37 +102,37 @@ class SwitchTemplate(SwitchDevice):
|
||||
self.update()
|
||||
|
||||
def _update_callback(_event):
|
||||
""" Called when the target device changes state. """
|
||||
"""Called when the target device changes state."""
|
||||
self.update_ha_state(True)
|
||||
|
||||
self.hass.bus.listen(EVENT_STATE_CHANGED, _update_callback)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Returns the name of the device. """
|
||||
"""Returns the name of the switch."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
""" Tells Home Assistant not to poll this entity. """
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
""" Fires the on action. """
|
||||
"""Fires the on action."""
|
||||
call_from_config(self.hass, self._on_action, True)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
""" Fires the off action. """
|
||||
"""Fires the off action."""
|
||||
call_from_config(self.hass, self._off_action, True)
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
""" True if device is on. """
|
||||
"""True if device is on."""
|
||||
return self._value.lower() == 'true' or self._value == STATE_ON
|
||||
|
||||
@property
|
||||
def is_off(self):
|
||||
""" True if device is off. """
|
||||
"""True if device is off."""
|
||||
return self._value.lower() == 'false' or self._value == STATE_OFF
|
||||
|
||||
@property
|
||||
@@ -142,7 +141,7 @@ class SwitchTemplate(SwitchDevice):
|
||||
return self.is_on or self.is_off
|
||||
|
||||
def update(self):
|
||||
""" Updates the state from the template. """
|
||||
"""Updates the state from the template."""
|
||||
try:
|
||||
self._value = template.render(self.hass, self._template)
|
||||
if not self.available:
|
||||
|
||||
Reference in new issue
Block a user