Allow setting the retain flag for mqtt switch.
Some devices can read the initial value on startup. If the retain flag is set they always receive the value as last set by home assistant.
This commit is contained in:
parent
3e60c4801c
commit
69e9d39690
2 changed files with 17 additions and 7 deletions
|
@ -17,6 +17,7 @@ DEFAULT_QOS = 0
|
|||
DEFAULT_PAYLOAD_ON = "ON"
|
||||
DEFAULT_PAYLOAD_OFF = "OFF"
|
||||
DEFAULT_OPTIMISTIC = False
|
||||
DEFAULT_RETAIN = False
|
||||
|
||||
DEPENDENCIES = ['mqtt']
|
||||
|
||||
|
@ -35,6 +36,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
config.get('state_topic'),
|
||||
config.get('command_topic'),
|
||||
config.get('qos', DEFAULT_QOS),
|
||||
config.get('retain', DEFAULT_RETAIN),
|
||||
config.get('payload_on', DEFAULT_PAYLOAD_ON),
|
||||
config.get('payload_off', DEFAULT_PAYLOAD_OFF),
|
||||
config.get('optimistic', DEFAULT_OPTIMISTIC),
|
||||
|
@ -44,7 +46,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
||||
class MqttSwitch(SwitchDevice):
|
||||
""" Represents a switch that can be toggled using MQTT. """
|
||||
def __init__(self, hass, name, state_topic, command_topic, qos,
|
||||
def __init__(self, hass, name, state_topic, command_topic, qos, retain,
|
||||
payload_on, payload_off, optimistic, state_format):
|
||||
self._state = False
|
||||
self._hass = hass
|
||||
|
@ -52,6 +54,7 @@ class MqttSwitch(SwitchDevice):
|
|||
self._state_topic = state_topic
|
||||
self._command_topic = command_topic
|
||||
self._qos = qos
|
||||
self._retain = retain
|
||||
self._payload_on = payload_on
|
||||
self._payload_off = payload_off
|
||||
self._optimistic = optimistic
|
||||
|
@ -93,7 +96,7 @@ class MqttSwitch(SwitchDevice):
|
|||
def turn_on(self, **kwargs):
|
||||
""" Turn the device on. """
|
||||
mqtt.publish(self.hass, self._command_topic, self._payload_on,
|
||||
self._qos)
|
||||
self._qos, self._retain)
|
||||
if self._optimistic:
|
||||
# optimistically assume that switch has changed state
|
||||
self._state = True
|
||||
|
@ -102,7 +105,7 @@ class MqttSwitch(SwitchDevice):
|
|||
def turn_off(self, **kwargs):
|
||||
""" Turn the device off. """
|
||||
mqtt.publish(self.hass, self._command_topic, self._payload_off,
|
||||
self._qos)
|
||||
self._qos, self._retain)
|
||||
if self._optimistic:
|
||||
# optimistically assume that switch has changed state
|
||||
self._state = False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue