Fix discovery update of MQTT state templates (#39901)
This commit is contained in:
parent
fd8a4182d9
commit
4d6e694d14
16 changed files with 853 additions and 128 deletions
|
@ -174,7 +174,7 @@ async def async_setup_platform(
|
|||
):
|
||||
"""Set up MQTT cover through configuration.yaml."""
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
await _async_setup_entity(config, async_add_entities)
|
||||
await _async_setup_entity(hass, config, async_add_entities)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
|
@ -186,7 +186,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
try:
|
||||
config = PLATFORM_SCHEMA(discovery_payload)
|
||||
await _async_setup_entity(
|
||||
config, async_add_entities, config_entry, discovery_data
|
||||
hass, config, async_add_entities, config_entry, discovery_data
|
||||
)
|
||||
except Exception:
|
||||
clear_discovery_hash(hass, discovery_data[ATTR_DISCOVERY_HASH])
|
||||
|
@ -198,10 +198,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
|
||||
|
||||
async def _async_setup_entity(
|
||||
config, async_add_entities, config_entry=None, discovery_data=None
|
||||
hass, config, async_add_entities, config_entry=None, discovery_data=None
|
||||
):
|
||||
"""Set up the MQTT Cover."""
|
||||
async_add_entities([MqttCover(config, config_entry, discovery_data)])
|
||||
async_add_entities([MqttCover(hass, config, config_entry, discovery_data)])
|
||||
|
||||
|
||||
class MqttCover(
|
||||
|
@ -213,8 +213,9 @@ class MqttCover(
|
|||
):
|
||||
"""Representation of a cover that can be controlled using MQTT."""
|
||||
|
||||
def __init__(self, config, config_entry, discovery_data):
|
||||
def __init__(self, hass, config, config_entry, discovery_data):
|
||||
"""Initialize the cover."""
|
||||
self.hass = hass
|
||||
self._unique_id = config.get(CONF_UNIQUE_ID)
|
||||
self._position = None
|
||||
self._state = None
|
||||
|
@ -257,8 +258,6 @@ class MqttCover(
|
|||
)
|
||||
self._tilt_optimistic = config[CONF_TILT_STATE_OPTIMISTIC]
|
||||
|
||||
async def _subscribe_topics(self):
|
||||
"""(Re)Subscribe to topics."""
|
||||
template = self._config.get(CONF_VALUE_TEMPLATE)
|
||||
if template is not None:
|
||||
template.hass = self.hass
|
||||
|
@ -269,6 +268,8 @@ class MqttCover(
|
|||
if tilt_status_template is not None:
|
||||
tilt_status_template.hass = self.hass
|
||||
|
||||
async def _subscribe_topics(self):
|
||||
"""(Re)Subscribe to topics."""
|
||||
topics = {}
|
||||
|
||||
@callback
|
||||
|
@ -276,6 +277,7 @@ class MqttCover(
|
|||
def tilt_message_received(msg):
|
||||
"""Handle tilt updates."""
|
||||
payload = msg.payload
|
||||
tilt_status_template = self._config.get(CONF_TILT_STATUS_TEMPLATE)
|
||||
if tilt_status_template is not None:
|
||||
payload = tilt_status_template.async_render_with_possible_json_value(
|
||||
payload
|
||||
|
@ -296,6 +298,7 @@ class MqttCover(
|
|||
def state_message_received(msg):
|
||||
"""Handle new MQTT state messages."""
|
||||
payload = msg.payload
|
||||
template = self._config.get(CONF_VALUE_TEMPLATE)
|
||||
if template is not None:
|
||||
payload = template.async_render_with_possible_json_value(payload)
|
||||
|
||||
|
@ -321,6 +324,7 @@ class MqttCover(
|
|||
def position_message_received(msg):
|
||||
"""Handle new MQTT state messages."""
|
||||
payload = msg.payload
|
||||
template = self._config.get(CONF_VALUE_TEMPLATE)
|
||||
if template is not None:
|
||||
payload = template.async_render_with_possible_json_value(payload)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue