Mqtt alarm added value_template and code_arm_required (#19558)

* Added value_template config for parsing json value from state topic

Added arm_code_required to avoid code enter when arming

* Renamed config parameter to code_arm_required

* Fix for discovery update compatibility

* Fixed lint error

* Added test
This commit is contained in:
Marco M 2019-02-28 17:44:23 +01:00 committed by emontnemery
parent b0dd6e4093
commit c3d4738649
2 changed files with 48 additions and 11 deletions

View file

@ -28,6 +28,7 @@ from homeassistant.helpers.typing import ConfigType, HomeAssistantType
_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'
@ -52,6 +53,7 @@ PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend({
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)
@ -197,7 +199,8 @@ class MqttAlarm(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
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),
@ -210,7 +213,8 @@ class MqttAlarm(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
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),

View file

@ -114,15 +114,19 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
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
}
})
@ -147,15 +151,19 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
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
}
})
@ -164,6 +172,27 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
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, {
@ -180,15 +209,19 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
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
}
})