Пример #1
0
    def test_include_exclude_events(self):
        """Test if events are filtered if include and exclude is configured."""
        entity_id = 'switch.bla'
        entity_id2 = 'sensor.blu'
        entity_id3 = 'sensor.bli'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)

        eventA1 = self.create_state_changed_event(pointA, entity_id, 10)
        eventA2 = self.create_state_changed_event(pointA, entity_id2, 10)
        eventA3 = self.create_state_changed_event(pointA, entity_id3, 10)
        eventB1 = self.create_state_changed_event(pointB, entity_id, 20)
        eventB2 = self.create_state_changed_event(pointB, entity_id2, 20)

        config = logbook.CONFIG_SCHEMA({
            ha.DOMAIN: {},
            logbook.DOMAIN: {
                logbook.CONF_INCLUDE: {
                    logbook.CONF_DOMAINS: ['sensor', ],
                    logbook.CONF_ENTITIES: ['switch.bla', ]},
                logbook.CONF_EXCLUDE: {
                    logbook.CONF_DOMAINS: ['switch', ],
                    logbook.CONF_ENTITIES: ['sensor.bli', ]}}})
        events = logbook._exclude_events(
            (ha.Event(EVENT_HOMEASSISTANT_START), eventA1, eventA2, eventA3,
             eventB1, eventB2), config[logbook.DOMAIN])
        entries = list(logbook.humanify(self.hass, events))

        self.assertEqual(3, len(entries))
        self.assert_entry(entries[0], name='Home Assistant', message='started',
                          domain=ha.DOMAIN)
        self.assert_entry(entries[1], pointA, 'blu', domain='sensor',
                          entity_id=entity_id2)
        self.assert_entry(entries[2], pointB, 'blu', domain='sensor',
                          entity_id=entity_id2)
Пример #2
0
def _logbook_filtering(hass, last_changed, last_updated):
    from homeassistant.components import logbook

    entity_id = 'test.entity'

    old_state = {
        'entity_id': entity_id,
        'state': 'off'
    }

    new_state = {
        'entity_id': entity_id,
        'state': 'on',
        'last_updated': last_updated,
        'last_changed': last_changed
    }

    event = core.Event(EVENT_STATE_CHANGED, {
        'entity_id': entity_id,
        'old_state': old_state,
        'new_state': new_state
    })

    events = [event] * 10**5

    start = timer()

    # pylint: disable=protected-access
    events = logbook._exclude_events(events, {})
    list(logbook.humanify(events))

    return timer() - start
Пример #3
0
    def test_include_events_entity(self):
        """Test if events are filtered if entity is included in config."""
        entity_id = 'sensor.bla'
        entity_id2 = 'sensor.blu'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)

        eventA = self.create_state_changed_event(pointA, entity_id, 10)
        eventB = self.create_state_changed_event(pointB, entity_id2, 20)

        config = logbook.CONFIG_SCHEMA({
            ha.DOMAIN: {},
            logbook.DOMAIN: {logbook.CONF_INCLUDE: {
                logbook.CONF_ENTITIES: [entity_id2, ]}}})
        events = logbook._exclude_events(
            (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB),
            config[logbook.DOMAIN])
        entries = list(logbook.humanify(self.hass, events))

        self.assertEqual(2, len(entries))
        self.assert_entry(
            entries[0], name='Home Assistant', message='stopped',
            domain=ha.DOMAIN)
        self.assert_entry(
            entries[1], pointB, 'blu', domain='sensor', entity_id=entity_id2)
Пример #4
0
    def test_exclude_script_events(self):
        """Test if script start can be excluded by entity_id."""
        name = 'My Script Rule'
        domain = 'script'
        entity_id = 'script.my_script'
        entity_id2 = 'script.my_script_2'
        entity_id2 = 'sensor.blu'

        eventA = ha.Event(logbook.EVENT_SCRIPT_STARTED, {
            logbook.ATTR_NAME: name,
            logbook.ATTR_ENTITY_ID: entity_id,
        })
        eventB = ha.Event(logbook.EVENT_SCRIPT_STARTED, {
            logbook.ATTR_NAME: name,
            logbook.ATTR_ENTITY_ID: entity_id2,
        })

        config = logbook.CONFIG_SCHEMA({
            ha.DOMAIN: {},
            logbook.DOMAIN: {logbook.CONF_EXCLUDE: {
                logbook.CONF_ENTITIES: [entity_id, ]}}})
        events = logbook._exclude_events(
            (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB),
            logbook._generate_filter_from_config(config[logbook.DOMAIN]))
        entries = list(logbook.humanify(self.hass, events))

        assert 2 == len(entries)
        self.assert_entry(
            entries[0], name='Home Assistant', message='stopped',
            domain=ha.DOMAIN)
        self.assert_entry(
            entries[1], name=name, domain=domain, entity_id=entity_id2)
Пример #5
0
    def test_exclude_events_domain(self):
        """Test if events are filtered if domain is excluded in config."""
        entity_id = 'switch.bla'
        entity_id2 = 'sensor.blu'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)

        eventA = self.create_state_changed_event(pointA, entity_id, 10)
        eventB = self.create_state_changed_event(pointB, entity_id2, 20)

        config = logbook.CONFIG_SCHEMA({
            ha.DOMAIN: {},
            logbook.DOMAIN: {logbook.CONF_EXCLUDE: {
                logbook.CONF_DOMAINS: ['switch', 'alexa', DOMAIN_HOMEKIT]}}})
        events = logbook._exclude_events(
            (ha.Event(EVENT_HOMEASSISTANT_START),
             ha.Event(EVENT_ALEXA_SMART_HOME),
             ha.Event(EVENT_HOMEKIT_CHANGED), eventA, eventB),
            logbook._generate_filter_from_config(config[logbook.DOMAIN]))
        entries = list(logbook.humanify(self.hass, events))

        assert 2 == len(entries)
        self.assert_entry(entries[0], name='Home Assistant', message='started',
                          domain=ha.DOMAIN)
        self.assert_entry(entries[1], pointB, 'blu', domain='sensor',
                          entity_id=entity_id2)
Пример #6
0
    def test_exclude_auto_groups(self):
        """Test if events of automatically generated groups are filtered."""
        entity_id = 'switch.bla'
        entity_id2 = 'group.switches'
        pointA = dt_util.utcnow()

        eventA = self.create_state_changed_event(pointA, entity_id, 10)
        eventB = self.create_state_changed_event(pointA, entity_id2, 20,
                                                 {'auto': True})

        events = logbook._exclude_events((eventA, eventB), {})
        entries = list(logbook.humanify(self.hass, events))

        self.assertEqual(1, len(entries))
        self.assert_entry(entries[0], pointA, 'bla', domain='switch',
                          entity_id=entity_id)
Пример #7
0
    def test_exclude_attribute_changes(self):
        """Test if events of attribute changes are filtered."""
        entity_id = 'switch.bla'
        entity_id2 = 'switch.blu'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=1)

        eventA = self.create_state_changed_event(pointA, entity_id, 10)
        eventB = self.create_state_changed_event(
            pointA, entity_id2, 20, last_changed=pointA, last_updated=pointB)

        events = logbook._exclude_events((eventA, eventB), {})
        entries = list(logbook.humanify(self.hass, events))

        self.assertEqual(1, len(entries))
        self.assert_entry(entries[0], pointA, 'bla', domain='switch',
                          entity_id=entity_id)
Пример #8
0
    def test_exclude_events_hidden(self):
        """Test if events are excluded if entity is hidden."""
        entity_id = 'sensor.bla'
        entity_id2 = 'sensor.blu'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)

        eventA = self.create_state_changed_event(pointA, entity_id, 10,
                                                 {ATTR_HIDDEN: 'true'})
        eventB = self.create_state_changed_event(pointB, entity_id2, 20)

        events = logbook._exclude_events((ha.Event(EVENT_HOMEASSISTANT_STOP),
                                          eventA, eventB), {})
        entries = list(logbook.humanify(self.hass, events))

        self.assertEqual(2, len(entries))
        self.assert_entry(
            entries[0], name='Home Assistant', message='stopped',
            domain=ha.DOMAIN)
        self.assert_entry(
            entries[1], pointB, 'blu', domain='sensor', entity_id=entity_id2)
Пример #9
0
    def test_exclude_removed_entities(self):
        """Test if events are excluded on last update."""
        entity_id = 'sensor.bla'
        entity_id2 = 'sensor.blu'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)

        eventA = self.create_state_changed_event(pointA, entity_id, 10)
        eventB = self.create_state_changed_event(pointB, entity_id2, 20)
        eventA.data['new_state'] = None

        events = logbook._exclude_events((ha.Event(EVENT_HOMEASSISTANT_STOP),
                                          eventA, eventB), self.EMPTY_CONFIG)
        entries = list(logbook.humanify(events))

        self.assertEqual(2, len(entries))
        self.assert_entry(
            entries[0], name='Home Assistant', message='stopped',
            domain=ha.DOMAIN)
        self.assert_entry(
            entries[1], pointB, 'blu', domain='sensor', entity_id=entity_id2)
Пример #10
0
    def test_exclude_new_entities(self):
        """Test if events are excluded on first update."""
        entity_id = 'sensor.bla'
        entity_id2 = 'sensor.blu'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)

        eventA = self.create_state_changed_event(pointA, entity_id, 10)
        eventB = self.create_state_changed_event(pointB, entity_id2, 20)
        eventA.data['old_state'] = None

        events = logbook._exclude_events(
            (ha.Event(EVENT_HOMEASSISTANT_STOP),
             eventA, eventB),
            logbook._generate_filter_from_config({}))
        entries = list(logbook.humanify(self.hass, events))

        assert 2 == len(entries)
        self.assert_entry(
            entries[0], name='Home Assistant', message='stopped',
            domain=ha.DOMAIN)
        self.assert_entry(
            entries[1], pointB, 'blu', domain='sensor', entity_id=entity_id2)
Пример #11
0
    def test_exclude_automation_events(self):
        """Test if automation entries can be excluded by entity_id."""
        name = 'My Automation Rule'
        message = 'has been triggered'
        domain = 'automation'
        entity_id = 'automation.my_automation_rule'
        entity_id2 = 'automation.my_automation_rule_2'
        entity_id2 = 'sensor.blu'

        eventA = ha.Event(logbook.EVENT_LOGBOOK_ENTRY, {
            logbook.ATTR_NAME: name,
            logbook.ATTR_MESSAGE: message,
            logbook.ATTR_DOMAIN: domain,
            logbook.ATTR_ENTITY_ID: entity_id,
        })
        eventB = ha.Event(logbook.EVENT_LOGBOOK_ENTRY, {
            logbook.ATTR_NAME: name,
            logbook.ATTR_MESSAGE: message,
            logbook.ATTR_DOMAIN: domain,
            logbook.ATTR_ENTITY_ID: entity_id2,
        })

        config = logbook.CONFIG_SCHEMA({
            ha.DOMAIN: {},
            logbook.DOMAIN: {logbook.CONF_EXCLUDE: {
                logbook.CONF_ENTITIES: [entity_id, ]}}})
        events = logbook._exclude_events(
            (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB),
            config[logbook.DOMAIN])
        entries = list(logbook.humanify(self.hass, events))

        self.assertEqual(2, len(entries))
        self.assert_entry(
            entries[0], name='Home Assistant', message='stopped',
            domain=ha.DOMAIN)
        self.assert_entry(
            entries[1], name=name, domain=domain, entity_id=entity_id2)
Пример #12
0
    def test_include_events_domain(self):
        """Test if events are filtered if domain is included in config."""
        entity_id = 'switch.bla'
        entity_id2 = 'sensor.blu'
        pointA = dt_util.utcnow()
        pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)

        event_alexa = ha.Event(EVENT_ALEXA_SMART_HOME, {'request': {
            'namespace': 'Alexa.Discovery',
            'name': 'Discover',
        }})
        event_homekit = ha.Event(EVENT_HOMEKIT_CHANGED, {
            ATTR_ENTITY_ID: 'lock.front_door',
            ATTR_DISPLAY_NAME: 'Front Door',
            ATTR_SERVICE: 'lock',
        })

        eventA = self.create_state_changed_event(pointA, entity_id, 10)
        eventB = self.create_state_changed_event(pointB, entity_id2, 20)

        config = logbook.CONFIG_SCHEMA({
            ha.DOMAIN: {},
            logbook.DOMAIN: {logbook.CONF_INCLUDE: {
                logbook.CONF_DOMAINS: ['sensor', 'alexa', DOMAIN_HOMEKIT]}}})
        events = logbook._exclude_events(
            (ha.Event(EVENT_HOMEASSISTANT_START),
             event_alexa, event_homekit, eventA, eventB),
            logbook._generate_filter_from_config(config[logbook.DOMAIN]))
        entries = list(logbook.humanify(self.hass, events))

        assert 4 == len(entries)
        self.assert_entry(entries[0], name='Home Assistant', message='started',
                          domain=ha.DOMAIN)
        self.assert_entry(entries[1], name='Amazon Alexa', domain='alexa')
        self.assert_entry(entries[2], name='HomeKit', domain=DOMAIN_HOMEKIT)
        self.assert_entry(entries[3], pointB, 'blu', domain='sensor',
                          entity_id=entity_id2)