core/homeassistant/components/alarm_control_panel/mqtt.py

126 lines
4.2 KiB
Python
Raw Normal View History

2015-09-18 15:30:34 +00:00
"""
This platform enables the possibility to control a MQTT alarm.
2015-10-13 18:42:05 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/alarm_control_panel.mqtt/
2015-09-18 15:30:34 +00:00
"""
import logging
2016-02-19 05:27:50 +00:00
import homeassistant.components.alarm_control_panel as alarm
import homeassistant.components.mqtt as mqtt
2015-10-14 06:08:12 +00:00
from homeassistant.const import (
2016-02-19 05:27:50 +00:00
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED,
2015-10-14 06:08:12 +00:00
STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED, STATE_UNKNOWN)
2015-09-18 15:30:34 +00:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "MQTT Alarm"
DEFAULT_QOS = 0
DEFAULT_PAYLOAD_DISARM = "DISARM"
DEFAULT_PAYLOAD_ARM_HOME = "ARM_HOME"
DEFAULT_PAYLOAD_ARM_AWAY = "ARM_AWAY"
DEPENDENCIES = ['mqtt']
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-07 19:21:43 +00:00
"""Setup the MQTT platform."""
2015-09-18 15:30:34 +00:00
if config.get('state_topic') is None:
_LOGGER.error("Missing required variable: state_topic")
return False
if config.get('command_topic') is None:
_LOGGER.error("Missing required variable: command_topic")
return False
add_devices([MqttAlarm(
hass,
config.get('name', DEFAULT_NAME),
config.get('state_topic'),
config.get('command_topic'),
config.get('qos', DEFAULT_QOS),
config.get('payload_disarm', DEFAULT_PAYLOAD_DISARM),
config.get('payload_arm_home', DEFAULT_PAYLOAD_ARM_HOME),
config.get('payload_arm_away', DEFAULT_PAYLOAD_ARM_AWAY),
config.get('code'))])
2015-09-18 15:30:34 +00:00
# pylint: disable=too-many-arguments, too-many-instance-attributes
# pylint: disable=abstract-method
2015-09-18 15:30:34 +00:00
class MqttAlarm(alarm.AlarmControlPanel):
2016-03-07 19:21:43 +00:00
"""Represent a MQTT alarm status."""
2015-09-18 15:30:34 +00:00
def __init__(self, hass, name, state_topic, command_topic, qos,
payload_disarm, payload_arm_home, payload_arm_away, code):
2016-03-07 19:21:43 +00:00
"""Initalize the MQTT alarm panel."""
2015-09-18 15:30:34 +00:00
self._state = STATE_UNKNOWN
self._hass = hass
self._name = name
self._state_topic = state_topic
self._command_topic = command_topic
self._qos = qos
self._payload_disarm = payload_disarm
self._payload_arm_home = payload_arm_home
self._payload_arm_away = payload_arm_away
2015-10-14 06:08:12 +00:00
self._code = str(code) if code else None
2015-09-18 15:30:34 +00:00
def message_received(topic, payload, qos):
2016-03-07 15:45:21 +00:00
"""A new MQTT message has been received."""
2015-10-14 06:08:12 +00:00
if payload not in (STATE_ALARM_DISARMED, STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_AWAY, STATE_ALARM_PENDING,
STATE_ALARM_TRIGGERED):
_LOGGER.warning('Received unexpected payload: %s', payload)
return
2015-09-18 15:30:34 +00:00
self._state = payload
self.update_ha_state()
mqtt.subscribe(hass, self._state_topic, message_received, self._qos)
@property
def should_poll(self):
2016-03-07 15:45:21 +00:00
"""No polling needed."""
2015-09-18 15:30:34 +00:00
return False
@property
def name(self):
2016-03-07 19:21:43 +00:00
"""Return the name of the device."""
2015-09-18 15:30:34 +00:00
return self._name
@property
def state(self):
2016-03-07 19:21:43 +00:00
"""Return the state of the device."""
2015-09-18 15:30:34 +00:00
return self._state
@property
def code_format(self):
2016-03-07 15:45:21 +00:00
"""One or more characters if code is defined."""
return None if self._code is None else '.+'
def alarm_disarm(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send disarm command."""
2015-10-14 06:08:12 +00:00
if not self._validate_code(code, 'disarming'):
return
mqtt.publish(self.hass, self._command_topic,
self._payload_disarm, self._qos)
2015-09-18 15:30:34 +00:00
def alarm_arm_home(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send arm home command."""
2015-10-14 06:08:12 +00:00
if not self._validate_code(code, 'arming home'):
return
mqtt.publish(self.hass, self._command_topic,
self._payload_arm_home, self._qos)
2015-09-18 15:30:34 +00:00
def alarm_arm_away(self, code=None):
2016-03-07 15:45:21 +00:00
"""Send arm away command."""
2015-10-14 06:08:12 +00:00
if not self._validate_code(code, 'arming away'):
return
mqtt.publish(self.hass, self._command_topic,
self._payload_arm_away, self._qos)
def _validate_code(self, code, state):
2016-03-07 15:45:21 +00:00
"""Validate given code."""
2015-10-14 06:08:12 +00:00
check = self._code is None or code == self._code
if not check:
_LOGGER.warning('Wrong code entered for %s', state)
return check