mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
style adaptions
This commit is contained in:
parent
d91f414313
commit
b35bdc606a
@ -1,4 +1,10 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
homeassistant.components.switch.command_switch
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Allows to configure custom shell commands to turn a switch on/off.
|
||||||
|
"""
|
||||||
import logging
|
import logging
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.helpers.entity import ToggleEntity
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
|
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
|
||||||
@ -14,17 +20,18 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||||||
switches = config.get('switches', {})
|
switches = config.get('switches', {})
|
||||||
devices = []
|
devices = []
|
||||||
|
|
||||||
for k, v in switches.items():
|
for dev_name, properties in switches.items():
|
||||||
devices.append(
|
devices.append(
|
||||||
CommandSwitch(
|
CommandSwitch(
|
||||||
k,
|
dev_name,
|
||||||
v.get('oncmd', 'true'),
|
properties.get('oncmd', 'true'),
|
||||||
v.get('offcmd', 'true')))
|
properties.get('offcmd', 'true')))
|
||||||
|
|
||||||
add_devices_callback(devices)
|
add_devices_callback(devices)
|
||||||
|
|
||||||
|
|
||||||
class CommandSwitch(ToggleEntity):
|
class CommandSwitch(ToggleEntity):
|
||||||
|
""" Represents a switch that can be togggled using shell commands """
|
||||||
def __init__(self, name, command_on, command_off):
|
def __init__(self, name, command_on, command_off):
|
||||||
self._name = name or DEVICE_DEFAULT_NAME
|
self._name = name or DEVICE_DEFAULT_NAME
|
||||||
self._state = STATE_OFF
|
self._state = STATE_OFF
|
||||||
@ -33,6 +40,7 @@ class CommandSwitch(ToggleEntity):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _switch(command):
|
def _switch(command):
|
||||||
|
""" Execute the actual commands """
|
||||||
_LOGGER.info('Running command: {}'.format(command))
|
_LOGGER.info('Running command: {}'.format(command))
|
||||||
|
|
||||||
success = (subprocess.call(command, shell=True) == 0)
|
success = (subprocess.call(command, shell=True) == 0)
|
||||||
@ -44,25 +52,30 @@ class CommandSwitch(ToggleEntity):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
|
""" No polling needed """
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
""" The name of the switch """
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
""" Returns the state of the switch. """
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
|
""" True if device is on. """
|
||||||
return self._state == STATE_ON
|
return self._state == STATE_ON
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
|
""" Turn the device on. """
|
||||||
if CommandSwitch._switch(self._command_on):
|
if CommandSwitch._switch(self._command_on):
|
||||||
self._state = STATE_ON
|
self._state = STATE_ON
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
|
""" Turn the device off. """
|
||||||
if CommandSwitch._switch(self._command_off):
|
if CommandSwitch._switch(self._command_off):
|
||||||
self._state = STATE_OFF
|
self._state = STATE_OFF
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user