Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send Debug Messages to MQTT #40

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions juicepassproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self, device_name, mqtt_settings, juicebox_id=None):
"temperature": None,
"voltage": None,
"power": None,
"debug_message": None,
}
self._init_devices()

Expand All @@ -71,16 +72,35 @@ def _init_devices(self):
self._init_device_temperature(device_info)
self._init_device_voltage(device_info)
self._init_device_power(device_info)
self._init_debug_message(device_info)

def _init_device_status(self, device_info):
name = "Status"
sensor_info = SensorInfo(
name=name, unique_id=f"{self.juicebox_id} {name}", device=device_info
name=name,
unique_id=f"{self.juicebox_id} {name}",
icon="mdi:ev-station",
device=device_info,
)
settings = Settings(mqtt=self.mqtt_settings, entity=sensor_info)
sensor = Sensor(settings)
self.entities["status"] = sensor

def _init_debug_message(self, device_info):
name = "Last Debug Message"
sensor_info = SensorInfo(
name=name,
unique_id=f"{self.juicebox_id} {name}",
expire_after=60,
enabled_by_default=False,
icon="mdi:bug",
entity_category="diagnostic",
device=device_info,
)
settings = Settings(mqtt=self.mqtt_settings, entity=sensor_info)
sensor = Sensor(settings)
self.entities["debug_message"] = sensor

def _init_device_current(self, device_info):
name = "Current"
sensor_info = SensorInfo(
Expand Down Expand Up @@ -214,11 +234,33 @@ def basic_message_try_parse(self, data):
message["power"] = round(
message.get("voltage", 0) * message.get("current", 0), 2
)
logging.debug(f"message: {message}")
return message

def debug_message_try_parse(self, data):
message = {"type": "debug"}
dbg_data = (
str(data)
.replace("https://", "https//")
.replace("http://", "http//")
.split(":")
)
dbg_level_abbr = dbg_data[1].split(",")[1]
if dbg_level_abbr == "NFO":
dbg_level = "INFO"
elif dbg_level_abbr == "WRN":
dbg_level = "WARNING"
elif dbg_level_abbr == "ERR":
dbg_level = "ERROR"
else:
dbg_level = dbg_level_abbr
dbg_msg = (
dbg_data[2].replace("https//", "https://").replace("http//", "http://")
)
message["debug_message"] = f"{dbg_level}: {dbg_msg}"
return message

def basic_message_publish(self, message):
logging.debug("basic message {}".format(message))
logging.debug(f"{message.get('type')} message: {message}")
try:
for k in message:
entity = self.entities.get(k)
Expand All @@ -232,8 +274,11 @@ def remote_data_handler(self, data):
return data

def local_data_handler(self, data):
logging.debug("local : {}".format(data))
message = self.basic_message_try_parse(data)
logging.debug("local: {}".format(data))
if ":DBG," in str(data):
message = self.debug_message_try_parse(data)
else:
message = self.basic_message_try_parse(data)
if message:
self.basic_message_publish(message)
return data
Expand Down Expand Up @@ -396,7 +441,9 @@ def main():
device_name=args.device_name,
juicebox_id=args.juicebox_id,
)

handler.basic_message_publish(
{"type": "debug", "debug_message": "INFO: Starting JuicePass Proxy"}
)
pyproxy.LOCAL_DATA_HANDLER = handler.local_data_handler
pyproxy.REMOTE_DATA_HANDLER = handler.remote_data_handler

Expand Down