Merge branch 'dev' into bump-1.15.0b1

This commit is contained in:
Otto Winter
2020-07-26 22:12:53 +02:00
650 changed files with 29231 additions and 6310 deletions
+25 -26
View File
@@ -1,9 +1,6 @@
from __future__ import print_function
from datetime import datetime
import hashlib
import logging
import socket
import ssl
import sys
import time
@@ -37,7 +34,7 @@ def initialize(config, subscriptions, on_message, username, password, client_id)
if client.reconnect() == 0:
_LOGGER.info("Successfully reconnected to the MQTT server")
break
except socket.error:
except OSError:
pass
wait_time = min(2**tries, 300)
@@ -47,7 +44,7 @@ def initialize(config, subscriptions, on_message, username, password, client_id)
time.sleep(wait_time)
tries += 1
client = mqtt.Client(client_id or u'')
client = mqtt.Client(client_id or '')
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
@@ -67,9 +64,11 @@ def initialize(config, subscriptions, on_message, username, password, client_id)
tls_version=tls_version, ciphers=None)
try:
client.connect(config[CONF_MQTT][CONF_BROKER], config[CONF_MQTT][CONF_PORT])
except socket.error as err:
raise EsphomeError("Cannot connect to MQTT broker: {}".format(err))
host = str(config[CONF_MQTT][CONF_BROKER])
port = int(config[CONF_MQTT][CONF_PORT])
client.connect(host, port)
except OSError as err:
raise EsphomeError(f"Cannot connect to MQTT broker: {err}")
try:
client.loop_forever()
@@ -86,17 +85,17 @@ def show_logs(config, topic=None, username=None, password=None, client_id=None):
if CONF_LOG_TOPIC in conf:
topic = config[CONF_MQTT][CONF_LOG_TOPIC][CONF_TOPIC]
elif CONF_TOPIC_PREFIX in config[CONF_MQTT]:
topic = config[CONF_MQTT][CONF_TOPIC_PREFIX] + u'/debug'
topic = config[CONF_MQTT][CONF_TOPIC_PREFIX] + '/debug'
else:
topic = config[CONF_ESPHOME][CONF_NAME] + u'/debug'
topic = config[CONF_ESPHOME][CONF_NAME] + '/debug'
else:
_LOGGER.error(u"MQTT isn't setup, can't start MQTT logs")
_LOGGER.error("MQTT isn't setup, can't start MQTT logs")
return 1
_LOGGER.info(u"Starting log output from %s", topic)
_LOGGER.info("Starting log output from %s", topic)
def on_message(client, userdata, msg):
time_ = datetime.now().time().strftime(u'[%H:%M:%S]')
payload = decode_text(msg.payload)
time_ = datetime.now().time().strftime('[%H:%M:%S]')
payload = msg.payload.decode(errors='backslashreplace')
message = time_ + payload
safe_print(message)
@@ -105,20 +104,20 @@ def show_logs(config, topic=None, username=None, password=None, client_id=None):
def clear_topic(config, topic, username=None, password=None, client_id=None):
if topic is None:
discovery_prefix = config[CONF_MQTT].get(CONF_DISCOVERY_PREFIX, u'homeassistant')
discovery_prefix = config[CONF_MQTT].get(CONF_DISCOVERY_PREFIX, 'homeassistant')
name = config[CONF_ESPHOME][CONF_NAME]
topic = u'{}/+/{}/#'.format(discovery_prefix, name)
_LOGGER.info(u"Clearing messages from '%s'", topic)
_LOGGER.info(u"Please close this window when no more messages appear and the "
u"MQTT topic has been cleared of retained messages.")
topic = f'{discovery_prefix}/+/{name}/#'
_LOGGER.info("Clearing messages from '%s'", topic)
_LOGGER.info("Please close this window when no more messages appear and the "
"MQTT topic has been cleared of retained messages.")
def on_message(client, userdata, msg):
if not msg.payload or not msg.retain:
return
try:
print(u"Clearing topic {}".format(msg.topic))
print(f"Clearing topic {msg.topic}")
except UnicodeDecodeError:
print(u"Skipping non-UTF-8 topic (prohibited by MQTT standard)")
print("Skipping non-UTF-8 topic (prohibited by MQTT standard)")
return
client.publish(msg.topic, None, retain=True)
@@ -127,18 +126,18 @@ def clear_topic(config, topic, username=None, password=None, client_id=None):
# From marvinroger/async-mqtt-client -> scripts/get-fingerprint/get-fingerprint.py
def get_fingerprint(config):
addr = config[CONF_MQTT][CONF_BROKER], config[CONF_MQTT][CONF_PORT]
addr = str(config[CONF_MQTT][CONF_BROKER]), int(config[CONF_MQTT][CONF_PORT])
_LOGGER.info("Getting fingerprint from %s:%s", addr[0], addr[1])
try:
cert_pem = ssl.get_server_certificate(addr)
except IOError as err:
except OSError as err:
_LOGGER.error("Unable to connect to server: %s", err)
return 1
cert_der = ssl.PEM_cert_to_DER_cert(cert_pem)
sha1 = hashlib.sha1(cert_der).hexdigest()
safe_print(u"SHA1 Fingerprint: " + color('cyan', sha1))
safe_print(u"Copy the string above into mqtt.ssl_fingerprints section of {}"
u"".format(CORE.config_path))
safe_print("SHA1 Fingerprint: " + color('cyan', sha1))
safe_print("Copy the string above into mqtt.ssl_fingerprints section of {}"
"".format(CORE.config_path))
return 0