-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reconfigure MQTT binary_sensor component if discovery info is changed #18169
Changes from 7 commits
6da9fcf
8a1e3c5
46b2a6f
ac19a0b
0542b2d
2b24f7c
ac1f738
b982832
76b0c6a
af03a4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Helper to handle a set of topics to subscribe to. | ||
|
||
For more details about this component, please refer to the documentation at | ||
https://home-assistant.io/components/mqtt/ | ||
""" | ||
import logging | ||
|
||
from homeassistant.components import mqtt | ||
from homeassistant.components.mqtt import DEFAULT_QOS, MessageCallbackType | ||
from homeassistant.loader import bind_hass | ||
from homeassistant.helpers.typing import ( | ||
HomeAssistantType) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@bind_hass | ||
async def async_subscribe_topics(hass: HomeAssistantType, sub_state: dict, | ||
balloob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new_topics: dict, | ||
msg_callback: MessageCallbackType, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not have a generic msg callback for all topics. For example, this won't work for the MQTT light, as it has a different callback for brightness and state. I would expect {
'/home/light/kitchen/state': {
'qos': 2,
'encoding': 'utf-8',
'msg_callback': state_received
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
qos: int = DEFAULT_QOS, | ||
encoding: str = 'utf-8'): | ||
"""(Re)Subscribe to a set of MQTT topics. | ||
|
||
State is kept in sub_state. | ||
""" | ||
if sub_state is None: | ||
sub_state = {'topics': {}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason to wrap it in a dictionary. It's an opaque object to the outside world, inside this method we always know the implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, fixed. |
||
old_topics = sub_state['topics'] | ||
for key, sub in list(old_topics.items()): | ||
emontnemery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
topic = sub[0] | ||
unsub = sub[1] | ||
if key not in new_topics or topic != new_topics[key]: | ||
if unsub is not None: | ||
unsub() | ||
del old_topics[key] | ||
for key, topic in new_topics.items(): | ||
if key not in old_topics and topic is not None: | ||
unsub = await mqtt.async_subscribe(hass, topic, msg_callback, qos) | ||
old_topics[key] = (topic, unsub) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's kinda confusing that we have an object called We should rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, rewritten as you suggest. |
||
|
||
return sub_state | ||
|
||
|
||
@bind_hass | ||
async def async_unsubscribe_topics(hass: HomeAssistantType, sub_state: dict): | ||
"""Unsubscribe from all MQTT topics managed by async_subscribe_topics.""" | ||
await async_subscribe_topics(hass, sub_state, {}, None) | ||
|
||
return sub_state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OttoWinter What do you suggest to do if device info is changed, just update the MqttEntityDeviceInfo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not the same as
availability_discovery_update
, just calleddevice_info_discovery_update
- this method would set the internalself._device_info
object to the latest values.(It might be the case though that the
device_info
is only read when the device is initially added to hass, then I guess we ignore the update to this attribute, as it's probably not used too often)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this seems to be the cased based on some quick testing.
Either just ignore this case as you suggest, or delete + re-add the component.