<s> """<STR_LIT>""" <EOL> import logging <EOL> import voluptuous as vol <EOL> import homeassistant . components . alarm_control_panel as alarm <EOL> import homeassistant . components . mqtt as mqtt <EOL> from homeassistant . const import ( <EOL> STATE_ALARM_ARMED_AWAY , STATE_ALARM_ARMED_HOME , STATE_ALARM_DISARMED , <EOL> STATE_ALARM_PENDING , STATE_ALARM_TRIGGERED , STATE_UNKNOWN , <EOL> CONF_NAME ) <EOL> from homeassistant . components . mqtt import ( <EOL> CONF_STATE_TOPIC , CONF_COMMAND_TOPIC , CONF_QOS ) <EOL> import homeassistant . helpers . config_validation as cv <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> DEPENDENCIES = [ '<STR_LIT>' ] <EOL> CONF_PAYLOAD_DISARM = '<STR_LIT>' <EOL> CONF_PAYLOAD_ARM_HOME = '<STR_LIT>' <EOL> CONF_PAYLOAD_ARM_AWAY = '<STR_LIT>' <EOL> CONF_CODE = '<STR_LIT:code>' <EOL> DEFAULT_NAME = "<STR_LIT>" <EOL> DEFAULT_DISARM = "<STR_LIT>" <EOL> DEFAULT_ARM_HOME = "<STR_LIT>" <EOL> DEFAULT_ARM_AWAY = "<STR_LIT>" <EOL> PLATFORM_SCHEMA = mqtt . MQTT_BASE_PLATFORM_SCHEMA . extend ( { <EOL> vol . Optional ( CONF_NAME , default = DEFAULT_NAME ) : cv . string , <EOL> vol . Required ( CONF_STATE_TOPIC ) : mqtt . valid_subscribe_topic , <EOL> vol . Required ( CONF_COMMAND_TOPIC ) : mqtt . valid_publish_topic , <EOL> vol . Optional ( CONF_PAYLOAD_DISARM , default = DEFAULT_DISARM ) : cv . string , <EOL> vol . Optional ( CONF_PAYLOAD_ARM_HOME , default = DEFAULT_ARM_HOME ) : cv . string , <EOL> vol . Optional ( CONF_PAYLOAD_ARM_AWAY , default = DEFAULT_ARM_AWAY ) : cv . string , <EOL> vol . Optional ( CONF_CODE ) : cv . string , <EOL> } ) <EOL> def setup_platform ( hass , config , add_devices , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> add_devices ( [ MqttAlarm ( <EOL> hass , <EOL> config [ CONF_NAME ] , <EOL> config [ CONF_STATE_TOPIC ] , <EOL> config [ CONF_COMMAND_TOPIC ] , <EOL> config [ CONF_QOS ] , <EOL> config [ CONF_PAYLOAD_DISARM ] , <EOL> config [ CONF_PAYLOAD_ARM_HOME ] , <EOL> config [ CONF_PAYLOAD_ARM_AWAY ] , <EOL> config . get ( CONF_CODE ) ) ] ) <EOL> class MqttAlarm ( alarm . AlarmControlPanel ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hass , name , state_topic , command_topic , qos , <EOL> payload_disarm , payload_arm_home , payload_arm_away , code ) : <EOL> """<STR_LIT>""" <EOL> self . _state = STATE_UNKNOWN <EOL> self . _hass = hass <EOL> self . _name = name <EOL> self . _state_topic = state_topic <EOL> self . _command_topic = command_topic <EOL> self . _qos = qos <EOL> self . _payload_disarm = payload_disarm <EOL> self . _payload_arm_home = payload_arm_home <EOL> self . _payload_arm_away = payload_arm_away <EOL> self . _code = code <EOL> def message_received ( topic , payload , qos ) : <EOL> """<STR_LIT>""" <EOL> if payload not in ( STATE_ALARM_DISARMED , STATE_ALARM_ARMED_HOME , <EOL> STATE_ALARM_ARMED_AWAY , STATE_ALARM_PENDING , <EOL> STATE_ALARM_TRIGGERED ) : <EOL> _LOGGER . warning ( '<STR_LIT>' , payload ) <EOL> return <EOL> self . _state = payload <EOL> self . update_ha_state ( ) <EOL> mqtt . subscribe ( hass , self . _state_topic , message_received , self . _qos ) <EOL> @ property <EOL> def should_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _name <EOL> @ property <EOL> def state ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _state <EOL> @ property <EOL> def code_format ( self ) : <EOL> """<STR_LIT>""" <EOL> return None if self . _code is None else '<STR_LIT>' <EOL> def alarm_disarm ( self , code = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . _validate_code ( code , '<STR_LIT>' ) : <EOL> return <EOL> mqtt . publish ( self . hass , self . _command_topic , <EOL> self . _payload_disarm , self . _qos ) <EOL> def alarm_arm_home ( self , code = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . _validate_code ( code , '<STR_LIT>' ) : <EOL> return <EOL> mqtt . publish ( self . hass , self . _command_topic , <EOL> self . _payload_arm_home , self . _qos ) <EOL> def alarm_arm_away ( self , code = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . _validate_code ( code , '<STR_LIT>' ) : <EOL> return <EOL> mqtt . publish ( self . hass , self . _command_topic , <EOL> self . _payload_arm_away , self . _qos ) <EOL> def _validate_code ( self , code , state ) : <EOL> """<STR_LIT>""" <EOL> check = self . _code is None or code == self . _code <EOL> if not check : <EOL> _LOGGER . warning ( '<STR_LIT>' , state ) <EOL> return check </s>
