Skip to content
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

More support for availability reporting on MQTT components #11336

Merged

Conversation

DanNixon
Copy link
Contributor

@DanNixon DanNixon commented Dec 27, 2017

Description:

Adds the ability to use alive and LWT messages to set the availability of the majority of MQTT devices and abstracts the logic for handling availability topics to a mixin MqttAvailability.

The following components now support availability:

  • Switch (previously supported)
  • Binary sensor (previously supported)
  • Cover (previously supported)
  • Light
  • JSON light
  • Template light
  • Lock
  • Fan
  • HVAC
  • Sensor
  • Vacuum
  • Alarm control panel

Breaking changes:

The default availability payloads for the MQTT Switch platform have changed from "ON" and "OFF" to "online" and "offline" (in order to match the majority of MQTT platforms that already supported availability reporting).

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4293

Example entry for configuration.yaml (if applicable):

binary_sensor:
  - platform: mqtt
    state_topic: "home-assistant/window/contact"
    availability_topic: "availability_topic"
    payload_available: "good"
    payload_not_available: "nogood"

The last three variables are the important ones, they can be added to the configuration of any of the components mentioned above.

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass


from homeassistant.setup import setup_component
from homeassistant.components import fan
from homeassistant.const import (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'homeassistant.const.STATE_ON' imported but unused
'homeassistant.const.STATE_OFF' imported but unused

@DanNixon DanNixon changed the title Mqtt availability all components More support for availability reporting on MQTT components Dec 27, 2017
@DanNixon DanNixon force-pushed the mqtt_availability_all_components branch from 72d92d1 to 1761cd7 Compare December 27, 2017 23:49
"""Init the MQTT Alarm Control Panel."""
super(MqttAlarm, self).__init__(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use python 3 style call:

super().__init__(...)

@@ -89,7 +99,7 @@ def message_received(topic, payload, qos):
self._state = payload
self.async_schedule_update_ha_state()

return mqtt.async_subscribe(
yield from mqtt.async_subscribe(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this a bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but it is required now as multiple topics can be subscribed (status and availability).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, for clarity I think we should add the @asyncio.coroutine annotation to all async_added_to_hass methods in the entity classes affected by this PR. I think that should have been done previously, so if you can do that it's 👍.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update the docstrings for these methods. Ie they don't return a coroutine object anymore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My last comment came out weird, of course the coroutine function will still return a coroutine object but it doesn't explicitly return something, so I think we can remove the last sentence in the docstring.

payload_not_available):
"""Initialize the MQTT binary sensor."""
self._availability_topic = availability_topic
self._available = True if availability_topic is None else False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self._available = availability_topic is None

- Moved availability topic and payloads to MQTT base schema.
- Updated components that already report availability:
  - Switch
  - Binary sensor
  - Cover
- Light
- JSON light
- Template light
- Lock
- Fan
- HVAC
- Sensor
- Vacuum
- Alarm control panel
@DanNixon DanNixon force-pushed the mqtt_availability_all_components branch from 1761cd7 to e22ff6c Compare December 28, 2017 09:33
@MartinHjelmare
Copy link
Member

Please don't squash after PR review has started. It makes it harder to follow changes.

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I think we can merge when tests pass.

@DanNixon
Copy link
Contributor Author

@MartinHjelmare Cool, I'm just getting the documentation ready now.

Also this changes the default values for availability payloads for the MQTT Switch platform, would that be enough to warrant this being a breaking change?

@MartinHjelmare
Copy link
Member

Great!

Yes, let's add the breaking change tag to be clear. Please add a paragraph in the description what has changed that is breaking.

@balloob balloob merged commit f0bf7b0 into home-assistant:dev Jan 2, 2018
balloob pushed a commit to home-assistant/home-assistant.io that referenced this pull request Jan 2, 2018
To accompany changes from:
home-assistant/core#11336

Updated platforms:
- Switch (previously supported)
- Binary sensor (previously supported)
- Cover (previously supported)
- Light
- JSON light
- Template light
- Sensor
- Alarm control panel
- Lock
- HVAC
- Fan
- Vacuum
@balloob balloob mentioned this pull request Jan 11, 2018
config.get(CONF_CODE),
config.get(CONF_AVAILABILITY_TOPIC),
config.get(CONF_PAYLOAD_AVAILABLE),
config.get(CONF_PAYLOAD_NOT_AVAILABLE))])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying it right now, my alarm is always showing as unavailable. I think you missed the .extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema). Can you confirm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is missing (added in #11829).

Not sure why the unit test didn't complain about this, and since it didn't why it is not working for you.

Copy link
Contributor

@Diaoul Diaoul Jan 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I saw the unit test, this is very surprising. I will test this further but it seems it sees the availability_topic in my config (as it shows the alarm as unavailable) but doesn't get the value from it (which is "online"). The message is properly retained, checked with mosquitto_sub.

Here is my config:

alarm_control_panel:
  platform: mqtt
  state_topic: home/alarm
  command_topic: home/alarm/set
  availability_topic: home/alarm/availability

Maybe the unit test somehow bypass some config validation? /cc @balloob

Copy link
Contributor

@Diaoul Diaoul Jan 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I've tested with this config:

alarm_control_panel:
  platform: mqtt
  state_topic: home/alarm
  command_topic: home/alarm/set
  availability_topic: home/alarm/availability
  payload_available: online
  payload_not_available: offline

And it works perfectly.

My guess is that without the schema extension, the component isn't able to get default values for the config variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. That makes sense. Without the schema the values for the payloads will both be None.

For the sake of completeness I'll add some extra tests for the default payloads in #11829.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your quick response and fixes!

@DanNixon DanNixon deleted the mqtt_availability_all_components branch January 20, 2018 15:02
@OttoWinter
Copy link
Member

@DanNixon Please correct me if I'm wrong, but I believe you forgot to delete the availability topic subscription in the cover component. See https://github.com/home-assistant/home-assistant/blob/master/homeassistant/components/cover/mqtt.py#L218 and line 235.

@DanNixon
Copy link
Contributor Author

@OttoWinter Yes, it looks that way. Thanks for spotting.

I'm a little busy for the time being but I can have a look and correct it in a couple of days.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants