예제 #1
0
    def test_if_fires_on_event(self):
        """Test the firing of events."""
        context = Context()

        assert setup_component(
            self.hass, automation.DOMAIN, {
                automation.DOMAIN: {
                    'trigger': {
                        'platform': 'event',
                        'event_type': 'test_event',
                    },
                    'action': {
                        'service': 'test.automation',
                    }
                }
            })

        self.hass.bus.fire('test_event', context=context)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
        assert self.calls[0].context is context

        common.turn_off(self.hass)
        self.hass.block_till_done()

        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #2
0
    def test_sunset_trigger(self):
        """Test the sunset trigger."""
        now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
        trigger_time = datetime(2015, 9, 16, 2, 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',
                        },
                        'action': {
                            'service': 'test.automation',
                        }
                    }
                })

        common.turn_off(self.hass)
        self.hass.block_till_done()

        fire_time_changed(self.hass, trigger_time)
        self.hass.block_till_done()
        self.assertEqual(0, len(self.calls))

        with patch('homeassistant.util.dt.utcnow', return_value=now):
            common.turn_on(self.hass)
            self.hass.block_till_done()

        fire_time_changed(self.hass, trigger_time)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #3
0
    def test_if_fires_on_entity_change_below(self):
        """Test the firing with changed entity."""
        context = Context()
        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, context=context)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
        assert self.calls[0].context is context

        # Set above 12 so the automation will fire again
        self.hass.states.set('test.entity', 12)
        common.turn_off(self.hass)
        self.hass.block_till_done()
        self.hass.states.set('test.entity', 9)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #4
0
    def test_if_fires_when_hour_matches(self):
        """Test for firing if hour is matching."""
        assert setup_component(
            self.hass, automation.DOMAIN, {
                automation.DOMAIN: {
                    'trigger': {
                        'platform': 'time',
                        'hours': 0,
                    },
                    'action': {
                        'service': 'test.automation'
                    }
                }
            })

        fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))

        common.turn_off(self.hass)
        self.hass.block_till_done()

        fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #5
0
    def test_if_fires_on_entity_change_below(self):
        """Test the firing with changed entity."""
        context = Context()
        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, context=context)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
        assert self.calls[0].context is context

        # Set above 12 so the automation will fire again
        self.hass.states.set('test.entity', 12)
        common.turn_off(self.hass)
        self.hass.block_till_done()
        self.hass.states.set('test.entity', 9)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #6
0
    def test_if_fires_on_topic_match(self):
        """Test if message is fired on topic match."""
        assert setup_component(self.hass, automation.DOMAIN, {
            automation.DOMAIN: {
                'trigger': {
                    'platform': 'mqtt',
                    'topic': 'test-topic'
                },
                'action': {
                    'service': 'test.automation',
                    'data_template': {
                        'some': '{{ trigger.platform }} - {{ trigger.topic }}'
                                ' - {{ trigger.payload }} - '
                                '{{ trigger.payload_json.hello }}'
                    },
                }
            }
        })

        fire_mqtt_message(self.hass, 'test-topic', '{ "hello": "world" }')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
        self.assertEqual('mqtt - test-topic - { "hello": "world" } - world',
                         self.calls[0].data['some'])

        common.turn_off(self.hass)
        self.hass.block_till_done()
        fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #7
0
    def test_sunset_trigger(self):
        """Test the sunset trigger."""
        now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
        trigger_time = datetime(2015, 9, 16, 2, 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',
                    },
                    'action': {
                        'service': 'test.automation',
                    }
                }
            })

        common.turn_off(self.hass)
        self.hass.block_till_done()

        fire_time_changed(self.hass, trigger_time)
        self.hass.block_till_done()
        self.assertEqual(0, len(self.calls))

        with patch('homeassistant.util.dt.utcnow',
                   return_value=now):
            common.turn_on(self.hass)
            self.hass.block_till_done()

        fire_time_changed(self.hass, trigger_time)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #8
0
    def test_if_fires_on_event_extra_data(self):
        """Test the firing of events still matches with event data."""
        assert setup_component(
            self.hass, automation.DOMAIN, {
                automation.DOMAIN: {
                    'trigger': {
                        'platform': 'event',
                        'event_type': 'test_event',
                    },
                    'action': {
                        'service': 'test.automation',
                    }
                }
            })

        self.hass.bus.fire('test_event', {'extra_key': 'extra_data'})
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))

        common.turn_off(self.hass)
        self.hass.block_till_done()

        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #9
0
    def test_if_fires_on_zone_enter(self):
        """Test for firing on zone enter."""
        context = Context()
        self.hass.states.set('test.entity', 'hello', {
            'latitude': 32.881011,
            'longitude': -117.234758
        })
        self.hass.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
        }, context=context)
        self.hass.block_till_done()

        self.assertEqual(1, len(self.calls))
        assert self.calls[0].context is context
        self.assertEqual(
            'zone - test.entity - hello - hello - test',
            self.calls[0].data['some'])

        # Set out of zone again so we can trigger call
        self.hass.states.set('test.entity', 'hello', {
            'latitude': 32.881011,
            'longitude': -117.234758
        })
        self.hass.block_till_done()

        common.turn_off(self.hass)
        self.hass.block_till_done()

        self.hass.states.set('test.entity', 'hello', {
            'latitude': 32.880586,
            'longitude': -117.237564
        })
        self.hass.block_till_done()

        self.assertEqual(1, len(self.calls))
예제 #10
0
    def test_services(self):
        """Test the automation services for turning entities on/off."""
        entity_id = 'automation.hello'

        assert self.hass.states.get(entity_id) is None
        assert not automation.is_on(self.hass, entity_id)

        assert setup_component(
            self.hass, automation.DOMAIN, {
                automation.DOMAIN: {
                    'alias': 'hello',
                    'trigger': {
                        'platform': 'event',
                        'event_type': 'test_event',
                    },
                    'action': {
                        'service': 'test.automation',
                    }
                }
            })

        assert self.hass.states.get(entity_id) is not None
        assert automation.is_on(self.hass, entity_id)

        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        assert len(self.calls) == 1

        common.turn_off(self.hass, entity_id)
        self.hass.block_till_done()

        assert not automation.is_on(self.hass, entity_id)
        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        assert len(self.calls) == 1

        common.toggle(self.hass, entity_id)
        self.hass.block_till_done()

        assert automation.is_on(self.hass, entity_id)
        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        assert len(self.calls) == 2

        common.trigger(self.hass, entity_id)
        self.hass.block_till_done()
        assert len(self.calls) == 3

        common.turn_off(self.hass, entity_id)
        self.hass.block_till_done()
        common.trigger(self.hass, entity_id)
        self.hass.block_till_done()
        assert len(self.calls) == 4

        common.turn_on(self.hass, entity_id)
        self.hass.block_till_done()
        assert automation.is_on(self.hass, entity_id)
예제 #11
0
    def test_services(self):
        """Test the automation services for turning entities on/off."""
        entity_id = 'automation.hello'

        assert self.hass.states.get(entity_id) is None
        assert not automation.is_on(self.hass, entity_id)

        assert setup_component(self.hass, automation.DOMAIN, {
            automation.DOMAIN: {
                'alias': 'hello',
                'trigger': {
                    'platform': 'event',
                    'event_type': 'test_event',
                },
                'action': {
                    'service': 'test.automation',
                }
            }
        })

        assert self.hass.states.get(entity_id) is not None
        assert automation.is_on(self.hass, entity_id)

        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        assert len(self.calls) == 1

        common.turn_off(self.hass, entity_id)
        self.hass.block_till_done()

        assert not automation.is_on(self.hass, entity_id)
        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        assert len(self.calls) == 1

        common.toggle(self.hass, entity_id)
        self.hass.block_till_done()

        assert automation.is_on(self.hass, entity_id)
        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        assert len(self.calls) == 2

        common.trigger(self.hass, entity_id)
        self.hass.block_till_done()
        assert len(self.calls) == 3

        common.turn_off(self.hass, entity_id)
        self.hass.block_till_done()
        common.trigger(self.hass, entity_id)
        self.hass.block_till_done()
        assert len(self.calls) == 4

        common.turn_on(self.hass, entity_id)
        self.hass.block_till_done()
        assert automation.is_on(self.hass, entity_id)
예제 #12
0
    def test_if_not_fires_on_entities_change_with_for_after_stop(self):
        """Test for not firing on entities change with for after stop."""
        assert setup_component(
            self.hass, automation.DOMAIN, {
                automation.DOMAIN: {
                    'trigger': {
                        'platform': 'numeric_state',
                        'entity_id': [
                            'test.entity_1',
                            'test.entity_2',
                        ],
                        'above': 8,
                        'below': 12,
                        'for': {
                            'seconds': 5
                        },
                    },
                    'action': {
                        'service': 'test.automation'
                    }
                }
            })

        self.hass.states.set('test.entity_1', 9)
        self.hass.states.set('test.entity_2', 9)
        self.hass.block_till_done()
        fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
        self.hass.block_till_done()
        self.assertEqual(2, len(self.calls))

        self.hass.states.set('test.entity_1', 15)
        self.hass.states.set('test.entity_2', 15)
        self.hass.block_till_done()
        self.hass.states.set('test.entity_1', 9)
        self.hass.states.set('test.entity_2', 9)
        self.hass.block_till_done()
        common.turn_off(self.hass)
        self.hass.block_till_done()

        fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
        self.hass.block_till_done()
        self.assertEqual(2, len(self.calls))
예제 #13
0
    def test_if_not_fires_on_entities_change_with_for_after_stop(self):
        """Test for not firing on entities change with for after stop."""
        assert setup_component(self.hass, automation.DOMAIN, {
            automation.DOMAIN: {
                'trigger': {
                    'platform': 'numeric_state',
                    'entity_id': [
                        'test.entity_1',
                        'test.entity_2',
                    ],
                    'above': 8,
                    'below': 12,
                    'for': {
                        'seconds': 5
                    },
                },
                'action': {
                    'service': 'test.automation'
                }
            }
        })

        self.hass.states.set('test.entity_1', 9)
        self.hass.states.set('test.entity_2', 9)
        self.hass.block_till_done()
        fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
        self.hass.block_till_done()
        self.assertEqual(2, len(self.calls))

        self.hass.states.set('test.entity_1', 15)
        self.hass.states.set('test.entity_2', 15)
        self.hass.block_till_done()
        self.hass.states.set('test.entity_1', 9)
        self.hass.states.set('test.entity_2', 9)
        self.hass.block_till_done()
        common.turn_off(self.hass)
        self.hass.block_till_done()

        fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10))
        self.hass.block_till_done()
        self.assertEqual(2, len(self.calls))
예제 #14
0
    def test_if_fires_on_entity_change(self):
        """Test for firing on entity change."""
        context = Context()
        self.hass.states.set('test.entity', 'hello')
        self.hass.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', context=context)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
        assert self.calls[0].context is context
        self.assertEqual(
            'state - test.entity - hello - world - None',
            self.calls[0].data['some'])

        common.turn_off(self.hass)
        self.hass.block_till_done()
        self.hass.states.set('test.entity', 'planet')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #15
0
    def test_if_fires_on_entity_change(self):
        """Test for firing on entity change."""
        context = Context()
        self.hass.states.set('test.entity', 'hello')
        self.hass.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', context=context)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
        assert self.calls[0].context is context
        self.assertEqual('state - test.entity - hello - world - None',
                         self.calls[0].data['some'])

        common.turn_off(self.hass)
        self.hass.block_till_done()
        self.hass.states.set('test.entity', 'planet')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #16
0
    def test_if_fires_on_change_bool(self):
        """Test for firing on boolean change."""
        assert setup_component(self.hass, automation.DOMAIN, {
            automation.DOMAIN: {
                'trigger': {
                    'platform': 'template',
                    'value_template': '{{ true }}',
                },
                'action': {
                    'service': 'test.automation'
                }
            }
        })

        self.hass.states.set('test.entity', 'world')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))

        common.turn_off(self.hass)
        self.hass.block_till_done()

        self.hass.states.set('test.entity', 'planet')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
예제 #17
0
    def test_if_fires_when_hour_matches(self):
        """Test for firing if hour is matching."""
        assert setup_component(self.hass, automation.DOMAIN, {
            automation.DOMAIN: {
                'trigger': {
                    'platform': 'time',
                    'hours': 0,
                },
                'action': {
                    'service': 'test.automation'
                }
            }
        })

        fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))

        common.turn_off(self.hass)
        self.hass.block_till_done()

        fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))