Add JSON attribute topic to MQTT climate (#20239)
This commit is contained in:
parent
89e9d827a2
commit
dbba3eb0d4
2 changed files with 109 additions and 4 deletions
|
@ -22,7 +22,8 @@ from homeassistant.const import (
|
||||||
STATE_OFF)
|
STATE_OFF)
|
||||||
from homeassistant.components.mqtt import (
|
from homeassistant.components.mqtt import (
|
||||||
ATTR_DISCOVERY_HASH, CONF_QOS, CONF_RETAIN, MQTT_BASE_PLATFORM_SCHEMA,
|
ATTR_DISCOVERY_HASH, CONF_QOS, CONF_RETAIN, MQTT_BASE_PLATFORM_SCHEMA,
|
||||||
MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo, subscription)
|
MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
||||||
|
MqttEntityDeviceInfo, subscription)
|
||||||
from homeassistant.components.mqtt.discovery import (
|
from homeassistant.components.mqtt.discovery import (
|
||||||
MQTT_DISCOVERY_NEW, clear_discovery_hash)
|
MQTT_DISCOVERY_NEW, clear_discovery_hash)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
@ -144,7 +145,8 @@ PLATFORM_SCHEMA = SCHEMA_BASE.extend({
|
||||||
vol.Optional(CONF_TEMP_STEP, default=1.0): vol.Coerce(float),
|
vol.Optional(CONF_TEMP_STEP, default=1.0): vol.Coerce(float),
|
||||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||||
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
|
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
|
||||||
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
|
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema).extend(
|
||||||
|
mqtt.MQTT_JSON_ATTRS_SCHEMA.schema)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
|
async def async_setup_platform(hass: HomeAssistantType, config: ConfigType,
|
||||||
|
@ -183,8 +185,8 @@ async def _async_setup_entity(hass, config, async_add_entities,
|
||||||
)])
|
)])
|
||||||
|
|
||||||
|
|
||||||
class MqttClimate(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
|
class MqttClimate(MqttAttributes, MqttAvailability, MqttDiscoveryUpdate,
|
||||||
ClimateDevice):
|
MqttEntityDeviceInfo, ClimateDevice):
|
||||||
"""Representation of an MQTT climate device."""
|
"""Representation of an MQTT climate device."""
|
||||||
|
|
||||||
def __init__(self, hass, config, discovery_hash):
|
def __init__(self, hass, config, discovery_hash):
|
||||||
|
@ -210,6 +212,7 @@ class MqttClimate(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
|
||||||
|
|
||||||
device_config = config.get(CONF_DEVICE)
|
device_config = config.get(CONF_DEVICE)
|
||||||
|
|
||||||
|
MqttAttributes.__init__(self, config)
|
||||||
MqttAvailability.__init__(self, config)
|
MqttAvailability.__init__(self, config)
|
||||||
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
MqttDiscoveryUpdate.__init__(self, discovery_hash,
|
||||||
self.discovery_update)
|
self.discovery_update)
|
||||||
|
@ -225,6 +228,7 @@ class MqttClimate(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
|
||||||
config = PLATFORM_SCHEMA(discovery_payload)
|
config = PLATFORM_SCHEMA(discovery_payload)
|
||||||
self._config = config
|
self._config = config
|
||||||
self._setup_from_config(config)
|
self._setup_from_config(config)
|
||||||
|
await self.attributes_discovery_update(config)
|
||||||
await self.availability_discovery_update(config)
|
await self.availability_discovery_update(config)
|
||||||
await self._subscribe_topics()
|
await self._subscribe_topics()
|
||||||
self.async_schedule_update_ha_state()
|
self.async_schedule_update_ha_state()
|
||||||
|
@ -463,6 +467,7 @@ class MqttClimate(MqttAvailability, MqttDiscoveryUpdate, MqttEntityDeviceInfo,
|
||||||
"""Unsubscribe when removed."""
|
"""Unsubscribe when removed."""
|
||||||
self._sub_state = await subscription.async_unsubscribe_topics(
|
self._sub_state = await subscription.async_unsubscribe_topics(
|
||||||
self.hass, self._sub_state)
|
self.hass, self._sub_state)
|
||||||
|
await MqttAttributes.async_will_remove_from_hass(self)
|
||||||
await MqttAvailability.async_will_remove_from_hass(self)
|
await MqttAvailability.async_will_remove_from_hass(self)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -674,6 +674,106 @@ class TestMQTTClimate(unittest.TestCase):
|
||||||
assert 0.01 == temp_step
|
assert 0.01 == temp_step
|
||||||
|
|
||||||
|
|
||||||
|
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
||||||
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||||
|
assert await async_setup_component(hass, climate.DOMAIN, {
|
||||||
|
climate.DOMAIN: {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'state_topic': 'test-topic',
|
||||||
|
'json_attributes_topic': 'attr-topic'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, 'attr-topic', '{ "val": "100" }')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get('climate.test')
|
||||||
|
|
||||||
|
assert '100' == state.attributes.get('val')
|
||||||
|
|
||||||
|
|
||||||
|
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
|
||||||
|
"""Test attributes get extracted from a JSON result."""
|
||||||
|
assert await async_setup_component(hass, climate.DOMAIN, {
|
||||||
|
climate.DOMAIN: {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'state_topic': 'test-topic',
|
||||||
|
'json_attributes_topic': 'attr-topic'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, 'attr-topic', '[ "list", "of", "things"]')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get('climate.test')
|
||||||
|
|
||||||
|
assert state.attributes.get('val') is None
|
||||||
|
assert 'JSON result was not a dictionary' in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
|
||||||
|
"""Test attributes get extracted from a JSON result."""
|
||||||
|
assert await async_setup_component(hass, climate.DOMAIN, {
|
||||||
|
climate.DOMAIN: {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'state_topic': 'test-topic',
|
||||||
|
'json_attributes_topic': 'attr-topic'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, 'attr-topic', 'This is not JSON')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get('climate.test')
|
||||||
|
assert state.attributes.get('val') is None
|
||||||
|
assert 'Erroneous JSON: This is not JSON' in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
|
||||||
|
"""Test update of discovered MQTTAttributes."""
|
||||||
|
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||||
|
await async_start(hass, 'homeassistant', {}, entry)
|
||||||
|
data1 = (
|
||||||
|
'{ "name": "Beer",'
|
||||||
|
' "command_topic": "test_topic",'
|
||||||
|
' "json_attributes_topic": "attr-topic1" }'
|
||||||
|
)
|
||||||
|
data2 = (
|
||||||
|
'{ "name": "Beer",'
|
||||||
|
' "command_topic": "test_topic",'
|
||||||
|
' "json_attributes_topic": "attr-topic2" }'
|
||||||
|
)
|
||||||
|
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config',
|
||||||
|
data1)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "100" }')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get('climate.beer')
|
||||||
|
assert '100' == state.attributes.get('val')
|
||||||
|
|
||||||
|
# Change json_attributes_topic
|
||||||
|
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config',
|
||||||
|
data2)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Verify we are no longer subscribing to the old topic
|
||||||
|
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "50" }')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get('climate.beer')
|
||||||
|
assert '100' == state.attributes.get('val')
|
||||||
|
|
||||||
|
# Verify we are subscribing to the new topic
|
||||||
|
async_fire_mqtt_message(hass, 'attr-topic2', '{ "val": "75" }')
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
state = hass.states.get('climate.beer')
|
||||||
|
assert '75' == state.attributes.get('val')
|
||||||
|
|
||||||
|
|
||||||
async def test_unique_id(hass):
|
async def test_unique_id(hass):
|
||||||
"""Test unique id option only creates one climate per unique_id."""
|
"""Test unique id option only creates one climate per unique_id."""
|
||||||
await async_mock_mqtt_component(hass)
|
await async_mock_mqtt_component(hass)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue