-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Set state for MQTT entities to 'unavailable' when no connection to broker #36479
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
Set state for MQTT entities to 'unavailable' when no connection to broker #36479
Conversation
to MQTT broker
@MartinHjelmare I'm not sure about the polling, do you think it would be better to emit events when MQTT connection status is changed, and have the MQTT availability mixin subscribe to that event? @definitio can you please add tests + fix the tests which now fail? |
Can we use the paho |
@MartinHjelmare Yes, that's what I meant:
And here:
And subscribe to those events here: core/homeassistant/components/mqtt/__init__.py Line 1088 in e807274
|
We can use the dispatch helper to communicate to entities. |
Sure, so something like this. In MqttAvailability: def __init__(self, config: dict) -> None:
async_dispatcher_connect(
hass, MQTT_CONNECTED, async_mqtt_connected
)
def async_mqtt_connected(state):
self.async_write_ha_state() And in the connection callbacks: dispatcher_send(
hass, MQTT_CONNECTED, connected
) |
Will the paho callback be called in sync or async context? We only need coroutines if we need to await something inside. |
The paho callbacks are called from the paho client's worker thread's context. |
@definitio can you try to rewrite to remove the polling, something like #36479 (comment) |
I will try.
…On Sat, Jun 6, 2020, 09:16 Erik Montnemery ***@***.***> wrote:
@definitio <https://github.com/definitio> can you try to rewrite to
remove the polling, something like #36479 (comment)
<#36479 (comment)>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#36479 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AI4KKJ33PTNCFDDH3KYDAL3RVHGK5ANCNFSM4NTSYFCA>
.
|
Make sure to decorate async callbacks in home assistant with |
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.
Looks good!
@definitio this is great! I updated the PR description a bit to reflect the final implementation. |
@laca75tn The problem you're reporting should be understood and corrected. However, a merged PR is not the right place to report issues. Please create a new issue for your problem. Edit: The problem as you describe it should be fixed by #37420 which was merged in 0.112.0. Which version did you upgrade to where you saw this problem? |
Sorry, my first time submitting an issue. Thanks for your patience |
Proposed change
Make MQTT entities change state to 'unavailable' when Home Assistant isn't connected to the MQTT broker.
Type of change
Example entry for
configuration.yaml
:# Example configuration.yaml
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.The integration reached or maintains the following Integration Quality Scale: