Skip to content

Commit

Permalink
Merge pull request #40 from Snuffy2/Send-Debug-Messages-to-MQTT-Status
Browse files Browse the repository at this point in the history
Send Debug Messages to MQTT
  • Loading branch information
snicker authored Nov 25, 2023
2 parents a0a3e7e + 341ec09 commit f96f52d
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions juicepassproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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 @@ -90,16 +91,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 @@ -240,11 +260,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 @@ -258,8 +300,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 @@ -589,7 +634,9 @@ def main():
device_name=args.device_name,
juicebox_id=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

0 comments on commit f96f52d

Please sign in to comment.