2018-12-05 21:22:06 +01:00
|
|
|
from datetime import datetime
|
2018-04-18 18:43:13 +02:00
|
|
|
import hashlib
|
2018-04-07 01:23:03 +02:00
|
|
|
import logging
|
2018-09-24 15:27:38 +02:00
|
|
|
import ssl
|
2018-09-25 10:36:12 +02:00
|
|
|
import sys
|
2018-12-05 21:22:06 +01:00
|
|
|
import time
|
2023-05-17 06:29:56 +02:00
|
|
|
import json
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
from esphome.const import (
|
|
|
|
CONF_BROKER,
|
|
|
|
CONF_DISCOVERY_PREFIX,
|
|
|
|
CONF_ESPHOME,
|
|
|
|
CONF_LOG_TOPIC,
|
|
|
|
CONF_MQTT,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_SSL_FINGERPRINTS,
|
|
|
|
CONF_TOPIC,
|
|
|
|
CONF_TOPIC_PREFIX,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome.core import CORE, EsphomeError
|
2021-04-08 13:58:01 +02:00
|
|
|
from esphome.log import color, Fore
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome.util import safe_print
|
2023-05-17 06:29:56 +02:00
|
|
|
from esphome.helpers import get_str_env, get_int_env
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-05-17 06:29:56 +02:00
|
|
|
def config_from_env():
|
|
|
|
config = {
|
|
|
|
CONF_MQTT: {
|
|
|
|
CONF_USERNAME: get_str_env("ESPHOME_DASHBOARD_MQTT_USERNAME"),
|
|
|
|
CONF_PASSWORD: get_str_env("ESPHOME_DASHBOARD_MQTT_PASSWORD"),
|
|
|
|
CONF_BROKER: get_str_env("ESPHOME_DASHBOARD_MQTT_BROKER"),
|
|
|
|
CONF_PORT: get_int_env("ESPHOME_DASHBOARD_MQTT_PORT", 1883),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(
|
|
|
|
config, subscriptions, on_message, on_connect, username, password, client_id
|
|
|
|
):
|
|
|
|
client = prepare(
|
|
|
|
config, subscriptions, on_message, on_connect, username, password, client_id
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
client.loop_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def prepare(
|
|
|
|
config, subscriptions, on_message, on_connect, username, password, client_id
|
|
|
|
):
|
|
|
|
def on_connect_(client, userdata, flags, return_code):
|
2019-10-24 20:11:17 +02:00
|
|
|
_LOGGER.info("Connected to MQTT broker!")
|
2018-04-07 01:23:03 +02:00
|
|
|
for topic in subscriptions:
|
|
|
|
client.subscribe(topic)
|
2023-05-17 06:29:56 +02:00
|
|
|
if on_connect is not None:
|
|
|
|
on_connect(client, userdata, flags, return_code)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def on_disconnect(client, userdata, result_code):
|
|
|
|
if result_code == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
tries = 0
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
if client.reconnect() == 0:
|
|
|
|
_LOGGER.info("Successfully reconnected to the MQTT server")
|
|
|
|
break
|
2019-12-07 18:28:55 +01:00
|
|
|
except OSError:
|
2018-12-05 21:22:06 +01:00
|
|
|
pass
|
|
|
|
|
2022-02-01 10:26:37 +01:00
|
|
|
wait_time = min(2**tries, 300)
|
2018-12-05 21:22:06 +01:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Disconnected from MQTT (%s). Trying to reconnect in %s s",
|
2021-03-07 20:03:16 +01:00
|
|
|
result_code,
|
|
|
|
wait_time,
|
|
|
|
)
|
2018-12-05 21:22:06 +01:00
|
|
|
time.sleep(wait_time)
|
|
|
|
tries += 1
|
|
|
|
|
2021-03-07 20:03:16 +01:00
|
|
|
client = mqtt.Client(client_id or "")
|
2023-05-17 06:29:56 +02:00
|
|
|
client.on_connect = on_connect_
|
2018-04-07 01:23:03 +02:00
|
|
|
client.on_message = on_message
|
2018-12-05 21:22:06 +01:00
|
|
|
client.on_disconnect = on_disconnect
|
2018-04-07 01:23:03 +02:00
|
|
|
if username is None:
|
|
|
|
if config[CONF_MQTT].get(CONF_USERNAME):
|
2021-03-07 20:03:16 +01:00
|
|
|
client.username_pw_set(
|
|
|
|
config[CONF_MQTT][CONF_USERNAME], config[CONF_MQTT][CONF_PASSWORD]
|
|
|
|
)
|
2018-04-07 01:23:03 +02:00
|
|
|
elif username:
|
|
|
|
client.username_pw_set(username, password)
|
2018-09-24 15:27:38 +02:00
|
|
|
|
|
|
|
if config[CONF_MQTT].get(CONF_SSL_FINGERPRINTS):
|
2018-09-25 10:36:12 +02:00
|
|
|
if sys.version_info >= (2, 7, 13):
|
|
|
|
tls_version = ssl.PROTOCOL_TLS # pylint: disable=no-member
|
|
|
|
else:
|
|
|
|
tls_version = ssl.PROTOCOL_SSLv23
|
2021-03-07 20:03:16 +01:00
|
|
|
client.tls_set(
|
|
|
|
ca_certs=None,
|
|
|
|
certfile=None,
|
|
|
|
keyfile=None,
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED,
|
|
|
|
tls_version=tls_version,
|
|
|
|
ciphers=None,
|
|
|
|
)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
try:
|
2019-12-04 15:58:58 +01:00
|
|
|
host = str(config[CONF_MQTT][CONF_BROKER])
|
|
|
|
port = int(config[CONF_MQTT][CONF_PORT])
|
|
|
|
client.connect(host, port)
|
2019-12-07 18:28:55 +01:00
|
|
|
except OSError as err:
|
2020-09-16 12:12:40 +02:00
|
|
|
raise EsphomeError(f"Cannot connect to MQTT broker: {err}") from err
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2023-05-17 06:29:56 +02:00
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
def show_discover(config, username=None, password=None, client_id=None):
|
|
|
|
topic = "esphome/discover/#"
|
|
|
|
_LOGGER.info("Starting log output from %s", topic)
|
|
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
|
|
|
time_ = datetime.now().time().strftime("[%H:%M:%S]")
|
|
|
|
payload = msg.payload.decode(errors="backslashreplace")
|
|
|
|
if len(payload) > 0:
|
|
|
|
message = time_ + " " + payload
|
|
|
|
safe_print(message)
|
|
|
|
|
|
|
|
def on_connect(client, userdata, flags, return_code):
|
|
|
|
_LOGGER.info("Send discover via MQTT broker")
|
|
|
|
client.publish("esphome/discover", None, retain=False)
|
|
|
|
|
|
|
|
return initialize(
|
|
|
|
config, [topic], on_message, on_connect, username, password, client_id
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_esphome_device_ip(
|
|
|
|
config, username=None, password=None, client_id=None, timeout=25
|
|
|
|
):
|
|
|
|
if CONF_MQTT not in config:
|
|
|
|
raise EsphomeError(
|
|
|
|
"Cannot discover IP via MQTT as the config does not include the mqtt: "
|
|
|
|
"component"
|
|
|
|
)
|
|
|
|
if CONF_ESPHOME not in config or CONF_NAME not in config[CONF_ESPHOME]:
|
|
|
|
raise EsphomeError(
|
|
|
|
"Cannot discover IP via MQTT as the config does not include the device name: "
|
|
|
|
"component"
|
|
|
|
)
|
|
|
|
|
|
|
|
dev_name = config[CONF_ESPHOME][CONF_NAME]
|
|
|
|
dev_ip = None
|
|
|
|
|
|
|
|
topic = "esphome/discover/" + dev_name
|
|
|
|
_LOGGER.info("Starting looking for IP in topic %s", topic)
|
|
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
|
|
|
nonlocal dev_ip
|
|
|
|
time_ = datetime.now().time().strftime("[%H:%M:%S]")
|
|
|
|
payload = msg.payload.decode(errors="backslashreplace")
|
|
|
|
if len(payload) > 0:
|
|
|
|
message = time_ + " " + payload
|
|
|
|
_LOGGER.debug(message)
|
|
|
|
|
|
|
|
data = json.loads(payload)
|
|
|
|
if "name" not in data or data["name"] != dev_name:
|
|
|
|
_LOGGER.Warn("Wrong device answer")
|
|
|
|
return
|
|
|
|
|
|
|
|
if "ip" in data:
|
|
|
|
dev_ip = data["ip"]
|
|
|
|
client.disconnect()
|
|
|
|
|
|
|
|
def on_connect(client, userdata, flags, return_code):
|
|
|
|
topic = "esphome/ping/" + dev_name
|
|
|
|
_LOGGER.info("Send discover via MQTT broker topic: %s", topic)
|
|
|
|
client.publish(topic, None, retain=False)
|
|
|
|
|
|
|
|
mqtt_client = prepare(
|
|
|
|
config, [topic], on_message, on_connect, username, password, client_id
|
|
|
|
)
|
|
|
|
|
|
|
|
mqtt_client.loop_start()
|
|
|
|
while timeout > 0:
|
|
|
|
if dev_ip is not None:
|
|
|
|
break
|
|
|
|
timeout -= 0.250
|
|
|
|
time.sleep(0.250)
|
|
|
|
mqtt_client.loop_stop()
|
|
|
|
|
|
|
|
if dev_ip is None:
|
|
|
|
raise EsphomeError("Failed to find IP via MQTT")
|
|
|
|
|
|
|
|
_LOGGER.info("Found IP: %s", dev_ip)
|
|
|
|
return dev_ip
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def show_logs(config, topic=None, username=None, password=None, client_id=None):
|
2018-04-07 01:23:03 +02:00
|
|
|
if topic is not None:
|
|
|
|
pass # already have topic
|
2018-04-18 18:43:13 +02:00
|
|
|
elif CONF_MQTT in config:
|
|
|
|
conf = config[CONF_MQTT]
|
|
|
|
if CONF_LOG_TOPIC in conf:
|
2018-06-06 11:12:29 +02:00
|
|
|
topic = config[CONF_MQTT][CONF_LOG_TOPIC][CONF_TOPIC]
|
2018-04-18 18:43:13 +02:00
|
|
|
elif CONF_TOPIC_PREFIX in config[CONF_MQTT]:
|
2021-09-19 19:22:28 +02:00
|
|
|
topic = f"{config[CONF_MQTT][CONF_TOPIC_PREFIX]}/debug"
|
2018-04-18 18:43:13 +02:00
|
|
|
else:
|
2021-09-19 19:22:28 +02:00
|
|
|
topic = f"{config[CONF_ESPHOME][CONF_NAME]}/debug"
|
2018-04-07 01:23:03 +02:00
|
|
|
else:
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.error("MQTT isn't setup, can't start MQTT logs")
|
2018-04-18 18:43:13 +02:00
|
|
|
return 1
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.info("Starting log output from %s", topic)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
2021-03-07 20:03:16 +01:00
|
|
|
time_ = datetime.now().time().strftime("[%H:%M:%S]")
|
|
|
|
payload = msg.payload.decode(errors="backslashreplace")
|
2019-10-24 20:11:17 +02:00
|
|
|
message = time_ + payload
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(message)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2023-05-17 06:29:56 +02:00
|
|
|
return initialize(config, [topic], on_message, None, username, password, client_id)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def clear_topic(config, topic, username=None, password=None, client_id=None):
|
|
|
|
if topic is None:
|
2021-03-07 20:03:16 +01:00
|
|
|
discovery_prefix = config[CONF_MQTT].get(CONF_DISCOVERY_PREFIX, "homeassistant")
|
2019-02-13 16:54:02 +01:00
|
|
|
name = config[CONF_ESPHOME][CONF_NAME]
|
2021-03-07 20:03:16 +01:00
|
|
|
topic = f"{discovery_prefix}/+/{name}/#"
|
2019-12-07 18:28:55 +01:00
|
|
|
_LOGGER.info("Clearing messages from '%s'", topic)
|
2021-03-07 20:03:16 +01:00
|
|
|
_LOGGER.info(
|
|
|
|
"Please close this window when no more messages appear and the "
|
|
|
|
"MQTT topic has been cleared of retained messages."
|
|
|
|
)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
2018-08-13 19:11:33 +02:00
|
|
|
if not msg.payload or not msg.retain:
|
|
|
|
return
|
|
|
|
try:
|
2019-12-07 18:28:55 +01:00
|
|
|
print(f"Clearing topic {msg.topic}")
|
2018-08-13 19:11:33 +02:00
|
|
|
except UnicodeDecodeError:
|
2019-12-07 18:28:55 +01:00
|
|
|
print("Skipping non-UTF-8 topic (prohibited by MQTT standard)")
|
2018-04-07 01:23:03 +02:00
|
|
|
return
|
|
|
|
client.publish(msg.topic, None, retain=True)
|
|
|
|
|
2023-05-17 06:29:56 +02:00
|
|
|
return initialize(config, [topic], on_message, None, username, password, client_id)
|
2018-04-18 18:43:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
# From marvinroger/async-mqtt-client -> scripts/get-fingerprint/get-fingerprint.py
|
|
|
|
def get_fingerprint(config):
|
2019-12-04 15:58:58 +01:00
|
|
|
addr = str(config[CONF_MQTT][CONF_BROKER]), int(config[CONF_MQTT][CONF_PORT])
|
2018-04-18 18:43:13 +02:00
|
|
|
_LOGGER.info("Getting fingerprint from %s:%s", addr[0], addr[1])
|
|
|
|
try:
|
|
|
|
cert_pem = ssl.get_server_certificate(addr)
|
2019-12-07 18:28:55 +01:00
|
|
|
except OSError as err:
|
2018-04-18 18:43:13 +02:00
|
|
|
_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()
|
|
|
|
|
2021-09-19 19:22:28 +02:00
|
|
|
safe_print(f"SHA1 Fingerprint: {color(Fore.CYAN, sha1)}")
|
2021-03-07 20:03:16 +01:00
|
|
|
safe_print(
|
2021-09-19 19:22:28 +02:00
|
|
|
f"Copy the string above into mqtt.ssl_fingerprints section of {CORE.config_path}"
|
2021-03-07 20:03:16 +01:00
|
|
|
)
|
2018-04-18 18:43:13 +02:00
|
|
|
return 0
|