def test_setup(self): # Bogus config self.assertFalse(switch.setup(self.hass, {})) self.assertFalse(switch.setup(self.hass, {switch.DOMAIN: {}})) # Test with non-existing component self.assertFalse(switch.setup( self.hass, {switch.DOMAIN: {CONF_PLATFORM: 'nonexisting'}} )) # Test if switch component returns 0 switches test_platform = loader.get_component('switch.test') test_platform.init(True) self.assertEqual( [], test_platform.get_switches(None, None)) self.assertFalse(switch.setup( self.hass, {switch.DOMAIN: {CONF_PLATFORM: 'test'}} )) # Test if we can load 2 platforms loader.set_component('switch.test2', test_platform) test_platform.init(False) self.assertTrue(switch.setup( self.hass, { switch.DOMAIN: {CONF_PLATFORM: 'test'}, '{} 2'.format(switch.DOMAIN): {CONF_PLATFORM: 'test2'}, } ))
def test_template_state_text(self): """"Test the state text of a template.""" assert switch.setup( self.hass, { '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': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.set('switch.test_state', STATE_ON) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_ON state = self.hass.states.set('switch.test_state', STATE_OFF) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_OFF
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_controlling_state_via_topic(self): """Test the controlling state via topic.""" self.assertTrue(switch.setup(self.hass, { 'switch': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_on': 1, 'payload_off': 0 } })) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', '1') self.hass.pool.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, 'state-topic', '0') 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_template_state_text(self): assert switch.setup(self.hass, { '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': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.set('switch.test_state', STATE_ON) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_ON state = self.hass.states.set('switch.test_state', STATE_OFF) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_OFF
def test_template_syntax_error(self): assert switch.setup(self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': { 'value_template': "{% if rubbish %}", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.set('switch.test_state', STATE_ON) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == 'unavailable'
def test_controlling_state_via_topic_and_json_message(self): self.assertTrue(switch.setup(self.hass, { 'switch': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'value_template': '{{ value_json.val }}' } })) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer on"}') self.hass.pool.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, 'state-topic', '{"val":"beer off"}') self.hass.pool.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_off_action(self): assert switch.setup(self.hass, { '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.states.set('switch.test_state', STATE_ON) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_ON core.switch.turn_off(self.hass, 'switch.test_template_switch') self.hass.pool.block_till_done() assert 1 == len(self.calls)
def test_controlling_state_via_topic(self): self.assertTrue(switch.setup(self.hass, { 'switch': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off' } })) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get(ATTR_ASSUMED_STATE)) fire_mqtt_message(self.hass, 'state-topic', 'beer on') self.hass.pool.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_ON, state.state) fire_mqtt_message(self.hass, 'state-topic', 'beer off') self.hass.pool.block_till_done() state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_off_action(self): """Test off action.""" assert switch.setup( self.hass, { '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.states.set('switch.test_state', STATE_ON) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_ON core.switch.turn_off(self.hass, 'switch.test_template_switch') self.hass.pool.block_till_done() assert 1 == len(self.calls)
def test_sending_mqtt_commands_and_optimistic(self): self.assertTrue(switch.setup(self.hass, { 'switch': { '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_OFF, state.state) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) switch.turn_on(self.hass, 'switch.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'beer on', 2, False), self.mock_publish.mock_calls[-1][1]) 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() self.assertEqual(('command-topic', 'beer off', 2, False), self.mock_publish.mock_calls[-1][1]) state = self.hass.states.get('switch.test') self.assertEqual(STATE_OFF, state.state)
def test_no_switches_does_not_create(self): assert switch.setup(self.hass, { 'switch': { 'platform': 'template' } }) assert self.hass.states.all() == []
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(switch.setup(self.hass, { 'switch': { 'platform': 'command_line', '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_template_syntax_error(self): """Test templating syntax error.""" assert switch.setup( self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': { 'value_template': "{% if rubbish %}", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.set('switch.test_state', STATE_ON) self.hass.pool.block_till_done() state = self.hass.states.get('switch.test_template_switch') assert state.state == 'unavailable'
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(switch.setup(self.hass, { 'switch': { 'platform': 'command_line', '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_sending_mqtt_commands_and_optimistic(self): self.assertTrue(switch.setup(self.hass, { 'switch': { '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_OFF, state.state) switch.turn_on(self.hass, 'switch.test') self.hass.pool.block_till_done() self.assertEqual(('command-topic', 'beer on', 2, False), self.mock_publish.mock_calls[-1][1]) 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() self.assertEqual(('command-topic', 'beer off', 2, False), self.mock_publish.mock_calls[-1][1]) 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 = { '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_line', '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_no_switches_does_not_create(self): """Test if there are no switches no creation.""" assert switch.setup(self.hass, { 'switch': { 'platform': 'template' } }) assert self.hass.states.all() == []
def test_invalid_switch_does_not_create(self): assert switch.setup(self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': 'Invalid' } } }) assert self.hass.states.all() == []
def test_setup(self): # Bogus config self.assertFalse(switch.setup(self.hass, {})) self.assertFalse(switch.setup(self.hass, {switch.DOMAIN: {}})) # Test with non-existing component self.assertFalse(switch.setup( self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'nonexisting'}} )) # Test if switch component returns 0 switches mock_toggledevice_platform.init(True) self.assertEqual( [], mock_toggledevice_platform.get_switches(None, None)) self.assertFalse(switch.setup( self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}} ))
def test_invalid_switch_does_not_create(self): assert switch.setup( self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': 'Invalid' } } }) assert self.hass.states.all() == []
def setUp(self): # pylint: disable=invalid-name self.hass = get_test_home_assistant() loader.prepare(self.hass) platform = loader.get_component("switch.test") platform.init() self.assertTrue(switch.setup(self.hass, {switch.DOMAIN: {CONF_PLATFORM: "test"}})) # Switch 1 is ON, switch 2 is OFF self.switch_1, self.switch_2, self.switch_3 = platform.DEVICES
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() platform = loader.get_component('switch.test') platform.init() self.assertTrue(switch.setup( self.hass, {switch.DOMAIN: {CONF_PLATFORM: 'test'}} )) # Switch 1 is ON, switch 2 is OFF self.switch_1, self.switch_2, self.switch_3 = \ platform.DEVICES
def setUp(self): # pylint: disable=invalid-name self.hass = ha.HomeAssistant() loader.prepare(self.hass) loader.set_component('switch.test', mock_toggledevice_platform) mock_toggledevice_platform.init() self.assertTrue(switch.setup( self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}} )) # Switch 1 is ON, switch 2 is OFF self.switch_1, self.switch_2, self.switch_3 = \ mock_toggledevice_platform.get_switches(None, None)
def test_setup(self): # Bogus config self.assertFalse(switch.setup(self.hass, {})) self.assertFalse(switch.setup(self.hass, {switch.DOMAIN: {}})) # Test with non-existing component self.assertFalse( switch.setup(self.hass, {switch.DOMAIN: { ha.CONF_TYPE: 'nonexisting' }})) # Test if switch component returns 0 switches mock_toggledevice_platform.init(True) self.assertEqual([], mock_toggledevice_platform.get_switches(None, None)) self.assertFalse( switch.setup(self.hass, {switch.DOMAIN: { ha.CONF_TYPE: 'test' }}))
def setUp(self): # pylint: disable=invalid-name self.hass = ha.HomeAssistant() loader.prepare(self.hass) loader.set_component('switch.test', mock_toggledevice_platform) mock_toggledevice_platform.init() self.assertTrue( switch.setup(self.hass, {switch.DOMAIN: { ha.CONF_TYPE: 'test' }})) # Switch 1 is ON, switch 2 is OFF self.switch_1, self.switch_2, self.switch_3 = \ mock_toggledevice_platform.get_switches(None, None)
def setUp(self): # pylint: disable=invalid-name self.hass = get_test_home_assistant() loader.prepare(self.hass) platform = loader.get_component('switch.test') platform.init() self.assertTrue(switch.setup( self.hass, {switch.DOMAIN: {CONF_PLATFORM: 'test'}} )) # Switch 1 is ON, switch 2 is OFF self.switch_1, self.switch_2, self.switch_3 = \ platform.get_switches(None, None)
def test_setup_two_platforms(self): """ Test with bad config. """ # Test if switch component returns 0 switches test_platform = loader.get_component('switch.test') test_platform.init(True) loader.set_component('switch.test2', test_platform) test_platform.init(False) self.assertTrue(switch.setup( self.hass, { switch.DOMAIN: {CONF_PLATFORM: 'test'}, '{} 2'.format(switch.DOMAIN): {CONF_PLATFORM: 'test2'}, } ))
def test_setup_two_platforms(self): """ Test with bad config. """ # Test if switch component returns 0 switches test_platform = loader.get_component("switch.test") test_platform.init(True) loader.set_component("switch.test2", test_platform) test_platform.init(False) self.assertTrue( switch.setup( self.hass, {switch.DOMAIN: {CONF_PLATFORM: "test"}, "{} 2".format(switch.DOMAIN): {CONF_PLATFORM: "test2"}}, ) )
def test_invalid_name_does_not_create(self): assert switch.setup(self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test INVALID switch': { 'value_template': "{{ rubbish }", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) assert self.hass.states.all() == []
def test_missing_off_does_not_create(self): assert switch.setup(self.hass, { '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' }, 'not_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) assert self.hass.states.all() == []
def test_invalid_name_does_not_create(self): assert switch.setup( self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test INVALID switch': { 'value_template': "{{ rubbish }", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) assert self.hass.states.all() == []
def test_missing_off_does_not_create(self): assert switch.setup( self.hass, { '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' }, 'not_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) assert self.hass.states.all() == []
def test_template_state_boolean_off(self): assert switch.setup( self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': { 'value_template': "{{ 1 == 2 }}", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_OFF
def test_template_state_boolean_off(self): assert switch.setup(self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': { 'value_template': "{{ 1 == 2 }}", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_OFF
def test_template_state_boolean_on(self): """Test the setting of the state with boolean on.""" assert switch.setup( self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': { 'value_template': "{{ 1 == 1 }}", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_ON
def test_template_state_boolean_on(self): """Test the setting of the state with boolean on.""" assert switch.setup(self.hass, { 'switch': { 'platform': 'template', 'switches': { 'test_template_switch': { 'value_template': "{{ 1 == 1 }}", 'turn_on': { 'service': 'switch.turn_on', 'entity_id': 'switch.test_state' }, 'turn_off': { 'service': 'switch.turn_off', 'entity_id': 'switch.test_state' }, } } } }) state = self.hass.states.get('switch.test_template_switch') assert state.state == STATE_ON
def test_no_switches_does_not_create(self): assert switch.setup(self.hass, {'switch': {'platform': 'template'}}) assert self.hass.states.all() == []
def test_no_switches_does_not_create(self): """Test if there are no switches no creation.""" assert switch.setup(self.hass, {'switch': {'platform': 'template'}}) assert self.hass.states.all() == []