mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Force update support for mqtt binary sensor (#12092)
This commit is contained in:
parent
6265d1b747
commit
25cbc8317f
@ -14,8 +14,8 @@ import homeassistant.components.mqtt as mqtt
|
|||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorDevice, DEVICE_CLASSES_SCHEMA)
|
BinarySensorDevice, DEVICE_CLASSES_SCHEMA)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_NAME, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_ON, CONF_PAYLOAD_OFF,
|
CONF_FORCE_UPDATE, CONF_NAME, CONF_VALUE_TEMPLATE, CONF_PAYLOAD_ON,
|
||||||
CONF_DEVICE_CLASS)
|
CONF_PAYLOAD_OFF, CONF_DEVICE_CLASS)
|
||||||
from homeassistant.components.mqtt import (
|
from homeassistant.components.mqtt import (
|
||||||
CONF_STATE_TOPIC, CONF_AVAILABILITY_TOPIC, CONF_PAYLOAD_AVAILABLE,
|
CONF_STATE_TOPIC, CONF_AVAILABILITY_TOPIC, CONF_PAYLOAD_AVAILABLE,
|
||||||
CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, MqttAvailability)
|
CONF_PAYLOAD_NOT_AVAILABLE, CONF_QOS, MqttAvailability)
|
||||||
@ -24,8 +24,10 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = 'MQTT Binary sensor'
|
DEFAULT_NAME = 'MQTT Binary sensor'
|
||||||
|
|
||||||
DEFAULT_PAYLOAD_OFF = 'OFF'
|
DEFAULT_PAYLOAD_OFF = 'OFF'
|
||||||
DEFAULT_PAYLOAD_ON = 'ON'
|
DEFAULT_PAYLOAD_ON = 'ON'
|
||||||
|
DEFAULT_FORCE_UPDATE = False
|
||||||
|
|
||||||
DEPENDENCIES = ['mqtt']
|
DEPENDENCIES = ['mqtt']
|
||||||
|
|
||||||
@ -34,6 +36,7 @@ PLATFORM_SCHEMA = mqtt.MQTT_RO_PLATFORM_SCHEMA.extend({
|
|||||||
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
|
vol.Optional(CONF_PAYLOAD_OFF, default=DEFAULT_PAYLOAD_OFF): cv.string,
|
||||||
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
|
vol.Optional(CONF_PAYLOAD_ON, default=DEFAULT_PAYLOAD_ON): cv.string,
|
||||||
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
||||||
|
vol.Optional(CONF_FORCE_UPDATE, default=DEFAULT_FORCE_UPDATE): cv.boolean,
|
||||||
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
|
}).extend(mqtt.MQTT_AVAILABILITY_SCHEMA.schema)
|
||||||
|
|
||||||
|
|
||||||
@ -53,6 +56,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
config.get(CONF_AVAILABILITY_TOPIC),
|
config.get(CONF_AVAILABILITY_TOPIC),
|
||||||
config.get(CONF_DEVICE_CLASS),
|
config.get(CONF_DEVICE_CLASS),
|
||||||
config.get(CONF_QOS),
|
config.get(CONF_QOS),
|
||||||
|
config.get(CONF_FORCE_UPDATE),
|
||||||
config.get(CONF_PAYLOAD_ON),
|
config.get(CONF_PAYLOAD_ON),
|
||||||
config.get(CONF_PAYLOAD_OFF),
|
config.get(CONF_PAYLOAD_OFF),
|
||||||
config.get(CONF_PAYLOAD_AVAILABLE),
|
config.get(CONF_PAYLOAD_AVAILABLE),
|
||||||
@ -65,7 +69,7 @@ class MqttBinarySensor(MqttAvailability, BinarySensorDevice):
|
|||||||
"""Representation a binary sensor that is updated by MQTT."""
|
"""Representation a binary sensor that is updated by MQTT."""
|
||||||
|
|
||||||
def __init__(self, name, state_topic, availability_topic, device_class,
|
def __init__(self, name, state_topic, availability_topic, device_class,
|
||||||
qos, payload_on, payload_off, payload_available,
|
qos, force_update, payload_on, payload_off, payload_available,
|
||||||
payload_not_available, value_template):
|
payload_not_available, value_template):
|
||||||
"""Initialize the MQTT binary sensor."""
|
"""Initialize the MQTT binary sensor."""
|
||||||
super().__init__(availability_topic, qos, payload_available,
|
super().__init__(availability_topic, qos, payload_available,
|
||||||
@ -77,6 +81,7 @@ class MqttBinarySensor(MqttAvailability, BinarySensorDevice):
|
|||||||
self._payload_on = payload_on
|
self._payload_on = payload_on
|
||||||
self._payload_off = payload_off
|
self._payload_off = payload_off
|
||||||
self._qos = qos
|
self._qos = qos
|
||||||
|
self._force_update = force_update
|
||||||
self._template = value_template
|
self._template = value_template
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@ -124,3 +129,8 @@ class MqttBinarySensor(MqttAvailability, BinarySensorDevice):
|
|||||||
def device_class(self):
|
def device_class(self):
|
||||||
"""Return the class of this sensor."""
|
"""Return the class of this sensor."""
|
||||||
return self._device_class
|
return self._device_class
|
||||||
|
|
||||||
|
@property
|
||||||
|
def force_update(self):
|
||||||
|
"""Force update."""
|
||||||
|
return self._force_update
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
"""The tests for the MQTT binary sensor platform."""
|
"""The tests for the MQTT binary sensor platform."""
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import homeassistant.core as ha
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import setup_component
|
||||||
import homeassistant.components.binary_sensor as binary_sensor
|
import homeassistant.components.binary_sensor as binary_sensor
|
||||||
from homeassistant.const import (STATE_OFF, STATE_ON,
|
|
||||||
STATE_UNAVAILABLE)
|
|
||||||
|
|
||||||
from tests.common import (
|
from homeassistant.const import STATE_OFF, STATE_ON
|
||||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
|
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
from tests.common import get_test_home_assistant, fire_mqtt_message
|
||||||
|
from tests.common import mock_component, mock_mqtt_component
|
||||||
|
|
||||||
|
|
||||||
class TestSensorMQTT(unittest.TestCase):
|
class TestSensorMQTT(unittest.TestCase):
|
||||||
@ -141,3 +143,64 @@ class TestSensorMQTT(unittest.TestCase):
|
|||||||
|
|
||||||
state = self.hass.states.get('binary_sensor.test')
|
state = self.hass.states.get('binary_sensor.test')
|
||||||
self.assertEqual(STATE_UNAVAILABLE, state.state)
|
self.assertEqual(STATE_UNAVAILABLE, state.state)
|
||||||
|
|
||||||
|
def test_force_update_disabled(self):
|
||||||
|
"""Test force update option."""
|
||||||
|
mock_component(self.hass, 'mqtt')
|
||||||
|
assert setup_component(self.hass, binary_sensor.DOMAIN, {
|
||||||
|
binary_sensor.DOMAIN: {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'state_topic': 'test-topic',
|
||||||
|
'payload_on': 'ON',
|
||||||
|
'payload_off': 'OFF'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
events = []
|
||||||
|
|
||||||
|
@ha.callback
|
||||||
|
def callback(event):
|
||||||
|
"""Verify event got called."""
|
||||||
|
events.append(event)
|
||||||
|
|
||||||
|
self.hass.bus.listen(EVENT_STATE_CHANGED, callback)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
self.assertEqual(1, len(events))
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
self.assertEqual(1, len(events))
|
||||||
|
|
||||||
|
def test_force_update_enabled(self):
|
||||||
|
"""Test force update option."""
|
||||||
|
mock_component(self.hass, 'mqtt')
|
||||||
|
assert setup_component(self.hass, binary_sensor.DOMAIN, {
|
||||||
|
binary_sensor.DOMAIN: {
|
||||||
|
'platform': 'mqtt',
|
||||||
|
'name': 'test',
|
||||||
|
'state_topic': 'test-topic',
|
||||||
|
'payload_on': 'ON',
|
||||||
|
'payload_off': 'OFF',
|
||||||
|
'force_update': True
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
events = []
|
||||||
|
|
||||||
|
@ha.callback
|
||||||
|
def callback(event):
|
||||||
|
"""Verify event got called."""
|
||||||
|
events.append(event)
|
||||||
|
|
||||||
|
self.hass.bus.listen(EVENT_STATE_CHANGED, callback)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
self.assertEqual(1, len(events))
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
self.assertEqual(2, len(events))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user