Skip to content
8 changes: 6 additions & 2 deletions homeassistant/components/mqtt/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

_LOGGER = logging.getLogger(__name__)

CONF_CODE_ARM_REQUIRED = 'code_arm_required'
CONF_PAYLOAD_DISARM = 'payload_disarm'
CONF_PAYLOAD_ARM_HOME = 'payload_arm_home'
CONF_PAYLOAD_ARM_AWAY = 'payload_arm_away'
Expand All @@ -52,6 +53,7 @@
vol.Optional(CONF_PAYLOAD_DISARM, default=DEFAULT_DISARM): cv.string,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
vol.Optional(CONF_CODE_ARM_REQUIRED, default=True): cv.boolean,
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)

Expand Down Expand Up @@ -197,7 +199,8 @@ async def async_alarm_arm_home(self, code=None):

This method is a coroutine.
"""
if not self._validate_code(code, 'arming home'):
code_required = self._config.get(CONF_CODE_ARM_REQUIRED)
if code_required and not self._validate_code(code, 'arming home'):
return
mqtt.async_publish(
self.hass, self._config.get(CONF_COMMAND_TOPIC),
Expand All @@ -210,7 +213,8 @@ async def async_alarm_arm_away(self, code=None):

This method is a coroutine.
"""
if not self._validate_code(code, 'arming away'):
code_required = self._config.get(CONF_CODE_ARM_REQUIRED)
if code_required and not self._validate_code(code, 'arming away'):
return
mqtt.async_publish(
self.hass, self._config.get(CONF_COMMAND_TOPIC),
Expand Down
51 changes: 42 additions & 9 deletions tests/components/mqtt/test_alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,19 @@ def test_arm_home_publishes_mqtt(self):
self.mock_publish.async_publish.assert_called_once_with(
'alarm/command', 'ARM_HOME', 0, False)

def test_arm_home_not_publishes_mqtt_with_invalid_code(self):
"""Test not publishing of MQTT messages with invalid code."""
def test_arm_home_not_publishes_mqtt_with_invalid_code_when_req(self):
"""Test not publishing of MQTT messages with invalid.

When code_arm_required = True
"""
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': '1234',
'code_arm_required': True
}
})

Expand All @@ -147,15 +151,19 @@ def test_arm_away_publishes_mqtt(self):
self.mock_publish.async_publish.assert_called_once_with(
'alarm/command', 'ARM_AWAY', 0, False)

def test_arm_away_not_publishes_mqtt_with_invalid_code(self):
"""Test not publishing of MQTT messages with invalid code."""
def test_arm_away_not_publishes_mqtt_with_invalid_code_when_req(self):
"""Test not publishing of MQTT messages with invalid code.

When code_arm_required = True
"""
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': '1234',
'code_arm_required': True
}
})

Expand All @@ -164,6 +172,27 @@ def test_arm_away_not_publishes_mqtt_with_invalid_code(self):
self.hass.block_till_done()
assert call_count == self.mock_publish.call_count

def test_arm_away_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_away(self.hass)
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
'alarm/command', 'ARM_AWAY', 0, False)

def test_arm_night_publishes_mqtt(self):
"""Test publishing of MQTT messages while armed."""
assert setup_component(self.hass, alarm_control_panel.DOMAIN, {
Expand All @@ -180,15 +209,19 @@ def test_arm_night_publishes_mqtt(self):
self.mock_publish.async_publish.assert_called_once_with(
'alarm/command', 'ARM_NIGHT', 0, False)

def test_arm_night_not_publishes_mqtt_with_invalid_code(self):
"""Test not publishing of MQTT messages with invalid code."""
def test_arm_night_not_publishes_mqtt_with_invalid_code_when_req(self):
"""Test not publishing of MQTT messages with invalid code.

When code_arm_required = True
"""
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': '1234',
'code_arm_required': True
}
})

Expand Down