mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
added test; addressed comments
This commit is contained in:
parent
fba5becd90
commit
e9059a3ed9
@ -24,10 +24,6 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||||||
devices = []
|
devices = []
|
||||||
|
|
||||||
for dev_name, properties in switches.items():
|
for dev_name, properties in switches.items():
|
||||||
if 'statecmd' in properties and CONF_VALUE_TEMPLATE not in properties:
|
|
||||||
_LOGGER.warning("Specify a %s when using statemcd",
|
|
||||||
CONF_VALUE_TEMPLATE)
|
|
||||||
continue
|
|
||||||
devices.append(
|
devices.append(
|
||||||
CommandSwitch(
|
CommandSwitch(
|
||||||
hass,
|
hass,
|
||||||
@ -68,8 +64,8 @@ class CommandSwitch(SwitchDevice):
|
|||||||
return success
|
return success
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _query_state(command):
|
def _query_state_value(command):
|
||||||
""" Execute state command. """
|
""" Execute state command for return value. """
|
||||||
_LOGGER.info('Running state command: %s', command)
|
_LOGGER.info('Running state command: %s', command)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -78,10 +74,16 @@ class CommandSwitch(SwitchDevice):
|
|||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
_LOGGER.error('Command failed: %s', command)
|
_LOGGER.error('Command failed: %s', command)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _query_state_code(command):
|
||||||
|
""" Execute state command for return code. """
|
||||||
|
_LOGGER.info('Running state command: %s', command)
|
||||||
|
return subprocess.call(command, shell=True) == 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
""" No polling needed. """
|
""" Only poll if we have statecmd. """
|
||||||
return True
|
return self._command_state is not None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -93,13 +95,23 @@ class CommandSwitch(SwitchDevice):
|
|||||||
""" True if device is on. """
|
""" True if device is on. """
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
|
def _query_state(self):
|
||||||
|
""" Query for state. """
|
||||||
|
if not self._command_state:
|
||||||
|
_LOGGER.error('No state command specified')
|
||||||
|
return
|
||||||
|
if self._value_template:
|
||||||
|
return CommandSwitch._query_state_value(self._command_state)
|
||||||
|
return CommandSwitch._query_state_code(self._command_state)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" Update device state. """
|
""" Update device state. """
|
||||||
if self._command_state and self._value_template:
|
if self._command_state:
|
||||||
payload = CommandSwitch._query_state(self._command_state)
|
payload = str(self._query_state())
|
||||||
payload = template.render_with_possible_json_value(
|
if self._value_template:
|
||||||
self._hass, self._value_template, payload)
|
payload = template.render_with_possible_json_value(
|
||||||
self._state = (payload == "True")
|
self._hass, self._value_template, payload)
|
||||||
|
self._state = (payload.lower() == "true")
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
""" Turn the device on. """
|
""" Turn the device on. """
|
||||||
|
158
tests/components/switch/test_command_switch.py
Normal file
158
tests/components/switch/test_command_switch.py
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
"""
|
||||||
|
tests.components.switch.test_command_switch
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests command switch.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from homeassistant import core
|
||||||
|
from homeassistant.const import STATE_ON, STATE_OFF
|
||||||
|
import homeassistant.components.switch as switch
|
||||||
|
|
||||||
|
|
||||||
|
class TestCommandSwitch(unittest.TestCase):
|
||||||
|
""" Test the command switch. """
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
self.hass = core.HomeAssistant()
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
""" Stop down stuff we started. """
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_state_none(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
|
path = os.path.join(tempdirname, 'switch_status')
|
||||||
|
test_switch = {
|
||||||
|
'oncmd': 'echo 1 > {}'.format(path),
|
||||||
|
'offcmd': 'echo 0 > {}'.format(path),
|
||||||
|
}
|
||||||
|
self.assertTrue(switch.setup(self.hass, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'command_switch',
|
||||||
|
'switches': {
|
||||||
|
'test': test_switch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
switch.turn_off(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
|
||||||
|
def test_state_value(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
|
path = os.path.join(tempdirname, 'switch_status')
|
||||||
|
test_switch = {
|
||||||
|
'statecmd': 'cat {}'.format(path),
|
||||||
|
'oncmd': 'echo 1 > {}'.format(path),
|
||||||
|
'offcmd': 'echo 0 > {}'.format(path),
|
||||||
|
'value_template': '{{ value=="1" }}'
|
||||||
|
}
|
||||||
|
self.assertTrue(switch.setup(self.hass, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'command_switch',
|
||||||
|
'switches': {
|
||||||
|
'test': test_switch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
switch.turn_off(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
|
||||||
|
def test_state_json_value(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
|
path = os.path.join(tempdirname, 'switch_status')
|
||||||
|
oncmd = json.dumps({'status': 'ok'})
|
||||||
|
offcmd = json.dumps({'status': 'nope'})
|
||||||
|
test_switch = {
|
||||||
|
'statecmd': 'cat {}'.format(path),
|
||||||
|
'oncmd': 'echo \'{}\' > {}'.format(oncmd, path),
|
||||||
|
'offcmd': 'echo \'{}\' > {}'.format(offcmd, path),
|
||||||
|
'value_template': '{{ value_json.status=="ok" }}'
|
||||||
|
}
|
||||||
|
self.assertTrue(switch.setup(self.hass, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'command_switch',
|
||||||
|
'switches': {
|
||||||
|
'test': test_switch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
switch.turn_off(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
def test_state_code(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tempdirname:
|
||||||
|
path = os.path.join(tempdirname, 'switch_status')
|
||||||
|
test_switch = {
|
||||||
|
'statecmd': 'cat {}'.format(path),
|
||||||
|
'oncmd': 'echo 1 > {}'.format(path),
|
||||||
|
'offcmd': 'echo 0 > {}'.format(path),
|
||||||
|
}
|
||||||
|
self.assertTrue(switch.setup(self.hass, {
|
||||||
|
'switch': {
|
||||||
|
'platform': 'command_switch',
|
||||||
|
'switches': {
|
||||||
|
'test': test_switch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_OFF, state.state)
|
||||||
|
|
||||||
|
switch.turn_on(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
||||||
|
|
||||||
|
switch.turn_off(self.hass, 'switch.test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('switch.test')
|
||||||
|
self.assertEqual(STATE_ON, state.state)
|
Loading…
x
Reference in New Issue
Block a user