MQTT different log level (#167)

* Add option to have different log level over MQTT

* Add Test

* Lint
This commit is contained in:
Otto Winter 2018-10-14 18:46:17 +02:00 committed by GitHub
parent 6f54afec00
commit 8d395e5338
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 9 deletions

View file

@ -2,16 +2,16 @@ import re
import voluptuous as vol import voluptuous as vol
import esphomeyaml.config_validation as cv
from esphomeyaml import automation from esphomeyaml import automation
from esphomeyaml.components import logger
import esphomeyaml.config_validation as cv
from esphomeyaml.const import CONF_BIRTH_MESSAGE, CONF_BROKER, CONF_CLIENT_ID, CONF_DISCOVERY, \ from esphomeyaml.const import CONF_BIRTH_MESSAGE, CONF_BROKER, CONF_CLIENT_ID, CONF_DISCOVERY, \
CONF_DISCOVERY_PREFIX, CONF_DISCOVERY_RETAIN, CONF_ID, CONF_KEEPALIVE, CONF_LOG_TOPIC, \ CONF_DISCOVERY_PREFIX, CONF_DISCOVERY_RETAIN, CONF_ID, CONF_KEEPALIVE, CONF_LEVEL, \
CONF_ON_MESSAGE, CONF_PASSWORD, CONF_PAYLOAD, CONF_PORT, CONF_QOS, CONF_RETAIN, \ CONF_LOG_TOPIC, CONF_ON_MESSAGE, CONF_PASSWORD, CONF_PAYLOAD, CONF_PORT, CONF_QOS, \
CONF_SSL_FINGERPRINTS, CONF_TOPIC, CONF_TOPIC_PREFIX, CONF_TRIGGER_ID, CONF_USERNAME, \ CONF_REBOOT_TIMEOUT, CONF_RETAIN, CONF_SHUTDOWN_MESSAGE, CONF_SSL_FINGERPRINTS, CONF_TOPIC, \
CONF_WILL_MESSAGE, CONF_REBOOT_TIMEOUT, CONF_SHUTDOWN_MESSAGE CONF_TOPIC_PREFIX, CONF_TRIGGER_ID, CONF_USERNAME, CONF_WILL_MESSAGE
from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, RawExpression, \ from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, RawExpression, \
StructInitializer, \ StructInitializer, TemplateArguments, add, esphomelib_ns, optional, std_string
TemplateArguments, add, esphomelib_ns, optional, std_string
def validate_message_just_topic(value): def validate_message_just_topic(value):
@ -68,7 +68,9 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_WILL_MESSAGE): MQTT_MESSAGE_SCHEMA, vol.Optional(CONF_WILL_MESSAGE): MQTT_MESSAGE_SCHEMA,
vol.Optional(CONF_SHUTDOWN_MESSAGE): MQTT_MESSAGE_SCHEMA, vol.Optional(CONF_SHUTDOWN_MESSAGE): MQTT_MESSAGE_SCHEMA,
vol.Optional(CONF_TOPIC_PREFIX): cv.publish_topic, vol.Optional(CONF_TOPIC_PREFIX): cv.publish_topic,
vol.Optional(CONF_LOG_TOPIC): MQTT_MESSAGE_TEMPLATE_SCHEMA, vol.Optional(CONF_LOG_TOPIC): vol.Any(None, MQTT_MESSAGE_BASE.extend({
vol.Optional(CONF_LEVEL): logger.is_log_level,
}), validate_message_just_topic),
vol.Optional(CONF_SSL_FINGERPRINTS): vol.All(cv.only_on_esp8266, vol.Optional(CONF_SSL_FINGERPRINTS): vol.All(cv.only_on_esp8266,
cv.ensure_list, [validate_fingerprint]), cv.ensure_list, [validate_fingerprint]),
vol.Optional(CONF_KEEPALIVE): cv.positive_time_period_seconds, vol.Optional(CONF_KEEPALIVE): cv.positive_time_period_seconds,
@ -98,6 +100,7 @@ def to_code(config):
rhs = App.init_mqtt(config[CONF_BROKER], config[CONF_PORT], rhs = App.init_mqtt(config[CONF_BROKER], config[CONF_PORT],
config[CONF_USERNAME], config[CONF_PASSWORD]) config[CONF_USERNAME], config[CONF_PASSWORD])
mqtt = Pvariable(config[CONF_ID], rhs) mqtt = Pvariable(config[CONF_ID], rhs)
if not config.get(CONF_DISCOVERY, True): if not config.get(CONF_DISCOVERY, True):
add(mqtt.disable_discovery()) add(mqtt.disable_discovery())
elif CONF_DISCOVERY_RETAIN in config or CONF_DISCOVERY_PREFIX in config: elif CONF_DISCOVERY_RETAIN in config or CONF_DISCOVERY_PREFIX in config:
@ -106,6 +109,7 @@ def to_code(config):
add(mqtt.set_discovery_info(discovery_prefix, discovery_retain)) add(mqtt.set_discovery_info(discovery_prefix, discovery_retain))
if CONF_TOPIC_PREFIX in config: if CONF_TOPIC_PREFIX in config:
add(mqtt.set_topic_prefix(config[CONF_TOPIC_PREFIX])) add(mqtt.set_topic_prefix(config[CONF_TOPIC_PREFIX]))
if CONF_BIRTH_MESSAGE in config: if CONF_BIRTH_MESSAGE in config:
birth_message = config[CONF_BIRTH_MESSAGE] birth_message = config[CONF_BIRTH_MESSAGE]
if not birth_message: if not birth_message:
@ -124,20 +128,28 @@ def to_code(config):
add(mqtt.disable_shutdown_message()) add(mqtt.disable_shutdown_message())
else: else:
add(mqtt.set_shutdown_message(exp_mqtt_message(shutdown_message))) add(mqtt.set_shutdown_message(exp_mqtt_message(shutdown_message)))
if CONF_CLIENT_ID in config: if CONF_CLIENT_ID in config:
add(mqtt.set_client_id(config[CONF_CLIENT_ID])) add(mqtt.set_client_id(config[CONF_CLIENT_ID]))
if CONF_LOG_TOPIC in config: if CONF_LOG_TOPIC in config:
log_topic = config[CONF_LOG_TOPIC] log_topic = config[CONF_LOG_TOPIC]
if not log_topic: if not log_topic:
add(mqtt.disable_log_message()) add(mqtt.disable_log_message())
else: else:
add(mqtt.set_log_message_template(exp_mqtt_message(log_topic))) add(mqtt.set_log_message_template(exp_mqtt_message(log_topic)))
if CONF_LEVEL in config:
add(mqtt.set_log_level(logger.LOG_LEVELS[config[CONF_LEVEL]]))
if CONF_SSL_FINGERPRINTS in config: if CONF_SSL_FINGERPRINTS in config:
for fingerprint in config[CONF_SSL_FINGERPRINTS]: for fingerprint in config[CONF_SSL_FINGERPRINTS]:
arr = [RawExpression("0x{}".format(fingerprint[i:i + 2])) for i in range(0, 40, 2)] arr = [RawExpression("0x{}".format(fingerprint[i:i + 2])) for i in range(0, 40, 2)]
add(mqtt.add_ssl_fingerprint(ArrayInitializer(*arr, multiline=False))) add(mqtt.add_ssl_fingerprint(ArrayInitializer(*arr, multiline=False)))
if CONF_KEEPALIVE in config: if CONF_KEEPALIVE in config:
add(mqtt.set_keep_alive(config[CONF_KEEPALIVE])) add(mqtt.set_keep_alive(config[CONF_KEEPALIVE]))
if CONF_REBOOT_TIMEOUT in config: if CONF_REBOOT_TIMEOUT in config:
add(mqtt.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT])) add(mqtt.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))

View file

@ -47,7 +47,9 @@ mqtt:
discovery_retain: False discovery_retain: False
discovery_prefix: discovery discovery_prefix: discovery
topic_prefix: helloworld topic_prefix: helloworld
log_topic: helloworld/hi log_topic:
topic: helloworld/hi
level: INFO
birth_message: birth_message:
will_message: will_message:
shutdown_message: shutdown_message: