Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya committed Oct 29, 2023
1 parent e031092 commit 91e115b
Show file tree
Hide file tree
Showing 2 changed files with 454 additions and 443 deletions.
15 changes: 13 additions & 2 deletions ecowitt2mqtt/helpers/publisher/mqtt/hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import asyncio
from dataclasses import asdict, dataclass
from typing import TypedDict
from typing import Any, TypedDict

from aiomqtt import Client, MqttError

Expand Down Expand Up @@ -184,6 +184,12 @@ class HassDiscoveryInfo:
state_class: str | None = None
unit_of_measurement: str | None = None

@staticmethod
def dict_factory(x: list[tuple[str, Any]]) -> dict[str, Any]:
"""Define a default dict factory for the dataclass."""
exclude_fields = ("config_topic",)
return {k: v for (k, v) in x if ((v is not None) and (k not in exclude_fields))}


AVAILABILITY_OFFLINE = "offline"
AVAILABILITY_ONLINE = "online"
Expand Down Expand Up @@ -564,7 +570,12 @@ async def async_publish(self, data: dict[str, CalculatedValueType]) -> None:
asyncio.create_task(
self._client.publish(
discovery_info.config_topic,
payload=generate_mqtt_payload(asdict(discovery_info)),
payload=generate_mqtt_payload(
asdict(
discovery_info,
dict_factory=HassDiscoveryInfo.dict_factory,
)
),
retain=self._config.mqtt_retain,
)
)
Expand Down
Loading

0 comments on commit 91e115b

Please sign in to comment.