Skip to content

Commit

Permalink
fix: support older protobuf version
Browse files Browse the repository at this point in the history
Issue: #6
  • Loading branch information
broglep committed Nov 23, 2024
1 parent 1866dc6 commit d50cb3b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions custom_components/meshtastic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from functools import wraps
from typing import Any

from google.protobuf.json_format import MessageToJson
from google.protobuf.message import Message
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import IntegrationError
from pubsub import pub
Expand Down Expand Up @@ -267,20 +269,28 @@ async def _process_meshtastic_packet(self, packet: dict[str, Any]) -> None:

async def async_get_channels(self) -> list[dict]:
return [
json.loads(meshtastic.util.message_to_json(c))
json.loads(self._message_to_json(c))
for c in self._interface.localNode.channels
]

async def async_get_node_local_config(self) -> list[dict]:
async def async_get_node_local_config(self) -> dict:
return json.loads(
meshtastic.util.message_to_json(self._interface.localNode.localConfig)
self._message_to_json(self._interface.localNode.localConfig)
)

async def async_get_node_module_config(self) -> list[dict]:
async def async_get_node_module_config(self) -> dict:
return json.loads(
meshtastic.util.message_to_json(self._interface.localNode.moduleConfig)
self._message_to_json(self._interface.localNode.moduleConfig)
)

def _message_to_json(self, message: Message) -> str:
try:
return MessageToJson(message, always_print_fields_with_no_presence=True)
except TypeError:
# older protobuf version
return MessageToJson(message, including_default_value_fields=True)


async def async_get_own_node(self) -> Any:
return self._interface.getMyNodeInfo() or {}

Expand Down

0 comments on commit d50cb3b

Please sign in to comment.