Example #1
0
    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
Example #2
0
    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_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'})
Example #4
0
    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'])
Example #5
0
    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))
Example #6
0
    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',
            }
        })
Example #7
0
    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,
                            }
                        },
                    }
                },
            )
        )
Example #8
0
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
Example #9
0
 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_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}}))
Example #11
0
    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_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)
Example #13
0
    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))
Example #14
0
    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'))
Example #15
0
    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))
Example #16
0
    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
Example #17
0
    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)
Example #18
0
    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))
Example #19
0
    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_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}}))
Example #21
0
    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_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_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'])
Example #24
0
    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
Example #25
0
    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'])
Example #26
0
    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_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'
         }
     })
Example #28
0
    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))
Example #29
0
    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))
Example #30
0
    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))