<s> """<STR_LIT>""" <EOL> import logging <EOL> import voluptuous as vol <EOL> from homeassistant . const import ( STATE_OPEN , STATE_CLOSED , SERVICE_OPEN , <EOL> SERVICE_CLOSE ) <EOL> import homeassistant . components . mqtt as mqtt <EOL> from homeassistant . components . garage_door import GarageDoorDevice <EOL> from homeassistant . const import ( <EOL> CONF_NAME , CONF_OPTIMISTIC , CONF_VALUE_TEMPLATE ) <EOL> from homeassistant . components . mqtt import ( <EOL> CONF_STATE_TOPIC , CONF_COMMAND_TOPIC , CONF_QOS , CONF_RETAIN ) <EOL> import homeassistant . helpers . config_validation as cv <EOL> from homeassistant . helpers import template <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> CONF_STATE_OPEN = '<STR_LIT>' <EOL> CONF_STATE_CLOSED = '<STR_LIT>' <EOL> CONF_SERVICE_OPEN = '<STR_LIT>' <EOL> CONF_SERVICE_CLOSE = '<STR_LIT>' <EOL> DEFAULT_NAME = '<STR_LIT>' <EOL> DEFAULT_OPTIMISTIC = False <EOL> DEFAULT_RETAIN = False <EOL> DEPENDENCIES = [ '<STR_LIT>' ] <EOL> PLATFORM_SCHEMA = mqtt . MQTT_RW_PLATFORM_SCHEMA . extend ( { <EOL> vol . Optional ( CONF_NAME , default = DEFAULT_NAME ) : cv . string , <EOL> vol . Optional ( CONF_VALUE_TEMPLATE ) : cv . template , <EOL> vol . Optional ( CONF_OPTIMISTIC , default = DEFAULT_OPTIMISTIC ) : cv . boolean , <EOL> vol . Optional ( CONF_RETAIN , default = DEFAULT_RETAIN ) : cv . boolean , <EOL> vol . Optional ( CONF_STATE_OPEN , default = STATE_OPEN ) : cv . string , <EOL> vol . Optional ( CONF_STATE_CLOSED , default = STATE_CLOSED ) : cv . string , <EOL> vol . Optional ( CONF_SERVICE_OPEN , default = SERVICE_OPEN ) : cv . string , <EOL> vol . Optional ( CONF_SERVICE_CLOSE , default = SERVICE_CLOSE ) : cv . string <EOL> } ) <EOL> def setup_platform ( hass , config , add_devices_callback , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> add_devices_callback ( [ MqttGarageDoor ( <EOL> hass , <EOL> config [ CONF_NAME ] , <EOL> config . get ( CONF_STATE_TOPIC ) , <EOL> config [ CONF_COMMAND_TOPIC ] , <EOL> config [ CONF_QOS ] , <EOL> config [ CONF_RETAIN ] , <EOL> config [ CONF_STATE_OPEN ] , <EOL> config [ CONF_STATE_CLOSED ] , <EOL> config [ CONF_SERVICE_OPEN ] , <EOL> config [ CONF_SERVICE_CLOSE ] , <EOL> config [ CONF_OPTIMISTIC ] , <EOL> config . get ( CONF_VALUE_TEMPLATE ) ) ] ) <EOL> class MqttGarageDoor ( GarageDoorDevice ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hass , name , state_topic , command_topic , qos , retain , <EOL> state_open , state_closed , service_open , service_close , <EOL> optimistic , value_template ) : <EOL> """<STR_LIT>""" <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 . _retain = retain <EOL> self . _state_open = state_open <EOL> self . _state_closed = state_closed <EOL> self . _service_open = service_open <EOL> self . _service_close = service_close <EOL> self . _optimistic = optimistic or state_topic is None <EOL> self . _state = False <EOL> def message_received ( topic , payload , qos ) : <EOL> """<STR_LIT>""" <EOL> if value_template is not None : <EOL> payload = template . render_with_possible_json_value ( <EOL> hass , value_template , payload ) <EOL> if payload == self . _state_open : <EOL> self . _state = True <EOL> self . update_ha_state ( ) <EOL> elif payload == self . _state_closed : <EOL> self . _state = False <EOL> self . update_ha_state ( ) <EOL> if self . _state_topic is None : <EOL> self . _optimistic = True <EOL> else : <EOL> mqtt . subscribe ( hass , self . _state_topic , message_received , <EOL> self . _qos ) <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _name <EOL> @ property <EOL> def is_opened ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _state <EOL> @ property <EOL> def is_closed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _state is False <EOL> @ property <EOL> def assumed_state ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _optimistic <EOL> def close_door ( self ) : <EOL> """<STR_LIT>""" <EOL> mqtt . publish ( self . hass , self . _command_topic , self . _service_close , <EOL> self . _qos , self . _retain ) <EOL> if self . _optimistic : <EOL> self . _state = False <EOL> self . update_ha_state ( ) <EOL> def open_door ( self ) : <EOL> """<STR_LIT>""" <EOL> mqtt . publish ( self . hass , self . _command_topic , self . _service_open , <EOL> self . _qos , self . _retain ) <EOL> if self . _optimistic : <EOL> self . _state = True <EOL> self . update_ha_state ( ) </s>
