mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Cast attribute values to string before publishing to MQTT (#9872)
* Cast attribute values to string before publishing to MQTT * Simplify * Use JSON serialization, add test
This commit is contained in:
parent
2d93285689
commit
248d974ded
@ -5,6 +5,7 @@ For more details about this component, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/mqtt_statestream/
|
https://home-assistant.io/components/mqtt_statestream/
|
||||||
"""
|
"""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ from homeassistant.const import MATCH_ALL
|
|||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.components.mqtt import valid_publish_topic
|
from homeassistant.components.mqtt import valid_publish_topic
|
||||||
from homeassistant.helpers.event import async_track_state_change
|
from homeassistant.helpers.event import async_track_state_change
|
||||||
|
from homeassistant.remote import JSONEncoder
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
CONF_BASE_TOPIC = 'base_topic'
|
CONF_BASE_TOPIC = 'base_topic'
|
||||||
@ -65,8 +67,9 @@ def async_setup(hass, config):
|
|||||||
if publish_attributes:
|
if publish_attributes:
|
||||||
for key, val in new_state.attributes.items():
|
for key, val in new_state.attributes.items():
|
||||||
if val:
|
if val:
|
||||||
|
encoded_val = json.dumps(val, cls=JSONEncoder)
|
||||||
hass.components.mqtt.async_publish(mybase + key,
|
hass.components.mqtt.async_publish(mybase + key,
|
||||||
val, 1, True)
|
encoded_val, 1, True)
|
||||||
|
|
||||||
async_track_state_change(hass, MATCH_ALL, _state_publisher)
|
async_track_state_change(hass, MATCH_ALL, _state_publisher)
|
||||||
return True
|
return True
|
||||||
|
@ -127,7 +127,11 @@ class TestMqttStateStream(object):
|
|||||||
# mqtt_statestream state change on initialization, etc.
|
# mqtt_statestream state change on initialization, etc.
|
||||||
mock_pub.reset_mock()
|
mock_pub.reset_mock()
|
||||||
|
|
||||||
test_attributes = {"testing": "YES"}
|
test_attributes = {
|
||||||
|
"testing": "YES",
|
||||||
|
"list": ["a", "b", "c"],
|
||||||
|
"bool": True
|
||||||
|
}
|
||||||
|
|
||||||
# Set a state of an entity
|
# Set a state of an entity
|
||||||
mock_state_change_event(self.hass, State(e_id, 'off',
|
mock_state_change_event(self.hass, State(e_id, 'off',
|
||||||
@ -138,7 +142,11 @@ class TestMqttStateStream(object):
|
|||||||
calls = [
|
calls = [
|
||||||
call.async_publish(self.hass, 'pub/fake/entity/state', 'off', 1,
|
call.async_publish(self.hass, 'pub/fake/entity/state', 'off', 1,
|
||||||
True),
|
True),
|
||||||
call.async_publish(self.hass, 'pub/fake/entity/testing', 'YES',
|
call.async_publish(self.hass, 'pub/fake/entity/testing', '"YES"',
|
||||||
|
1, True),
|
||||||
|
call.async_publish(self.hass, 'pub/fake/entity/list',
|
||||||
|
'["a", "b", "c"]', 1, True),
|
||||||
|
call.async_publish(self.hass, 'pub/fake/entity/bool', "true",
|
||||||
1, True)
|
1, True)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user