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)
def test_arm_home_with_pending(self): """ Test arm home method. """ self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'code': CODE, 'pending_time': 1 }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) alarm_control_panel.alarm_arm_home(self.hass, CODE, 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=1) 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_ARMED_HOME, self.hass.states.get(entity_id).state)
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)
def test_arm_home_with_pending(self): """Test arm home method.""" self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'code': CODE, 'pending_time': 1 }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) alarm_control_panel.alarm_arm_home(self.hass, CODE, 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=1) 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_ARMED_HOME, self.hass.states.get(entity_id).state)
def test_fail_setup_without_command_topic(self, mock_error): self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'mqtt', 'state_topic': 'alarm/state' }})) self.assertEqual(1, mock_error.call_count)
def test_fail_setup_without_state_topic(self, mock_error): """Test for failing with no state topic.""" self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'mqtt', 'command_topic': 'alarm/command' }})) self.assertEqual(1, mock_error.call_count)
def test_arm_home_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_arm_home(self.hass) self.hass.pool.block_till_done() self.assertEqual(('alarm/command', 'ARM_HOME', 0, False), self.mock_publish.mock_calls[-1][1])
def test_disarm_publishes_mqtt(self): """Test publishing of MQTT messages while disarmed.""" 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])
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)
def test_ignore_update_state_if_unknown_via_state_topic(self): """ Test arm home method. """ self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_UNKNOWN, self.hass.states.get(entity_id).state) fire_mqtt_message(self.hass, 'alarm/state', 'unsupported state') self.hass.pool.block_till_done() self.assertEqual(STATE_UNKNOWN, self.hass.states.get(entity_id).state)
def test_trigger_with_pending(self): """Test arm home method.""" self.assertTrue( alarm_control_panel.setup( self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'pending_time': 2, 'trigger_time': 3, '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.pool.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) future = dt_util.utcnow() + timedelta(seconds=2) 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) 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_DISARMED, self.hass.states.get(entity_id).state)
def test_trigger_no_pending(self): """ Test arm home method. """ self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'trigger_time': 0 }})) 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, entity_id=entity_id) self.hass.pool.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, self.hass.states.get(entity_id).state)
def test_trigger_no_pending(self): """Test arm home method.""" self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'trigger_time': 0 }})) 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, entity_id=entity_id) self.hass.pool.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, self.hass.states.get(entity_id).state)
def test_arm_away_no_pending(self): """Test arm home method.""" self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'code': CODE, 'pending_time': 0 }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) alarm_control_panel.alarm_arm_away(self.hass, CODE, entity_id) self.hass.pool.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state)
def test_arm_home_with_invalid_code(self): """Attempt to arm home without a valid code.""" self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'code': CODE, 'pending_time': 1 }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) alarm_control_panel.alarm_arm_home(self.hass, CODE + '2') self.hass.pool.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state)
def test_arm_home_no_pending(self): """ Test arm home method. """ self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'code': CODE, 'pending_time': 0 }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) alarm_control_panel.alarm_arm_home(self.hass, CODE) self.hass.pool.block_till_done() self.assertEqual(STATE_ALARM_ARMED_HOME, self.hass.states.get(entity_id).state)
def test_arm_home_with_invalid_code(self): """ Attempt to arm home without a valid code. """ self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'code': CODE, 'pending_time': 1 }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) alarm_control_panel.alarm_arm_home(self.hass, CODE + '2') self.hass.pool.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state)
def test_update_state_via_state_topic(self): """ Test arm home method. """ self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'mqtt', 'name': 'test', 'state_topic': 'alarm/state', 'command_topic': 'alarm/command', }})) entity_id = 'alarm_control_panel.test' self.assertEqual(STATE_UNKNOWN, self.hass.states.get(entity_id).state) for state in (STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_AWAY, STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED): fire_mqtt_message(self.hass, 'alarm/state', state) self.hass.pool.block_till_done() self.assertEqual(state, self.hass.states.get(entity_id).state)
def test_trigger_with_pending(self): """Test arm home method.""" self.assertTrue(alarm_control_panel.setup(self.hass, { 'alarm_control_panel': { 'platform': 'manual', 'name': 'test', 'pending_time': 2, 'trigger_time': 3, '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.pool.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) future = dt_util.utcnow() + timedelta(seconds=2) 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) 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_DISARMED, self.hass.states.get(entity_id).state)