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

        automation.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))
    def test_if_fires_on_entity_change(self):
        """Test for firing on entity change."""
        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')
        self.hass.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.block_till_done()
        self.hass.states.set('test.entity', 'planet')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
    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 }}'
                    },
                }
            }
        })

        fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
        self.assertEqual('mqtt - test-topic - test_payload',
                         self.calls[0].data['some'])

        automation.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))
    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',
                    }
                }
            })

        automation.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):
            automation.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))
    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.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.block_till_done()
        self.hass.states.set('test.entity', 9)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
Exemple #6
0
    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.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.block_till_done()
        self.hass.states.set('test.entity', 9)
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
    def test_if_fires_on_entity_change(self):
        """Test for firing on entity change."""
        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')
        self.hass.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.block_till_done()
        self.hass.states.set('test.entity', 'planet')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))
Exemple #8
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'])

        automation.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))
Exemple #9
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))

        automation.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))
    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))

        automation.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))
Exemple #11
0
    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.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.block_till_done()

        self.assertEqual(1, len(self.calls))
        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()

        automation.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))
    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.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.block_till_done()

        self.assertEqual(1, len(self.calls))
        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()

        automation.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))
Exemple #13
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.pool.block_till_done()
        assert len(self.calls) == 1

        automation.turn_off(self.hass, entity_id)
        self.hass.pool.block_till_done()

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

        automation.toggle(self.hass, entity_id)
        self.hass.pool.block_till_done()

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

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

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

        automation.turn_on(self.hass, entity_id)
        self.hass.pool.block_till_done()
        assert automation.is_on(self.hass, entity_id)
    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

        automation.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

        automation.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

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

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

        automation.turn_on(self.hass, entity_id)
        self.hass.block_till_done()
        assert automation.is_on(self.hass, entity_id)
Exemple #15
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()
        automation.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))
    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()
        automation.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))
Exemple #17
0
    def test_sunset_trigger(self):
        """Test the sunset trigger."""
        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, 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',
                    }
                }
            })

        automation.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):
            automation.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))
Exemple #18
0
    def test_sunset_trigger(self):
        """Test the sunset trigger."""
        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, 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',
                    }
                }
            })

        automation.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):
            automation.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))
    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))

        automation.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))
Exemple #20
0
    def test_if_fires_on_event(self):
        """Test the firing of events."""
        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')
        self.hass.block_till_done()
        self.assertEqual(1, len(self.calls))

        automation.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))
    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))

        automation.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))
    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))

        automation.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))