Skip to content

Commit

Permalink
Fix missing code_required check in async_alarm_arm_night (#21858)
Browse files Browse the repository at this point in the history
* Fix missing code_required check in async_alarm_arm_night

* Remove double code validation
Test added
  • Loading branch information
ToRvaLDz authored and emontnemery committed Mar 10, 2019
1 parent 6f77d9b commit 05333f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 additions & 1 deletion homeassistant/components/mqtt/alarm_control_panel.py
Expand Up @@ -227,7 +227,8 @@ async def async_alarm_arm_night(self, code=None):
This method is a coroutine.
"""
if not self._validate_code(code, 'arming night'):
code_required = self._config.get(CONF_CODE_ARM_REQUIRED)
if code_required and not self._validate_code(code, 'arming night'):
return
mqtt.async_publish(
self.hass, self._config.get(CONF_COMMAND_TOPIC),
Expand Down
42 changes: 42 additions & 0 deletions tests/components/mqtt/test_alarm_control_panel.py
Expand Up @@ -135,6 +135,27 @@ def test_arm_home_not_publishes_mqtt_with_invalid_code_when_req(self):
self.hass.block_till_done()
assert call_count == self.mock_publish.call_count

def test_arm_home_publishes_mqtt_when_code_not_req(self):
"""Test publishing of MQTT messages.
When code_arm_required = False
"""
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',
'code_arm_required': False
}
})

common.alarm_arm_home(self.hass)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'alarm/command', 'ARM_HOME', 0, False)

def test_arm_away_publishes_mqtt(self):
"""Test publishing of MQTT messages while armed."""
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
Expand Down Expand Up @@ -230,6 +251,27 @@ def test_arm_night_not_publishes_mqtt_with_invalid_code_when_req(self):
self.hass.block_till_done()
assert call_count == self.mock_publish.call_count

def test_arm_night_publishes_mqtt_when_code_not_req(self):
"""Test publishing of MQTT messages.
When code_arm_required = False
"""
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',
'code_arm_required': False
}
})

common.alarm_arm_night(self.hass)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'alarm/command', 'ARM_NIGHT', 0, False)

def test_disarm_publishes_mqtt(self):
"""Test publishing of MQTT messages while disarmed."""
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
Expand Down

0 comments on commit 05333f6

Please sign in to comment.