More support for availability reporting on MQTT components (#11336)
* Abstract MQTT availability from individual components - Moved availability topic and payloads to MQTT base schema. - Updated components that already report availability: - Switch - Binary sensor - Cover * Add availability reporting to additional MQTT components - Light - JSON light - Template light - Lock - Fan - HVAC - Sensor - Vacuum - Alarm control panel * Annotate MQTT platform coroutines
This commit is contained in:
1 parent
541707c3e7
commit
f0bf7b0def
25 files changed
+530
-193
No files matched your search
@@ -7,7 +7,7 @@ from unittest.mock import patch
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.components.sensor as sensor
|
||||
from homeassistant.const import EVENT_STATE_CHANGED
|
||||
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import mock_mqtt_component, fire_mqtt_message
|
||||
@@ -185,6 +185,34 @@ class TestSensorMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(events))
|
||||
|
||||
def test_custom_availability_payload(self):
|
||||
"""Test availability by custom payload with defined topic."""
|
||||
self.assertTrue(setup_component(self.hass, sensor.DOMAIN, {
|
||||
sensor.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test-topic',
|
||||
'availability_topic': 'availability-topic',
|
||||
'payload_available': 'good',
|
||||
'payload_not_available': 'nogood'
|
||||
}
|
||||
}))
|
||||
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual(STATE_UNAVAILABLE, state.state)
|
||||
|
||||
fire_mqtt_message(self.hass, 'availability-topic', 'good')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertNotEqual(STATE_UNAVAILABLE, state.state)
|
||||
|
||||
fire_mqtt_message(self.hass, 'availability-topic', 'nogood')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual(STATE_UNAVAILABLE, state.state)
|
||||
|
||||
def _send_time_changed(self, now):
|
||||
"""Send a time changed event."""
|
||||
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
|
||||
Reference in new issue
Block a user