esphome / aioesphomeapi

Python Client for ESPHome native API. Used by Home Assistant.

Home Page:https://esphome.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

log level issue

GrKoR opened this issue · comments

commented

My ESPHome device publish some logs with log level DEBUG.

If I try to subscribe to the log with subscribe_logs(log_callback, LOG_LEVEL_DEBUG) I receive nothing.
Messages are received only with subscribe_logs(log_callback, LOG_LEVEL_VERBOSE) or subscribe_logs(log_callback, LOG_LEVEL_VERY_VERBOSE).

Output example:

level: LOG_LEVEL_VERBOSE
message: "\033[0;36m[D][AirCon:976]: 0001776712: [=>] [BB 00 01 80 01 00 08 00] 1C 27 00 00 00 00 00 00 [1E 58] \033[0m"

There is a string "[D]" in output. It means that message has LOG_LEVEL = DEBUG.

It looks like an incorrect comparison somewhere in the code. "<" instead of "<=" or something like that.

You need to set your ESPHome device to publish the VERBOSE/VERY_VERBOSE logs, otherwise they are not even compiled into the firmware.

commented

I did a little research.
There is some discrepancy in the LOG_LEVEL constants of ESPHome and aioesphomeapi:

In ESPHome core/log.h

#define ESPHOME_LOG_LEVEL_NONE 0
#define ESPHOME_LOG_LEVEL_ERROR 1
#define ESPHOME_LOG_LEVEL_WARN 2
#define ESPHOME_LOG_LEVEL_INFO 3
#define ESPHOME_LOG_LEVEL_CONFIG 4
#define ESPHOME_LOG_LEVEL_DEBUG 5
#define ESPHOME_LOG_LEVEL_VERBOSE 6
#define ESPHOME_LOG_LEVEL_VERY_VERBOSE 7

In aioesphomeapi api_pb2.py

    ...
    _descriptor.EnumValueDescriptor(
      name='LOG_LEVEL_INFO', index=3, number=3,
      serialized_options=None,
      type=None),
    _descriptor.EnumValueDescriptor(
      name='LOG_LEVEL_DEBUG', index=4, number=4,
      serialized_options=None,
      type=None),
    ...

From LOG_LEVEL_NONE to LOG_LEVEL_INFO all constants are the same.
From LOG_LEVEL_DEBUG to LOG_LEVEL_VERY_VERBOSE all constants are reduced by 1.

There is no LOG_LEVEL_CONFIG in aioesphomeapi and this is the reason for shifting constants by one and the incorrect subscribe_logs(log_callback, LOG_LEVEL_DEBUG) calling.

Ok so yeah, because the protobuf does not have CONFIG as level 4, its all a level lower.
The ESPHome cli and dashboard use their own functions to attach to logs with hardcoded level 7 which is why it works. There are no applications using this library for logging which is why this has not been noticed before.

We will need to fix this on both sides, aioesphomeapi and in ESPHome device code.

@OttoWinter 2 options here:

  1. Update the proto to add the config level, or
  2. Update the defines so that both DEBUG and CONFIG are level 4 with V and VV moving down by 1 there.

I dont think it will cause any other issues going with option 2 and should be the simplest.
Either option is only going to work for updated esphome devices anyway and updating the proto enum can cause other issues.

I don't think 2 would work - the logger component uses the tag (as an int) to figure out what preamble to print before (so ANSI color + [C] or [D]). Making both the same value would break that.

So I think we need to update the protobuf definitions.