Skip to content

Commit

Permalink
Moved json dump from init to sensors
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Karlquist <[email protected]>
  • Loading branch information
ha-enthus1ast committed Nov 22, 2023
1 parent cea9fad commit 6a771d5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
12 changes: 4 additions & 8 deletions ha_mqtt_discoverable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def _connect_client(self) -> None:

def _state_helper(
self,
state: Optional[str | dict[str, Any]],
state: Optional[str],
topic: Optional[str] = None,
retain=True,
) -> MQTTMessageInfo:
Expand All @@ -751,18 +751,14 @@ def _state_helper(
if not topic:
logger.debug(f"State topic unset, using default: {self.state_topic}")
topic = self.state_topic
if state and isinstance(state, dict):
logger.debug("State is dict, dumping state to json")
state_payload = json.dumps(state)
else:
state_payload = state
logger.debug(f"Writing '{state_payload}' to {topic}")

logger.debug(f"Writing '{state}' to {topic}")

if self._settings.debug:
logger.debug(f"Debug is {self.debug}, skipping state write")
return

message_info = self.mqtt_client.publish(topic, state_payload, retain=retain)
message_info = self.mqtt_client.publish(topic, state, retain=retain)
logger.debug(f"Publish result: {message_info}")
return message_info

Expand Down
10 changes: 5 additions & 5 deletions ha_mqtt_discoverable/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Required to define a class itself as type https://stackoverflow.com/a/33533514
from __future__ import annotations

import json
import logging
from typing import Any, Optional

Expand Down Expand Up @@ -318,7 +319,6 @@ def color(self, color_mode: str, color: dict[str, Any]) -> None:
state_payload = {
"color_mode": color_mode,
"color": color,
"color_temp": 155,
"state": self._entity.payload_on,
}
self._update_state(state_payload)
Expand All @@ -342,17 +342,17 @@ def effect(self, effect: str) -> None:
}
self._update_state(state_payload)

def _update_state(self, state: str | dict[str, Any]) -> None:
def _update_state(self, state: dict[str, Any]) -> None:
"""
Update MQTT sensor state
Args:
state(str | Dict[str, Any]): What state to set the light to
state(Dict[str, Any]): What state to set the light to
"""
logger.info(f"Setting {self._entity.name} to {state} using {self.state_topic}")

json_state = json.dumps(state)
self._state_helper(
state=state, topic=self.state_topic, retain=self._entity.retain
state=json_state, topic=self.state_topic, retain=self._entity.retain
)


Expand Down
2 changes: 0 additions & 2 deletions tests/test_light.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import pytest

from ha_mqtt_discoverable import Settings
from ha_mqtt_discoverable.sensors import Light, LightInfo


# Test data
color_modes = ["rgb", "rgbw"]
effects = ["rainbow", "mycustomeffect"]
Expand Down

0 comments on commit 6a771d5

Please sign in to comment.