本文整理汇总了Python中homeassistant.helpers.typing.HomeAssistantType.data[DATA_MQTT]方法的典型用法代码示例。如果您正苦于以下问题:Python HomeAssistantType.data[DATA_MQTT]方法的具体用法?Python HomeAssistantType.data[DATA_MQTT]怎么用?Python HomeAssistantType.data[DATA_MQTT]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类homeassistant.helpers.typing.HomeAssistantType
的用法示例。
在下文中一共展示了HomeAssistantType.data[DATA_MQTT]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: async_setup
# 需要导入模块: from homeassistant.helpers.typing import HomeAssistantType [as 别名]
# 或者: from homeassistant.helpers.typing.HomeAssistantType import data[DATA_MQTT] [as 别名]
async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
"""Start the MQTT protocol service."""
conf = config.get(DOMAIN) # type: Optional[ConfigType]
if conf is None:
conf = CONFIG_SCHEMA({DOMAIN: {}})[DOMAIN]
conf = cast(ConfigType, conf)
client_id = conf.get(CONF_CLIENT_ID) # type: Optional[str]
keepalive = conf.get(CONF_KEEPALIVE) # type: int
# Only setup if embedded config passed in or no broker specified
if CONF_EMBEDDED not in conf and CONF_BROKER in conf:
broker_config = None
else:
broker_config = await _async_setup_server(hass, config)
if CONF_BROKER in conf:
broker = conf[CONF_BROKER] # type: str
port = conf[CONF_PORT] # type: int
username = conf.get(CONF_USERNAME) # type: Optional[str]
password = conf.get(CONF_PASSWORD) # type: Optional[str]
certificate = conf.get(CONF_CERTIFICATE) # type: Optional[str]
client_key = conf.get(CONF_CLIENT_KEY) # type: Optional[str]
client_cert = conf.get(CONF_CLIENT_CERT) # type: Optional[str]
tls_insecure = conf.get(CONF_TLS_INSECURE) # type: Optional[bool]
protocol = conf[CONF_PROTOCOL] # type: str
elif broker_config is not None:
# If no broker passed in, auto config to internal server
broker, port, username, password, certificate, protocol = broker_config
# Embedded broker doesn't have some ssl variables
client_key, client_cert, tls_insecure = None, None, None
# hbmqtt requires a client id to be set.
if client_id is None:
client_id = 'home-assistant'
else:
err = "Unable to start MQTT broker."
if conf.get(CONF_EMBEDDED) is not None:
# Explicit embedded config, requires explicit broker config
err += " (Broker configuration required.)"
_LOGGER.error(err)
return False
# For cloudmqtt.com, secured connection, auto fill in certificate
if (certificate is None and 19999 < port < 30000 and
broker.endswith('.cloudmqtt.com')):
certificate = os.path.join(os.path.dirname(__file__),
'addtrustexternalcaroot.crt')
# When the certificate is set to auto, use bundled certs from requests
if certificate == 'auto':
certificate = requests.certs.where()
will_message = None # type: Optional[Message]
if conf.get(CONF_WILL_MESSAGE) is not None:
will_message = Message(**conf.get(CONF_WILL_MESSAGE))
birth_message = None # type: Optional[Message]
if conf.get(CONF_BIRTH_MESSAGE) is not None:
birth_message = Message(**conf.get(CONF_BIRTH_MESSAGE))
# Be able to override versions other than TLSv1.0 under Python3.6
conf_tls_version = conf.get(CONF_TLS_VERSION) # type: str
if conf_tls_version == '1.2':
tls_version = ssl.PROTOCOL_TLSv1_2
elif conf_tls_version == '1.1':
tls_version = ssl.PROTOCOL_TLSv1_1
elif conf_tls_version == '1.0':
tls_version = ssl.PROTOCOL_TLSv1
else:
import sys
# Python3.6 supports automatic negotiation of highest TLS version
if sys.hexversion >= 0x03060000:
tls_version = ssl.PROTOCOL_TLS # pylint: disable=no-member
else:
tls_version = ssl.PROTOCOL_TLSv1
try:
hass.data[DATA_MQTT] = MQTT(
hass, broker, port, client_id, keepalive, username, password,
certificate, client_key, client_cert, tls_insecure, protocol,
will_message, birth_message, tls_version)
except socket.error:
_LOGGER.exception("Can't connect to the broker. "
"Please check your settings and the broker itself")
return False
async def async_stop_mqtt(event: Event):
"""Stop MQTT component."""
await hass.data[DATA_MQTT].async_disconnect()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_mqtt)
success = await hass.data[DATA_MQTT].async_connect() # type: bool
if not success:
return False
async def async_publish_service(call: ServiceCall):
"""Handle MQTT publish service calls."""
msg_topic = call.data[ATTR_TOPIC] # type: str
payload = call.data.get(ATTR_PAYLOAD)
#.........这里部分代码省略.........