def test_sunset_trigger_with_offset(self): """Test the sunset trigger with offset.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_SETTING: '2015-09-16T02:00:00Z', }) now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 2, 30, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=now): _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunset', 'offset': '0:30:00' }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join(( 'platform', 'event', 'offset')) }, } } }) fire_time_changed(self.hass, trigger_time) self.hass.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('sun - sunset - 0:30:00', self.calls[0].data['some'])
def test_validate_component_config(self): """Test validating component configuration.""" config_schema = vol.Schema({ 'comp_conf': { 'hello': str } }, required=True) loader.set_component( 'comp_conf', MockModule('comp_conf', config_schema=config_schema)) assert not bootstrap._setup_component(self.hass, 'comp_conf', {}) assert not bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': None }) assert not bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': {} }) assert not bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': { 'hello': 'world', 'invalid': 'extra', } }) assert bootstrap._setup_component(self.hass, 'comp_conf', { 'comp_conf': { 'hello': 'world', } })
def test_if_action_after(self): """Test if action was after.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z', }) _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': { 'condition': 'sun', 'after': 'sunrise', }, 'action': { 'service': 'test.automation' } } }) now = datetime(2015, 9, 16, 13, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.now', return_value=now): self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(0, len(self.calls)) now = datetime(2015, 9, 16, 15, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.now', return_value=now): self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_sunrise_trigger(self): """Test the sunrise trigger.""" self.hass.states.set(sun.ENTITY_ID, sun.STATE_ABOVE_HORIZON, { sun.STATE_ATTR_NEXT_RISING: '2015-09-16T14:00:00Z', }) now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC) trigger_time = datetime(2015, 9, 16, 14, tzinfo=dt_util.UTC) with patch('homeassistant.util.dt.utcnow', return_value=now): _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'sun', 'event': 'sunrise', }, 'action': { 'service': 'test.automation', } } }) fire_time_changed(self.hass, trigger_time) self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_fire_event_sensor(self, mock_sleep): """Test fire event.""" self.assertTrue(_setup_component(self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'dummy': True} })) self.assertTrue(_setup_component(self.hass, 'sensor', { 'sensor': {'platform': 'rfxtrx', 'automatic_add': True, 'devices': {'0a520802060100ff0e0269': { 'name': 'Test', rfxtrx.ATTR_FIREEVENT: True} }}})) calls = [] def record_event(event): """Add recorded event to set.""" calls.append(event) self.hass.bus.listen("signal_received", record_event) self.hass.block_till_done() event = rfxtrx.get_rfx_object('0a520802060101ff0f0269') event.data = bytearray(b'\nR\x08\x01\x07\x01\x00\xb8\x1b\x02y') rfxtrx.RECEIVED_EVT_SUBSCRIBERS[0](event) self.hass.block_till_done() self.assertEqual(1, len(calls)) self.assertEqual(calls[0].data, {'entity_id': 'sensor.test'})
def test_valid_config(self): """Test configuration.""" self.assertTrue( _setup_component( self.hass, "light", { "light": { "platform": "rfxtrx", "automatic_add": True, "devices": {"0b1100cd0213c7f210010f51": {"name": "Test", rfxtrx_core.ATTR_FIREEVENT: True}}, } }, ) ) self.assertTrue( _setup_component( self.hass, "light", { "light": { "platform": "rfxtrx", "automatic_add": True, "devices": { "213c7f216": { "name": "Test", "packetid": "0b1100cd0213c7f210010f51", "signal_repetitions": 3, } }, } }, ) )
def test_reloading_groups(self): """Test reloading the group config.""" _setup_component(self.hass, 'group', {'group': { 'second_group': { 'entities': 'light.Bowl', 'icon': 'mdi:work', 'view': True, }, 'test_group': 'hello.world,sensor.happy', 'empty_group': {'name': 'Empty Group', 'entities': None}, } }) assert sorted(self.hass.states.entity_ids()) == \ ['group.empty_group', 'group.second_group', 'group.test_group'] assert self.hass.bus.listeners['state_changed'] == 3 with patch('homeassistant.config.load_yaml_config_file', return_value={ 'group': { 'hello': { 'entities': 'light.Bowl', 'icon': 'mdi:work', 'view': True, }}}): group.reload(self.hass) self.hass.block_till_done() assert self.hass.states.entity_ids() == ['group.hello'] assert self.hass.bus.listeners['state_changed'] == 1
def mock_mqtt_component(hass, mock_mqtt): """Mock the MQTT component.""" _setup_component(hass, mqtt.DOMAIN, { mqtt.DOMAIN: { mqtt.CONF_BROKER: 'mock-broker', } }) return mock_mqtt
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() db_uri = 'sqlite://' # In memory DB _setup_component(self.hass, recorder.DOMAIN, { recorder.DOMAIN: {recorder.CONF_DB_URL: db_uri}}) self.hass.start() recorder._verify_instance() self.session = recorder.Session() recorder._INSTANCE.block_till_done()
def test_component_not_setup_missing_dependencies(self): """Test we do not setup a component if not all dependencies loaded.""" deps = ['non_existing'] loader.set_component('comp', MockModule('comp', dependencies=deps)) assert not bootstrap._setup_component(self.hass, 'comp', None) assert 'comp' not in self.hass.config.components self.hass.config.components.append('non_existing') assert bootstrap._setup_component(self.hass, 'comp', None)
def test_invalid_config(self): """Test configuration.""" self.assertFalse(_setup_component(self.hass, 'rfxtrx', { 'rfxtrx': {} })) self.assertFalse(_setup_component(self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'invalid_key': True}}))
def test_default_config(self, mock_sleep): """Test configuration.""" self.assertTrue(_setup_component(self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'dummy': True} })) self.assertTrue(_setup_component(self.hass, 'sensor', { 'sensor': {'platform': 'rfxtrx', 'automatic_add': True, 'devices': {}}})) self.assertEqual(len(rfxtrx.RFXOBJECT.sensors()), 2)
def test_if_fires_on_entity_change(self): """Test for firing on entity change.""" self.hass.states.set('test.entity', 'hello') self.hass.pool.block_till_done() assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'state', 'entity_id': 'test.entity', }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join(( 'platform', 'entity_id', 'from_state.state', 'to_state.state', 'for')) }, } } }) self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual( 'state - test.entity - hello - world - None', self.calls[0].data['some']) automation.turn_off(self.hass) self.hass.pool.block_till_done() self.hass.states.set('test.entity', 'planet') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action_after(self): """Test for if action after.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'after': '10:00', }, 'action': { 'service': 'test.automation' } } }) before_10 = dt_util.now().replace(hour=8) after_10 = dt_util.now().replace(hour=14) with patch('homeassistant.helpers.condition.dt_util.now', return_value=before_10): self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(0, len(self.calls)) with patch('homeassistant.helpers.condition.dt_util.now', return_value=after_10): self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_action(self): """Test for firing if action.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': [{ 'condition': 'template', 'value_template': '{{ is_state("test.entity", "world") }}' }], 'action': { 'service': 'test.automation' } } }) # Condition is not true yet self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(0, len(self.calls)) # Change condition to true, but it shouldn't be triggered yet self.hass.states.set('test.entity', 'world') self.hass.block_till_done() self.assertEqual(0, len(self.calls)) # Condition is true and event is triggered self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_component_failing_setup(self): """Test component that fails setup.""" loader.set_component( 'comp', MockModule('comp', setup=lambda hass, config: False)) assert not bootstrap._setup_component(self.hass, 'comp', None) assert 'comp' not in self.hass.config.components
def test_config_not_valid_service_names(self): """Test if config contains invalid service names.""" assert not _setup_component(self.hass, shell_command.DOMAIN, { shell_command.DOMAIN: { 'this is invalid because space': 'touch bla.txt' } })
def test_template_string(self): """"Test template string.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'value_template': '{{ state.attributes.test_attribute | multiply(10) }}', 'below': 10, }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join(( 'platform', 'entity_id', 'below', 'above', 'from_state.state', 'to_state.state')) }, } } }) self.hass.states.set('test.entity', 'test state 1', {'test_attribute': '1.2'}) self.hass.pool.block_till_done() self.hass.states.set('test.entity', 'test state 2', {'test_attribute': '0.9'}) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual( 'numeric_state - test.entity - 10.0 - None - test state 1 - ' 'test state 2', self.calls[0].data['some'])
def test_if_fires_on_entity_change_below(self): """"Test the firing with changed entity.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'below': 10, }, 'action': { 'service': 'test.automation' } } }) # 9 is below 10 self.hass.states.set('test.entity', 9) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) # Set above 12 so the automation will fire again self.hass.states.set('test.entity', 12) automation.turn_off(self.hass) self.hass.pool.block_till_done() self.hass.states.set('test.entity', 9) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition_with_invalid_condition(self): """Event is not a valid condition.""" entity_id = 'test.entity' self.hass.states.set(entity_id, 100) assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': [ { 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 } ], 'condition': 'use_trigger_values', 'action': { 'service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_fires_on_for_condition(self): """Test for firing if contition is on.""" point1 = dt_util.utcnow() point2 = point1 + timedelta(seconds=10) with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow: mock_utcnow.return_value = point1 self.hass.states.set('test.entity', 'on') assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': { 'platform': 'state', 'entity_id': 'test.entity', 'state': 'on', 'for': { 'seconds': 5 }, }, 'action': {'service': 'test.automation'}, } }) # not enough time has passed self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Time travel 10 secs into the future mock_utcnow.return_value = point2 self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_automation_calling_two_actions(self): """Test if we can call two actions from automation definition.""" self.assertTrue(_setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': [{ 'service': 'test.automation', 'data': {'position': 0}, }, { 'service': 'test.automation', 'data': {'position': 1}, }], } })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() assert len(self.calls) == 2 assert self.calls[0].data['position'] == 0 assert self.calls[1].data['position'] == 1
def test_automation_list_setting(self): """Event is not a valid condition.""" self.assertTrue(_setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: [{ 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', } }, { 'trigger': { 'platform': 'event', 'event_type': 'test_event_2', }, 'action': { 'service': 'test.automation', } }] })) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.bus.fire('test_event_2') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_several_rollershutters(self): """Test with 3 roller shutters.""" self.assertTrue(_setup_component(self.hass, 'rollershutter', { 'rollershutter': {'platform': 'rfxtrx', 'signal_repetitions': 3, 'devices': {'0b1100cd0213c7f230010f71': { 'name': 'Test'}, '0b1100100118cdea02010f70': { 'name': 'Bath'}, '0b1100101118cdea02010f70': { 'name': 'Living'} }}})) self.assertEqual(3, len(rfxtrx_core.RFX_DEVICES)) device_num = 0 for id in rfxtrx_core.RFX_DEVICES: entity = rfxtrx_core.RFX_DEVICES[id] self.assertEqual(entity.signal_repetitions, 3) if entity.name == 'Living': device_num = device_num + 1 elif entity.name == 'Bath': device_num = device_num + 1 elif entity.name == 'Test': device_num = device_num + 1 self.assertEqual(3, device_num)
def test_no_color_or_brightness_if_no_config(self): \ # pylint: disable=invalid-name """Test if there is no color and brightness if they aren't defined.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'state_topic': 'test_light_rgb', 'command_topic': 'test_light_rgb/set', } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness')) fire_mqtt_message(self.hass, 'test_light_rgb', '{"state":"ON"}') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertIsNone(state.attributes.get('rgb_color')) self.assertIsNone(state.attributes.get('brightness'))
def test_valid_config(self, mock_sleep): """Test configuration.""" self.assertTrue(_setup_component(self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'dummy': True}})) self.hass.config.components.remove('rfxtrx') self.assertTrue(_setup_component(self.hass, 'rfxtrx', { 'rfxtrx': { 'device': '/dev/serial/by-id/usb' + '-RFXCOM_RFXtrx433_A1Y0NJGR-if00-port0', 'dummy': True, 'debug': True}}))
def test_if_fires_on_no_change_with_template_advanced(self): """Test for firing on no change with template advanced.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '''{%- if is_state("test.entity", "world") -%} true {%- else -%} false {%- endif -%}''', }, 'action': { 'service': 'test.automation' } } }) # Different state self.hass.states.set('test.entity', 'worldz') self.hass.block_till_done() self.assertEqual(0, len(self.calls)) # Different state self.hass.states.set('test.entity', 'hello') self.hass.block_till_done() self.assertEqual(0, len(self.calls))
def test_two_triggers(self): """Test triggers.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': [ { 'platform': 'event', 'event_type': 'test_event', }, { 'platform': 'state', 'entity_id': 'test.entity', } ], 'action': { 'service': 'test.automation', } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set('test.entity', 'hello') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_if_fires_on_change_with_template_advanced(self): """Test for firing on change with template advanced.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '''{%- if is_state("test.entity", "world") -%} true {%- else -%} false {%- endif -%}''', }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join(( 'platform', 'entity_id', 'from_state.state', 'to_state.state')) }, } } }) self.hass.block_till_done() self.calls = [] self.hass.states.set('test.entity', 'world') self.hass.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual( 'template - test.entity - hello - world', self.calls[0].data['some'])
def test_if_action_one_weekday(self): """Test for if action with one weekday.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event' }, 'condition': { 'platform': 'time', 'weekday': 'mon', }, 'action': { 'service': 'test.automation' } } }) days_past_monday = dt_util.now().weekday() monday = dt_util.now() - timedelta(days=days_past_monday) tuesday = monday + timedelta(days=1) with patch('homeassistant.helpers.condition.dt_util.now', return_value=monday): self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls)) with patch('homeassistant.helpers.condition.dt_util.now', return_value=tuesday): self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_not_fires_on_entity_change_with_not_attribute_below(self): """"Test attributes change.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'value_template': '{{ state.attributes.test_attribute }}', 'below': 10, }, 'action': { 'service': 'test.automation' } } }) # 11 is not below 10, entity state value should not be tested self.hass.states.set('test.entity', 'entity') self.hass.block_till_done() self.assertEqual(0, len(self.calls))
def test_transition(self): """Test for transition time being sent when included.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt_json', 'name': 'test', 'state_topic': 'test_light_rgb', 'command_topic': 'test_light_rgb/set', 'qos': 0 } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) light.turn_on(self.hass, 'light.test', transition=10) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.mock_calls[-1][1][0]) self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2]) self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3]) # Get the sent message message_json = json.loads(self.mock_publish.mock_calls[-1][1][1]) self.assertEqual(10, message_json["transition"]) self.assertEqual("ON", message_json["state"]) # Transition back off light.turn_off(self.hass, 'light.test', transition=10) self.hass.block_till_done() self.assertEqual('test_light_rgb/set', self.mock_publish.mock_calls[-1][1][0]) self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2]) self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3]) # Get the sent message message_json = json.loads(self.mock_publish.mock_calls[-1][1][1]) self.assertEqual(10, message_json["transition"]) self.assertEqual("OFF", message_json["state"])
def test_if_action(self): """"Test if action.""" entity_id = 'domain.test_entity' test_state = 10 assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': { 'condition': 'numeric_state', 'entity_id': entity_id, 'above': test_state, 'below': test_state + 2 }, 'action': { 'service': 'test.automation' } } }) self.hass.states.set(entity_id, test_state) self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, test_state - 1) self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, test_state + 1) self.hass.bus.fire('test_event') self.hass.block_till_done() self.assertEqual(2, len(self.calls))
def test_if_fires_on_change_with_template_2(self): """Test for firing on change with template.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '{{ not is_state("test.entity", "world") }}', }, 'action': { 'service': 'test.automation' } } }) self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) self.hass.states.set('test.entity', 'home') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set('test.entity', 'work') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set('test.entity', 'not_home') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set('test.entity', 'home') self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_one_rollershutter(self): """Test with 1 roller shutter.""" self.assertTrue(_setup_component(self.hass, 'rollershutter', { 'rollershutter': {'platform': 'rfxtrx', 'devices': {'0b1400cd0213c7f210010f51': { 'name': 'Test' }}}})) import RFXtrx as rfxtrxmod rfxtrx_core.RFXOBJECT =\ rfxtrxmod.Core("", transport_protocol=rfxtrxmod.DummyTransport) self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES)) for id in rfxtrx_core.RFX_DEVICES: entity = rfxtrx_core.RFX_DEVICES[id] self.assertEqual(entity.signal_repetitions, 1) self.assertFalse(entity.should_fire_event) self.assertFalse(entity.should_poll) entity.move_up() entity.move_down() entity.stop()
def test_template_string(self): """"Test template string.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'value_template': '{{ state.attributes.test_attribute | multiply(10) }}', 'below': 10, }, 'action': { 'service': 'test.automation' } } }) # 9 is below 10 self.hass.states.set('test.entity', 'entity', {'test_attribute': '0.9'}) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_fires_when_all_matches(self): """Test for firing if everything matches.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'time', 'hours': 1, 'minutes': 2, 'seconds': 3, }, 'action': { 'service': 'test.automation' } } }) fire_time_changed(self.hass, dt_util.utcnow().replace(hour=1, minute=2, second=3)) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_controlling_state_via_topic_with_templates(self): """Test the setting og the state with a template.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'test_light_rgb/status', 'command_topic': 'test_light_rgb/set', 'brightness_state_topic': 'test_light_rgb/brightness/status', 'color_temp_state_topic': 'test_light_rgb/color_temp/status', 'rgb_state_topic': 'test_light_rgb/rgb/status', 'state_value_template': '{{ value_json.hello }}', 'brightness_value_template': '{{ value_json.hello }}', 'color_temp_value_template': '{{ value_json.hello }}', 'rgb_value_template': '{{ value_json.hello | join(",") }}', } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('brightness')) self.assertIsNone(state.attributes.get('rgb_color')) fire_mqtt_message(self.hass, 'test_light_rgb/rgb/status', '{"hello": [1, 2, 3]}') fire_mqtt_message(self.hass, 'test_light_rgb/status', '{"hello": "ON"}') fire_mqtt_message(self.hass, 'test_light_rgb/brightness/status', '{"hello": "50"}') fire_mqtt_message(self.hass, 'test_light_rgb/color_temp/status', '{"hello": "300"}') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(50, state.attributes.get('brightness')) self.assertEqual([1, 2, 3], state.attributes.get('rgb_color')) self.assertEqual(300, state.attributes.get('color_temp'))
def test_calling_service_template(self): """Test the calling of a service.""" calls = [] def record_call(service): """Add recorded event to set.""" calls.append(service) self.hass.services.register('test', 'script', record_call) assert _setup_component(self.hass, 'script', { 'script': { 'test': { 'sequence': [{ 'service_template': """ {% if True %} test.script {% else %} test.not_script {% endif %}""", 'data_template': { 'hello': """ {% if True %} world {% else %} Not world {% endif %} """ } }] } } }) script.turn_on(self.hass, ENTITY_ID) self.hass.pool.block_till_done() self.assertEqual(1, len(calls)) self.assertEqual('world', calls[0].data.get('hello'))
def test_if_fires_on_zone_enter(self): """Test for firing on zone enter.""" self.hass.states.set('test.entity', 'hello', { 'latitude': 32.881011, 'longitude': -117.234758 }) self.hass.pool.block_till_done() assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'zone', 'entity_id': 'test.entity', 'zone': 'zone.test', 'event': 'enter', }, 'action': { 'service': 'test.automation', 'data_template': { 'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join(( 'platform', 'entity_id', 'from_state.state', 'to_state.state', 'zone.name')) }, } } }) self.hass.states.set('test.entity', 'hello', { 'latitude': 32.880586, 'longitude': -117.237564 }) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual( 'zone - test.entity - hello - hello - test', self.calls[0].data['some'])
def test_if_not_fires_if_from_filter_not_match(self): """Test for not firing if from filter is not a match.""" self.hass.states.set('test.entity', 'bye') assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'state', 'entity_id': 'test.entity', 'from': 'hello', 'to': 'world' }, 'action': { 'service': 'test.automation' } } }) self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_template_list(self): """"Test template list.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'value_template': '{{ state.attributes.test_attribute[2] }}', 'below': 10, }, 'action': { 'service': 'test.automation' } } }) # 3 is below 10 self.hass.states.set('test.entity', 'entity', {'test_attribute': [11, 15, 3]}) self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_using_trigger_as_condition(self): """Test triggers as condition.""" entity_id = 'test.entity' assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': [{ 'platform': 'state', 'entity_id': entity_id, 'from': '120', 'state': '100' }, { 'platform': 'numeric_state', 'entity_id': entity_id, 'below': 150 }], 'condition': 'use_trigger_values', 'action': { 'service': 'test.automation', } } }) self.hass.states.set(entity_id, 100) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, 120) self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.hass.states.set(entity_id, 100) self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls)) self.hass.states.set(entity_id, 151) self.hass.pool.block_till_done() self.assertEqual(2, len(self.calls))
def test_passing_variables(self): """Test different ways of passing in variables.""" calls = [] def record_call(service): """Add recorded event to set.""" calls.append(service) self.hass.services.register('test', 'script', record_call) assert _setup_component( self.hass, 'script', { 'script': { 'test': { 'sequence': { 'service': 'test.script', 'data_template': { 'hello': '{{ greeting }}', }, }, }, }, }) script.turn_on(self.hass, ENTITY_ID, {'greeting': 'world'}) self.hass.pool.block_till_done() assert len(calls) == 1 assert calls[-1].data['hello'] == 'world' self.hass.services.call('script', 'test', { 'greeting': 'universe', }) self.hass.pool.block_till_done() assert len(calls) == 2 assert calls[-1].data['hello'] == 'universe'
def test_service_specify_data(self): """Test service data.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'action': { 'service': 'test.automation', 'data': { 'some': 'data' } } } }) self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) self.assertEqual('data', self.calls[0].data['some'])
def test_old_config_sensor(self): """Test with 1 sensor.""" self.assertTrue( _setup_component( self.hass, 'sensor', { 'sensor': { 'platform': 'rfxtrx', 'devices': { 'sensor_0502': { 'name': 'Test', 'packetid': '0a52080705020095220269', 'data_type': 'Temperature' } } } })) self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES)) entity = rfxtrx_core.RFX_DEVICES['sensor_0502']['Temperature'] self.assertEqual('Test', entity.name) self.assertEqual(TEMP_CELSIUS, entity.unit_of_measurement) self.assertEqual(None, entity.state)
def test_if_not_fires_on_change_with_template(self): """Test for not firing on change with template.""" assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '{{ is_state("test.entity", "hello") }}', }, 'action': { 'service': 'test.automation' } } }) self.hass.block_till_done() self.calls = [] self.hass.states.set('test.entity', 'world') self.hass.block_till_done() assert len(self.calls) == 0
def test_if_not_fires_on_entity_change_below_to_below(self): """"Test the firing with changed entity.""" self.hass.states.set('test.entity', 9) self.hass.pool.block_till_done() assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'below': 10, }, 'action': { 'service': 'test.automation' } } }) # 9 is below 10 so this should not fire again self.hass.states.set('test.entity', 8) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_if_fires_on_for_condition(self): """Test for firing if contition is on.""" point1 = dt_util.utcnow() point2 = point1 + timedelta(seconds=10) with patch('homeassistant.core.dt_util.utcnow') as mock_utcnow: mock_utcnow.return_value = point1 self.hass.states.set('test.entity', 'on') assert _setup_component( self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'event', 'event_type': 'test_event', }, 'condition': { 'platform': 'state', 'entity_id': 'test.entity', 'state': 'on', 'for': { 'seconds': 5 }, }, 'action': { 'service': 'test.automation' }, } }) # not enough time has passed self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls)) # Time travel 10 secs into the future mock_utcnow.return_value = point2 self.hass.bus.fire('test_event') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_alt_delay(self): """Test alternative delay config format.""" event = 'test_event' calls = [] def record_event(event): """Add recorded event to set.""" calls.append(event) self.hass.bus.listen(event, record_event) assert _setup_component(self.hass, 'script', { 'script': { 'test': { 'sequence': [{ 'event': event, }, { 'delay': None, 'seconds': 5 }, { 'event': event, }] } } }) script.turn_on(self.hass, ENTITY_ID) self.hass.pool.block_till_done() self.assertTrue(script.is_on(self.hass, ENTITY_ID)) self.assertEqual(1, len(calls)) future = dt_util.utcnow() + timedelta(seconds=5) fire_time_changed(self.hass, future) self.hass.pool.block_till_done() self.assertFalse(script.is_on(self.hass, ENTITY_ID)) self.assertEqual(2, len(calls))
def test_if_fires_on_two_change(self): """Test for firing on two changes.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'template', 'value_template': '{{ true }}', }, 'action': { 'service': 'test.automation' } } }) # Trigger once self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls)) # Trigger again self.hass.states.set('test.entity', 'world') self.hass.pool.block_till_done() self.assertEqual(1, len(self.calls))
def test_if_fires_on_entity_change_over_to_below_above_range(self): """"Test the firing with changed entity.""" self.hass.states.set('test.entity', 11) self.hass.pool.block_till_done() assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'below': 10, 'above': 5, }, 'action': { 'service': 'test.automation' } } }) # 4 is below 5 so it should not fire self.hass.states.set('test.entity', 4) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_if_not_fires_on_entity_change_above_to_above(self): """"Test the firing with changed entity.""" # set initial state self.hass.states.set('test.entity', 11) self.hass.pool.block_till_done() assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'numeric_state', 'entity_id': 'test.entity', 'above': 10, }, 'action': { 'service': 'test.automation' } } }) # 11 is above 10 so this should fire again self.hass.states.set('test.entity', 12) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_show_color_temp_only_if_command_topic(self): """Test the color temp only if a command topic is present.""" self.hass.config.components = ['mqtt'] assert _setup_component(self.hass, light.DOMAIN, { light.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'color_temp_command_topic': 'test_light_rgb/brightness/set', 'command_topic': 'test_light_rgb/set', 'state_topic': 'test_light_rgb/status' } }) state = self.hass.states.get('light.test') self.assertEqual(STATE_OFF, state.state) self.assertIsNone(state.attributes.get('color_temp')) fire_mqtt_message(self.hass, 'test_light_rgb/status', 'ON') self.hass.block_till_done() state = self.hass.states.get('light.test') self.assertEqual(STATE_ON, state.state) self.assertEqual(150, state.attributes.get('color_temp'))
def test_if_fires_on_entity_change_with_for(self): """Test for firing on entity change with for.""" assert _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'state', 'entity_id': 'test.entity', 'to': 'world', 'for': { 'seconds': 5 }, }, 'action': { 'service': 'test.automation' } } }) self.hass.states.set('test.entity', 'world') self.hass.block_till_done() fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10)) self.hass.block_till_done() self.assertEqual(1, len(self.calls))
def test_update_state_via_state_topic(self): """Test updating with via state topic.""" self.hass.config.components = ['mqtt'] assert _setup_component( self.hass, alarm_control_panel.DOMAIN, { alarm_control_panel.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', } }) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_UNKNOWN, self.hass.states.get(entity_id).state) for state in (STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY, STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED): fire_mqtt_message(self.hass, 'alarm/state', state) self.hass.pool.block_till_done() self.assertEqual(state, self.hass.states.get(entity_id).state)
def test_if_not_fires_using_wrong_after(self): """YAML translates time values to total seconds. This should break the before rule. """ assert not _setup_component(self.hass, automation.DOMAIN, { automation.DOMAIN: { 'trigger': { 'platform': 'time', 'after': 3605, # Total seconds. Hour = 3600 second }, 'action': { 'service': 'test.automation' } } }) fire_time_changed(self.hass, dt_util.utcnow().replace( hour=1, minute=0, second=5)) self.hass.pool.block_till_done() self.assertEqual(0, len(self.calls))
def test_one_switch(self): """Test with 1 switch.""" self.assertTrue(_setup_component(self.hass, 'switch', { 'switch': {'platform': 'rfxtrx', 'devices': {'0b1100cd0213c7f210010f51': { 'name': 'Test'}}}})) import RFXtrx as rfxtrxmod rfxtrx_core.RFXOBJECT =\ rfxtrxmod.Core("", transport_protocol=rfxtrxmod.DummyTransport) self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES)) entity = rfxtrx_core.RFX_DEVICES['213c7f216'] self.assertEqual('Test', entity.name) self.assertEqual('off', entity.state) self.assertTrue(entity.assumed_state) self.assertEqual(entity.signal_repetitions, 1) self.assertFalse(entity.should_fire_event) self.assertFalse(entity.should_poll) self.assertFalse(entity.is_on) entity.turn_on() self.assertTrue(entity.is_on) entity.turn_off() self.assertFalse(entity.is_on) entity_id = rfxtrx_core.RFX_DEVICES['213c7f216'].entity_id entity_hass = self.hass.states.get(entity_id) self.assertEqual('Test', entity_hass.name) self.assertEqual('off', entity_hass.state) entity.turn_on() entity_hass = self.hass.states.get(entity_id) self.assertEqual('on', entity_hass.state) entity.turn_off() entity_hass = self.hass.states.get(entity_id) self.assertEqual('off', entity_hass.state)
def test_discover_rollershutter(self): """Test with discovery of roller shutters.""" self.assertTrue(_setup_component(self.hass, 'rollershutter', { 'rollershutter': {'platform': 'rfxtrx', 'automatic_add': True, 'devices': {}}})) event = rfxtrx_core.get_rfx_object('0a140002f38cae010f0070') event.data = bytearray([0x0A, 0x14, 0x00, 0x02, 0xF3, 0x8C, 0xAE, 0x01, 0x0F, 0x00, 0x70]) for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS: evt_sub(event) self.assertEqual(1, len(rfxtrx_core.RFX_DEVICES)) event = rfxtrx_core.get_rfx_object('0a1400adf394ab020e0060') event.data = bytearray([0x0A, 0x14, 0x00, 0xAD, 0xF3, 0x94, 0xAB, 0x02, 0x0E, 0x00, 0x60]) for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS: evt_sub(event) self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES)) # Trying to add a sensor event = rfxtrx_core.get_rfx_object('0a52085e070100b31b0279') event.data = bytearray(b'\nR\x08^\x07\x01\x00\xb3\x1b\x02y') for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS: evt_sub(event) self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES)) # Trying to add a light event = rfxtrx_core.get_rfx_object('0b1100100118cdea02010f70') event.data = bytearray([0x0b, 0x11, 0x11, 0x10, 0x01, 0x18, 0xcd, 0xea, 0x01, 0x02, 0x0f, 0x70]) for evt_sub in rfxtrx_core.RECEIVED_EVT_SUBSCRIBERS: evt_sub(event) self.assertEqual(2, len(rfxtrx_core.RFX_DEVICES))
def test_controlling_state_via_topic(self): """Test the controlling state via topic.""" self.hass.config.components = ['mqtt'] assert _setup_component( self.hass, rollershutter.DOMAIN, { rollershutter.DOMAIN: { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'state-topic', 'command_topic': 'command-topic', 'qos': 0, 'payload_up': 'UP', 'payload_down': 'DOWN', 'payload_stop': 'STOP' } }) state = self.hass.states.get('rollershutter.test') self.assertEqual(STATE_UNKNOWN, state.state) fire_mqtt_message(self.hass, 'state-topic', '0') self.hass.pool.block_till_done() state = self.hass.states.get('rollershutter.test') self.assertEqual(STATE_CLOSED, state.state) fire_mqtt_message(self.hass, 'state-topic', '50') self.hass.pool.block_till_done() state = self.hass.states.get('rollershutter.test') self.assertEqual(STATE_OPEN, state.state) fire_mqtt_message(self.hass, 'state-topic', '100') self.hass.pool.block_till_done() state = self.hass.states.get('rollershutter.test') self.assertEqual(STATE_OPEN, state.state)