def test_state_json_value(self): """Test with state JSON value.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, 'switch_status') oncmd = json.dumps({'status': 'ok'}) offcmd = json.dumps({'status': 'nope'}) test_switch = { 'command_state': 'cat {}'.format(path), 'command_on': 'echo \'{}\' > {}'.format(oncmd, path), 'command_off': 'echo \'{}\' > {}'.format(offcmd, path), 'value_template': '{{ value_json.status=="ok" }}' } self.assertTrue(setup_component(self.hass, switch.DOMAIN, { 'switch': { 'platform': 'command_line', 'switches': { 'test': test_switch } } })) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_state_value(self): """Test with state value.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, 'switch_status') test_switch = { 'command_state': 'cat {}'.format(path), 'command_on': 'echo 1 > {}'.format(path), 'command_off': 'echo 0 > {}'.format(path), 'value_template': '{{ value=="1" }}' } self.assertTrue( setup_component( self.hass, switch.DOMAIN, { 'switch': { 'platform': 'command_line', 'switches': { 'test': test_switch } } })) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_state_none(self): """Test with none state.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, 'switch_status') test_switch = { 'command_on': 'echo 1 > {}'.format(path), 'command_off': 'echo 0 > {}'.format(path), } self.assertTrue(setup_component(self.hass, switch.DOMAIN, { 'switch': { 'platform': 'command_line', 'switches': { 'test': test_switch } } })) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_state_none(self): """Test with none state.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, "switch_status") test_switch = { "command_on": "echo 1 > {}".format(path), "command_off": "echo 0 > {}".format(path), } assert setup_component( self.hass, switch.DOMAIN, { "switch": { "platform": "command_line", "switches": { "test": test_switch }, } }, ) state = self.hass.states.get("switch.test") assert STATE_OFF == state.state common.turn_on(self.hass, "switch.test") self.hass.block_till_done() state = self.hass.states.get("switch.test") assert STATE_ON == state.state common.turn_off(self.hass, "switch.test") self.hass.block_till_done() state = self.hass.states.get("switch.test") assert STATE_OFF == state.state
def test_valid_hostname(self): """Test with valid hostname.""" global TEST_STATE TEST_STATE = False self.assertTrue(setup_component(self.hass, switch.DOMAIN, { 'switch': { 'platform': 'wake_on_lan', 'mac_address': '00-01-02-03-04-05', 'host': 'validhostname', } })) state = self.hass.states.get('switch.wake_on_lan') self.assertEqual(STATE_OFF, state.state) TEST_STATE = True common.turn_on(self.hass, 'switch.wake_on_lan') self.hass.block_till_done() state = self.hass.states.get('switch.wake_on_lan') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'switch.wake_on_lan') self.hass.block_till_done() state = self.hass.states.get('switch.wake_on_lan') self.assertEqual(STATE_ON, state.state)
def test_methods(self): """Test is_on, turn_on, turn_off methods.""" assert setup_component(self.opp, switch.DOMAIN, {switch.DOMAIN: { CONF_PLATFORM: "test" }}) assert switch.is_on(self.opp, self.switch_1.entity_id) assert not switch.is_on(self.opp, self.switch_2.entity_id) assert not switch.is_on(self.opp, self.switch_3.entity_id) common.turn_off(self.opp, self.switch_1.entity_id) common.turn_on(self.opp, self.switch_2.entity_id) self.opp.block_till_done() assert not switch.is_on(self.opp, self.switch_1.entity_id) assert switch.is_on(self.opp, self.switch_2.entity_id) # Turn all off common.turn_off(self.opp) self.opp.block_till_done() assert not switch.is_on(self.opp, self.switch_1.entity_id) assert not switch.is_on(self.opp, self.switch_2.entity_id) assert not switch.is_on(self.opp, self.switch_3.entity_id) # Turn all on common.turn_on(self.opp) self.opp.block_till_done() assert switch.is_on(self.opp, self.switch_1.entity_id) assert switch.is_on(self.opp, self.switch_2.entity_id) assert switch.is_on(self.opp, self.switch_3.entity_id)
def test_off_action(self): """Test off action.""" assert setup.setup_component( self.hass, "switch", { "switch": { "platform": "template", "switches": { "test_template_switch": { "value_template": "{{ states.switch.test_state.state }}", "turn_on": { "service": "switch.turn_on", "entity_id": "switch.test_state", }, "turn_off": {"service": "test.automation"}, } }, } }, ) self.hass.start() self.hass.block_till_done() self.hass.states.set("switch.test_state", STATE_ON) self.hass.block_till_done() state = self.hass.states.get("switch.test_template_switch") assert state.state == STATE_ON common.turn_off(self.hass, "switch.test_template_switch") self.hass.block_till_done() assert len(self.calls) == 1
def test_valid_hostname(self): """Test with valid hostname.""" global TEST_STATE TEST_STATE = False assert setup_component( self.opp, switch.DOMAIN, { "switch": { "platform": "wake_on_lan", "mac": "00-01-02-03-04-05", "host": "validhostname", } }, ) state = self.opp.states.get("switch.wake_on_lan") assert STATE_OFF == state.state TEST_STATE = True common.turn_on(self.opp, "switch.wake_on_lan") self.opp.block_till_done() state = self.opp.states.get("switch.wake_on_lan") assert STATE_ON == state.state common.turn_off(self.opp, "switch.wake_on_lan") self.opp.block_till_done() state = self.opp.states.get("switch.wake_on_lan") assert STATE_ON == state.state
def test_off_action(self): """Test off action.""" assert setup.setup_component(self.hass, 'switch', { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': { 'value_template': "{{ states.switch.test_state.state }}", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'test.automation' }, } } } }) self.hass.start() self.hass.block_till_done() self.hass.states.set('switch.test_state', STATE_ON) self.hass.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_ON common.turn_off(self.hass, 'switch.test_template_switch') self.hass.block_till_done() assert len(self.calls) == 1
def test_state_none(self): """Test with none state.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, 'switch_status') test_switch = { 'command_on': 'echo 1 > {}'.format(path), 'command_off': 'echo 0 > {}'.format(path), } assert setup_component( self.hass, switch.DOMAIN, { 'switch': { 'platform': 'command_line', 'switches': { 'test': test_switch } } }) state = self.hass.states.get('switch.test') assert STATE_OFF == state.state common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') assert STATE_ON == state.state common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') assert STATE_OFF == state.state
def test_state_value(self): """Test with state value.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, 'switch_status') test_switch = { 'command_state': 'cat {}'.format(path), 'command_on': 'echo 1 > {}'.format(path), 'command_off': 'echo 0 > {}'.format(path), 'value_template': '{{ value=="1" }}' } assert setup_component(self.hass, switch.DOMAIN, { 'switch': { 'platform': 'command_line', 'switches': { 'test': test_switch } } }) state = self.hass.states.get('switch.test') assert STATE_OFF == state.state common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') assert STATE_ON == state.state common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') assert STATE_OFF == state.state
def test_on_off(self): """Test turning the switch on and off.""" assert self.switch().state == 'off' assert self.other_switch().state == 'off' assert not switch.is_on(self.hass, ENTITY_SWITCH) common.turn_on(self.hass, ENTITY_SWITCH) self.hass.block_till_done() self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER) common.turn_off(self.hass, ENTITY_SWITCH) self.hass.block_till_done() self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
def test_on_off(self): """Test turning the switch on and off.""" assert self.switch().state == "off" assert self.other_switch().state == "off" assert not switch.is_on(self.hass, ENTITY_SWITCH) common.turn_on(self.hass, ENTITY_SWITCH) self.hass.block_till_done() self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER) common.turn_off(self.hass, ENTITY_SWITCH) self.hass.block_till_done() self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
def test_methods(self): """Test is_on, turn_on, turn_off methods.""" self.assertTrue( setup_component(self.hass, switch.DOMAIN, {switch.DOMAIN: { CONF_PLATFORM: 'test' }})) self.assertTrue(switch.is_on(self.hass)) self.assertEqual( STATE_ON, self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id)) common.turn_off(self.hass, self.switch_1.entity_id) common.turn_on(self.hass, self.switch_2.entity_id) self.hass.block_till_done() self.assertTrue(switch.is_on(self.hass)) self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id)) # Turn all off common.turn_off(self.hass) self.hass.block_till_done() self.assertFalse(switch.is_on(self.hass)) self.assertEqual( STATE_OFF, self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id)) # Turn all on common.turn_on(self.hass) self.hass.block_till_done() self.assertTrue(switch.is_on(self.hass)) self.assertEqual( STATE_ON, self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertTrue(switch.is_on(self.hass, self.switch_3.entity_id))
def test_methods(self): """Test is_on, turn_on, turn_off methods.""" self.assertTrue(setup_component( self.hass, switch.DOMAIN, {switch.DOMAIN: {CONF_PLATFORM: 'test'}} )) self.assertTrue(switch.is_on(self.hass)) self.assertEqual( STATE_ON, self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id)) common.turn_off(self.hass, self.switch_1.entity_id) common.turn_on(self.hass, self.switch_2.entity_id) self.hass.block_till_done() self.assertTrue(switch.is_on(self.hass)) self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id)) # Turn all off common.turn_off(self.hass) self.hass.block_till_done() self.assertFalse(switch.is_on(self.hass)) self.assertEqual( STATE_OFF, self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id)) # Turn all on common.turn_on(self.hass) self.hass.block_till_done() self.assertTrue(switch.is_on(self.hass)) self.assertEqual( STATE_ON, self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id)) self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertTrue(switch.is_on(self.hass, self.switch_3.entity_id))
def test_methods(self): """Test is_on, turn_on, turn_off methods.""" assert setup_component(self.hass, switch.DOMAIN, {switch.DOMAIN: { CONF_PLATFORM: "test" }}) assert switch.is_on(self.hass) assert STATE_ON == self.hass.states.get( switch.ENTITY_ID_ALL_SWITCHES).state assert switch.is_on(self.hass, self.switch_1.entity_id) assert not switch.is_on(self.hass, self.switch_2.entity_id) assert not switch.is_on(self.hass, self.switch_3.entity_id) common.turn_off(self.hass, self.switch_1.entity_id) common.turn_on(self.hass, self.switch_2.entity_id) self.hass.block_till_done() assert switch.is_on(self.hass) assert not switch.is_on(self.hass, self.switch_1.entity_id) assert switch.is_on(self.hass, self.switch_2.entity_id) # Turn all off common.turn_off(self.hass) self.hass.block_till_done() assert not switch.is_on(self.hass) assert STATE_OFF == self.hass.states.get( switch.ENTITY_ID_ALL_SWITCHES).state assert not switch.is_on(self.hass, self.switch_1.entity_id) assert not switch.is_on(self.hass, self.switch_2.entity_id) assert not switch.is_on(self.hass, self.switch_3.entity_id) # Turn all on common.turn_on(self.hass) self.hass.block_till_done() assert switch.is_on(self.hass) assert STATE_ON == self.hass.states.get( switch.ENTITY_ID_ALL_SWITCHES).state assert switch.is_on(self.hass, self.switch_1.entity_id) assert switch.is_on(self.hass, self.switch_2.entity_id) assert switch.is_on(self.hass, self.switch_3.entity_id)
def test_methods(self): """Test is_on, turn_on, turn_off methods.""" assert setup_component( self.hass, switch.DOMAIN, {switch.DOMAIN: {CONF_PLATFORM: 'test'}} ) assert switch.is_on(self.hass) assert STATE_ON == \ self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state assert switch.is_on(self.hass, self.switch_1.entity_id) assert not switch.is_on(self.hass, self.switch_2.entity_id) assert not switch.is_on(self.hass, self.switch_3.entity_id) common.turn_off(self.hass, self.switch_1.entity_id) common.turn_on(self.hass, self.switch_2.entity_id) self.hass.block_till_done() assert switch.is_on(self.hass) assert not switch.is_on(self.hass, self.switch_1.entity_id) assert switch.is_on(self.hass, self.switch_2.entity_id) # Turn all off common.turn_off(self.hass) self.hass.block_till_done() assert not switch.is_on(self.hass) assert STATE_OFF == \ self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state assert not switch.is_on(self.hass, self.switch_1.entity_id) assert not switch.is_on(self.hass, self.switch_2.entity_id) assert not switch.is_on(self.hass, self.switch_3.entity_id) # Turn all on common.turn_on(self.hass) self.hass.block_till_done() assert switch.is_on(self.hass) assert STATE_ON == \ self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state assert switch.is_on(self.hass, self.switch_1.entity_id) assert switch.is_on(self.hass, self.switch_2.entity_id) assert switch.is_on(self.hass, self.switch_3.entity_id)
def test_off_script(self): """Test with turn off script.""" global TEST_STATE TEST_STATE = False assert setup_component( self.hass, switch.DOMAIN, { "switch": { "platform": "wake_on_lan", "mac": "00-01-02-03-04-05", "host": "validhostname", "turn_off": { "service": "shell_command.turn_off_target" }, } }, ) self.hass.block_till_done() calls = mock_service(self.hass, "shell_command", "turn_off_target") state = self.hass.states.get("switch.wake_on_lan") assert STATE_OFF == state.state TEST_STATE = True common.turn_on(self.hass, "switch.wake_on_lan") self.hass.block_till_done() state = self.hass.states.get("switch.wake_on_lan") assert STATE_ON == state.state assert len(calls) == 0 TEST_STATE = False common.turn_off(self.hass, "switch.wake_on_lan") self.hass.block_till_done() state = self.hass.states.get("switch.wake_on_lan") assert STATE_OFF == state.state assert len(calls) == 1
async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish): """Test the sending MQTT commands in optimistic mode.""" fake_state = ha.State('switch.test', 'on') with patch( 'homeassistant.helpers.restore_state.RestoreEntity' '.async_get_last_state', return_value=mock_coro(fake_state)): assert await async_setup_component( hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'qos': '2' } }) state = hass.states.get('switch.test') assert STATE_ON == state.state assert state.attributes.get(ATTR_ASSUMED_STATE) common.turn_on(hass, 'switch.test') await hass.async_block_till_done() mock_publish.async_publish.assert_called_once_with('command-topic', 'beer on', 2, False) mock_publish.async_publish.reset_mock() state = hass.states.get('switch.test') assert STATE_ON == state.state common.turn_off(hass, 'switch.test') await hass.async_block_till_done() await hass.async_block_till_done() mock_publish.async_publish.assert_called_once_with('command-topic', 'beer off', 2, False) state = hass.states.get('switch.test') assert STATE_OFF == state.state
def test_state_json_value(self): """Test with state JSON value.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, "switch_status") oncmd = json.dumps({"status": "ok"}) offcmd = json.dumps({"status": "nope"}) test_switch = { "command_state": f"cat {path}", "command_on": f"echo '{oncmd}' > {path}", "command_off": f"echo '{offcmd}' > {path}", "value_template": '{{ value_json.status=="ok" }}', } assert setup_component( self.hass, switch.DOMAIN, { "switch": { "platform": "command_line", "switches": { "test": test_switch }, } }, ) self.hass.block_till_done() state = self.hass.states.get("switch.test") assert STATE_OFF == state.state common.turn_on(self.hass, "switch.test") self.hass.block_till_done() state = self.hass.states.get("switch.test") assert STATE_ON == state.state common.turn_off(self.hass, "switch.test") self.hass.block_till_done() state = self.hass.states.get("switch.test") assert STATE_OFF == state.state
async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish): """Test the sending MQTT commands in optimistic mode.""" fake_state = ha.State('switch.test', 'on') with patch('homeassistant.helpers.restore_state.RestoreEntity' '.async_get_last_state', return_value=mock_coro(fake_state)): assert await async_setup_component(hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'qos': '2' } }) state = hass.states.get('switch.test') assert STATE_ON == state.state assert state.attributes.get(ATTR_ASSUMED_STATE) common.turn_on(hass, 'switch.test') await hass.async_block_till_done() mock_publish.async_publish.assert_called_once_with( 'command-topic', 'beer on', 2, False) mock_publish.async_publish.reset_mock() state = hass.states.get('switch.test') assert STATE_ON == state.state common.turn_off(hass, 'switch.test') await hass.async_block_till_done() await hass.async_block_till_done() mock_publish.async_publish.assert_called_once_with( 'command-topic', 'beer off', 2, False) state = hass.states.get('switch.test') assert STATE_OFF == state.state
def test_sending_mqtt_commands_and_optimistic(self): """Test the sending MQTT commands in optimistic mode.""" fake_state = ha.State('switch.test', 'on') with patch('homeassistant.components.switch.mqtt.async_get_last_state', return_value=mock_coro(fake_state)): assert setup_component( self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'qos': '2' } }) state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'command-topic', 'beer on', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'command-topic', 'beer off', 2, False) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_off_script(self): """Test with turn off script.""" global TEST_STATE TEST_STATE = False assert setup_component( self.hass, switch.DOMAIN, { 'switch': { 'platform': 'wake_on_lan', 'mac_address': '00-01-02-03-04-05', 'host': 'validhostname', 'turn_off': { 'service': 'shell_command.turn_off_target', }, } }) calls = mock_service(self.hass, 'shell_command', 'turn_off_target') state = self.hass.states.get('switch.wake_on_lan') assert STATE_OFF == state.state TEST_STATE = True common.turn_on(self.hass, 'switch.wake_on_lan') self.hass.block_till_done() state = self.hass.states.get('switch.wake_on_lan') assert STATE_ON == state.state assert len(calls) == 0 TEST_STATE = False common.turn_off(self.hass, 'switch.wake_on_lan') self.hass.block_till_done() state = self.hass.states.get('switch.wake_on_lan') assert STATE_OFF == state.state assert len(calls) == 1
def test_sending_mqtt_commands_and_optimistic(self): """Test the sending MQTT commands in optimistic mode.""" fake_state = ha.State('switch.test', 'on') with patch('homeassistant.components.switch.mqtt.async_get_last_state', return_value=mock_coro(fake_state)): assert setup_component(self.hass, switch.DOMAIN, { switch.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'qos': '2' } }) state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'command-topic', 'beer on', 2, False) self.mock_publish.async_publish.reset_mock() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'command-topic', 'beer off', 2, False) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_off_script(self): """Test with turn off script.""" global TEST_STATE TEST_STATE = False self.assertTrue(setup_component(self.hass, switch.DOMAIN, { 'switch': { 'platform': 'wake_on_lan', 'mac_address': '00-01-02-03-04-05', 'host': 'validhostname', 'turn_off': { 'service': 'shell_command.turn_off_TARGET', }, } })) calls = mock_service(self.hass, 'shell_command', 'turn_off_TARGET') state = self.hass.states.get('switch.wake_on_lan') self.assertEqual(STATE_OFF, state.state) TEST_STATE = True common.turn_on(self.hass, 'switch.wake_on_lan') self.hass.block_till_done() state = self.hass.states.get('switch.wake_on_lan') self.assertEqual(STATE_ON, state.state) assert len(calls) == 0 TEST_STATE = False common.turn_off(self.hass, 'switch.wake_on_lan') self.hass.block_till_done() state = self.hass.states.get('switch.wake_on_lan') self.assertEqual(STATE_OFF, state.state) assert len(calls) == 1
def test_state_json_value(self): """Test with state JSON value.""" with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, 'switch_status') oncmd = json.dumps({'status': 'ok'}) offcmd = json.dumps({'status': 'nope'}) test_switch = { 'command_state': 'cat {}'.format(path), 'command_on': 'echo \'{}\' > {}'.format(oncmd, path), 'command_off': 'echo \'{}\' > {}'.format(offcmd, path), 'value_template': '{{ value_json.status=="ok" }}' } assert setup_component( self.hass, switch.DOMAIN, { 'switch': { 'platform': 'command_line', 'switches': { 'test': test_switch } } }) state = self.hass.states.get('switch.test') assert STATE_OFF == state.state common.turn_on(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') assert STATE_ON == state.state common.turn_off(self.hass, 'switch.test') self.hass.block_till_done() state = self.hass.states.get('switch.test') assert STATE_OFF == state.state