Beispiel #1
0
    def test_disarm_during_trigger_with_invalid_code(self):
        """Test disarming while code is invalid."""
        self.assertTrue(alarm_control_panel.setup(self.hass, {
            'alarm_control_panel': {
                'platform': 'manual',
                'name': 'test',
                'pending_time': 5,
                'code': CODE + '2'
            }}))

        entity_id = 'alarm_control_panel.test'

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_trigger(self.hass)
        self.hass.pool.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id)
        self.hass.pool.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        future = dt_util.utcnow() + timedelta(seconds=5)
        with patch(('homeassistant.components.alarm_control_panel.manual.'
                    'dt_util.utcnow'), return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.pool.block_till_done()

        self.assertEqual(STATE_ALARM_TRIGGERED,
                         self.hass.states.get(entity_id).state)
Beispiel #2
0
    def test_disarm_during_trigger_with_invalid_code(self):
        self.assertTrue(alarm_control_panel.setup(self.hass, {
            'alarm_control_panel': {
                'platform': 'manual',
                'name': 'test',
                'pending_time': 5,
                'code': CODE + '2'
            }}))

        entity_id = 'alarm_control_panel.test'

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_trigger(self.hass)
        self.hass.pool.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id)
        self.hass.pool.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        future = dt_util.utcnow() + timedelta(seconds=5)
        with patch(('homeassistant.components.alarm_control_panel.manual.'
                    'dt_util.utcnow'), return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.pool.block_till_done()

        self.assertEqual(STATE_ALARM_TRIGGERED,
                         self.hass.states.get(entity_id).state)
Beispiel #3
0
    def test_state_changes_are_published_to_mqtt(self):
        """Test publishing of MQTT messages when state changes."""
        assert setup_component(
            self.hass, alarm_control_panel.DOMAIN, {
                alarm_control_panel.DOMAIN: {
                    'platform': 'manual_mqtt',
                    'name': 'test',
                    'pending_time': 1,
                    'trigger_time': 1,
                    'state_topic': 'alarm/state',
                    'command_topic': 'alarm/command',
                }
            })

        # Component should send disarmed alarm state on startup
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_DISARMED, 0, True),
                         self.mock_publish.mock_calls[-2][1])

        # Arm in home mode
        alarm_control_panel.alarm_arm_home(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_PENDING, 0, True),
                         self.mock_publish.mock_calls[-2][1])
        # Fast-forward a little bit
        future = dt_util.utcnow() + timedelta(seconds=1)
        with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
                    'dt_util.utcnow'),
                   return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_ARMED_HOME, 0, True),
                         self.mock_publish.mock_calls[-2][1])

        # Arm in away mode
        alarm_control_panel.alarm_arm_away(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_PENDING, 0, True),
                         self.mock_publish.mock_calls[-2][1])
        # Fast-forward a little bit
        future = dt_util.utcnow() + timedelta(seconds=1)
        with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
                    'dt_util.utcnow'),
                   return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_ARMED_AWAY, 0, True),
                         self.mock_publish.mock_calls[-2][1])

        # Disarm
        alarm_control_panel.alarm_disarm(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_DISARMED, 0, True),
                         self.mock_publish.mock_calls[-2][1])
Beispiel #4
0
    def test_disarm_publishes_mqtt(self):
        self.assertTrue(alarm_control_panel.setup(self.hass, {
            'alarm_control_panel': {
                'platform': 'mqtt',
                'name': 'test',
                'state_topic': 'alarm/state',
                'command_topic': 'alarm/command',
            }}))

        alarm_control_panel.alarm_disarm(self.hass)
        self.hass.pool.block_till_done()
        self.assertEqual(('alarm/command', 'DISARM', 0, False),
                         self.mock_publish.mock_calls[-1][1])
Beispiel #5
0
    def test_disarm_not_publishes_mqtt_with_invalid_code(self):
        self.assertTrue(alarm_control_panel.setup(self.hass, {
            'alarm_control_panel': {
                'platform': 'mqtt',
                'name': 'test',
                'state_topic': 'alarm/state',
                'command_topic': 'alarm/command',
                'code': '1234'
            }}))

        call_count = self.mock_publish.call_count
        alarm_control_panel.alarm_disarm(self.hass, 'abcd')
        self.hass.pool.block_till_done()
        self.assertEqual(call_count, self.mock_publish.call_count)
Beispiel #6
0
    def test_disarm_publishes_mqtt(self):
        """Test publishing of MQTT messages while disarmed."""
        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',
            }
        })

        alarm_control_panel.alarm_disarm(self.hass)
        self.hass.block_till_done()
        self.mock_publish.async_publish.assert_called_once_with(
            'alarm/command', 'DISARM', 0, False)
    def test_disarm_publishes_mqtt(self):
        """Test publishing of MQTT messages while disarmed."""
        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',
            }
        })

        alarm_control_panel.alarm_disarm(self.hass)
        self.hass.block_till_done()
        self.mock_publish.async_publish.assert_called_once_with(
            'alarm/command', 'DISARM', 0, False)
Beispiel #8
0
    def test_disarm_not_publishes_mqtt_with_invalid_code(self):
        """Test not publishing of MQTT messages with invalid code."""
        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',
                'code': '1234'
            }
        })

        call_count = self.mock_publish.call_count
        alarm_control_panel.alarm_disarm(self.hass, 'abcd')
        self.hass.block_till_done()
        self.assertEqual(call_count, self.mock_publish.call_count)
    def test_disarm_publishes_mqtt(self):
        """Test publishing of MQTT messages while disarmed."""
        self.hass.config.components = set(['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',
            }
        })

        alarm_control_panel.alarm_disarm(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/command', 'DISARM', 0, False),
                         self.mock_publish.mock_calls[-2][1])
Beispiel #10
0
    def test_disarm_publishes_mqtt(self):
        """Test publishing of MQTT messages while disarmed."""
        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',
            }
        })

        alarm_control_panel.alarm_disarm(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/command', 'DISARM', 0, False),
                         self.mock_publish.mock_calls[-1][1])
    def test_disarm_not_publishes_mqtt_with_invalid_code(self):
        """Test not publishing of MQTT messages with invalid code."""
        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',
                'code': '1234'
            }
        })

        call_count = self.mock_publish.call_count
        alarm_control_panel.alarm_disarm(self.hass, 'abcd')
        self.hass.block_till_done()
        self.assertEqual(call_count, self.mock_publish.call_count)
Beispiel #12
0
    def test_disarm_during_trigger_with_invalid_code(self):
        """Test disarming while code is invalid."""
        self.assertTrue(
            setup_component(
                self.hass, alarm_control_panel.DOMAIN, {
                    'alarm_control_panel': {
                        'platform': 'manual_mqtt',
                        'name': 'test',
                        'pending_time': 5,
                        'code': CODE + '2',
                        'disarm_after_trigger': False,
                        'command_topic': 'alarm/command',
                        'state_topic': 'alarm/state',
                    }
                }))

        entity_id = 'alarm_control_panel.test'

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_trigger(self.hass)
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id)
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        future = dt_util.utcnow() + timedelta(seconds=5)
        with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
                    'dt_util.utcnow'),
                   return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_TRIGGERED,
                         self.hass.states.get(entity_id).state)
Beispiel #13
0
    def test_disarm_while_pending_trigger(self):
        """Test disarming while pending state."""
        self.assertTrue(
            setup_component(
                self.hass, alarm_control_panel.DOMAIN, {
                    'alarm_control_panel': {
                        'platform': 'manual',
                        'name': 'test',
                        'trigger_time': 5,
                        'disarm_after_trigger': False
                    }
                }))

        entity_id = 'alarm_control_panel.test'

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_trigger(self.hass)
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id)
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        future = dt_util.utcnow() + timedelta(seconds=5)
        with patch(('homeassistant.components.alarm_control_panel.manual.'
                    'dt_util.utcnow'),
                   return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)
    def test_disarm_with_template_code(self):
        """Attempt to disarm with a valid or invalid template-based code."""
        self.assertTrue(setup_component(
            self.hass, alarm_control_panel.DOMAIN,
            {'alarm_control_panel': {
                'platform': 'manual',
                'name': 'test',
                'code_template':
                    '{{ "" if from_state == "disarmed" else "abc" }}',
                'pending_time': 0,
                'disarm_after_trigger': False
            }}))

        entity_id = 'alarm_control_panel.test'

        self.hass.start()
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_arm_home(self.hass, 'def')
        self.hass.block_till_done()

        state = self.hass.states.get(entity_id)
        self.assertEqual(STATE_ALARM_ARMED_HOME, state.state)

        alarm_control_panel.alarm_disarm(self.hass, 'def')
        self.hass.block_till_done()

        state = self.hass.states.get(entity_id)
        self.assertEqual(STATE_ALARM_ARMED_HOME, state.state)

        alarm_control_panel.alarm_disarm(self.hass, 'abc')
        self.hass.block_till_done()

        state = self.hass.states.get(entity_id)
        self.assertEqual(STATE_ALARM_DISARMED, state.state)
    def test_disarm_while_pending_trigger(self):
        """Test disarming while pending state."""
        self.assertTrue(setup_component(
            self.hass, alarm_control_panel.DOMAIN,
            {'alarm_control_panel': {
                'platform': 'manual_mqtt',
                'name': 'test',
                'trigger_time': 5,
                'disarm_after_trigger': False,
                'command_topic': 'alarm/command',
                'state_topic': 'alarm/state',
            }}))

        entity_id = 'alarm_control_panel.test'

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_trigger(self.hass)
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_PENDING,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id)
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        future = dt_util.utcnow() + timedelta(seconds=5)
        with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
                    'dt_util.utcnow'), return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)
Beispiel #16
0
    def test_disarm_with_template_code(self):
        """Attempt to disarm with a valid or invalid template-based code."""
        self.assertTrue(setup_component(
            self.hass, alarm_control_panel.DOMAIN,
            {'alarm_control_panel': {
                'platform': 'manual',
                'name': 'test',
                'code_template':
                    '{{ "" if from_state == "disarmed" else "abc" }}',
                'pending_time': 0,
                'disarm_after_trigger': False
            }}))

        entity_id = 'alarm_control_panel.test'

        self.hass.start()
        self.hass.block_till_done()

        self.assertEqual(STATE_ALARM_DISARMED,
                         self.hass.states.get(entity_id).state)

        alarm_control_panel.alarm_arm_home(self.hass, 'def')
        self.hass.block_till_done()

        state = self.hass.states.get(entity_id)
        self.assertEqual(STATE_ALARM_ARMED_HOME, state.state)

        alarm_control_panel.alarm_disarm(self.hass, 'def')
        self.hass.block_till_done()

        state = self.hass.states.get(entity_id)
        self.assertEqual(STATE_ALARM_ARMED_HOME, state.state)

        alarm_control_panel.alarm_disarm(self.hass, 'abc')
        self.hass.block_till_done()

        state = self.hass.states.get(entity_id)
        self.assertEqual(STATE_ALARM_DISARMED, state.state)
    def test_state_changes_are_published_to_mqtt(self):
        """Test publishing of MQTT messages when state changes."""
        assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
            alarm_control_panel.DOMAIN: {
                'platform': 'manual_mqtt',
                'name': 'test',
                'pending_time': 1,
                'trigger_time': 1,
                'state_topic': 'alarm/state',
                'command_topic': 'alarm/command',
            }
        })

        # Component should send disarmed alarm state on startup
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_DISARMED, 0, True),
                         self.mock_publish.mock_calls[-2][1])

        # Arm in home mode
        alarm_control_panel.alarm_arm_home(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_PENDING, 0, True),
                         self.mock_publish.mock_calls[-2][1])
        # Fast-forward a little bit
        future = dt_util.utcnow() + timedelta(seconds=1)
        with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
                    'dt_util.utcnow'), return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_ARMED_HOME, 0, True),
                         self.mock_publish.mock_calls[-2][1])

        # Arm in away mode
        alarm_control_panel.alarm_arm_away(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_PENDING, 0, True),
                         self.mock_publish.mock_calls[-2][1])
        # Fast-forward a little bit
        future = dt_util.utcnow() + timedelta(seconds=1)
        with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
                    'dt_util.utcnow'), return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_ARMED_AWAY, 0, True),
                         self.mock_publish.mock_calls[-2][1])

        # Arm in night mode
        alarm_control_panel.alarm_arm_night(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_PENDING, 0, True),
                         self.mock_publish.mock_calls[-2][1])
        # Fast-forward a little bit
        future = dt_util.utcnow() + timedelta(seconds=1)
        with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
                    'dt_util.utcnow'), return_value=future):
            fire_time_changed(self.hass, future)
            self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_ARMED_NIGHT, 0, True),
                         self.mock_publish.mock_calls[-2][1])

        # Disarm
        alarm_control_panel.alarm_disarm(self.hass)
        self.hass.block_till_done()
        self.assertEqual(('alarm/state', STATE_ALARM_DISARMED, 0, True),
                         self.mock_publish.mock_calls[-2][1])