Skip to content

Commit

Permalink
Publish birth and will messages by default (#37371)
Browse files Browse the repository at this point in the history
* Publish birth and will messages by default

* Remove useless copy
  • Loading branch information
emontnemery committed Jul 4, 2020
1 parent 4b3ad0a commit b636550
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
42 changes: 33 additions & 9 deletions homeassistant/components/mqtt/__init__.py
Expand Up @@ -110,7 +110,7 @@
DEFAULT_PORT = 1883
DEFAULT_KEEPALIVE = 60
DEFAULT_PROTOCOL = PROTOCOL_311
DEFAULT_DISCOVERY_PREFIX = "homeassistant"
DEFAULT_PREFIX = "homeassistant"
DEFAULT_TLS_PROTOCOL = "auto"
DEFAULT_PAYLOAD_AVAILABLE = "online"
DEFAULT_PAYLOAD_NOT_AVAILABLE = "offline"
Expand Down Expand Up @@ -139,10 +139,24 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType
"the MQTT broker configuration"
)

DEFAULT_BIRTH = {
ATTR_TOPIC: DEFAULT_PREFIX + "/status",
CONF_PAYLOAD: DEFAULT_PAYLOAD_AVAILABLE,
ATTR_QOS: DEFAULT_QOS,
ATTR_RETAIN: DEFAULT_RETAIN,
}

DEFAULT_WILL = {
ATTR_TOPIC: DEFAULT_PREFIX + "/status",
CONF_PAYLOAD: DEFAULT_PAYLOAD_NOT_AVAILABLE,
ATTR_QOS: DEFAULT_QOS,
ATTR_RETAIN: DEFAULT_RETAIN,
}

MQTT_WILL_BIRTH_SCHEMA = vol.Schema(
{
vol.Required(ATTR_TOPIC): valid_publish_topic,
vol.Required(ATTR_PAYLOAD, CONF_PAYLOAD): cv.string,
vol.Inclusive(ATTR_TOPIC, "topic_payload"): valid_publish_topic,
vol.Inclusive(ATTR_PAYLOAD, "topic_payload"): cv.string,
vol.Optional(ATTR_QOS, default=DEFAULT_QOS): _VALID_QOS_SCHEMA,
vol.Optional(ATTR_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
},
Expand Down Expand Up @@ -188,13 +202,17 @@ def embedded_broker_deprecated(value):
vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTOCOL): vol.All(
cv.string, vol.In([PROTOCOL_31, PROTOCOL_311])
),
vol.Optional(CONF_WILL_MESSAGE): MQTT_WILL_BIRTH_SCHEMA,
vol.Optional(CONF_BIRTH_MESSAGE): MQTT_WILL_BIRTH_SCHEMA,
vol.Optional(
CONF_WILL_MESSAGE, default=DEFAULT_WILL
): MQTT_WILL_BIRTH_SCHEMA,
vol.Optional(
CONF_BIRTH_MESSAGE, default=DEFAULT_BIRTH
): MQTT_WILL_BIRTH_SCHEMA,
vol.Optional(CONF_DISCOVERY, default=DEFAULT_DISCOVERY): cv.boolean,
# discovery_prefix must be a valid publish topic because if no
# state topic is specified, it will be created with the given prefix.
vol.Optional(
CONF_DISCOVERY_PREFIX, default=DEFAULT_DISCOVERY_PREFIX
CONF_DISCOVERY_PREFIX, default=DEFAULT_PREFIX
): valid_publish_topic,
}
),
Expand Down Expand Up @@ -698,7 +716,10 @@ def init_client(self):
self._mqttc.on_disconnect = self._mqtt_on_disconnect
self._mqttc.on_message = self._mqtt_on_message

if CONF_WILL_MESSAGE in self.conf:
if (
CONF_WILL_MESSAGE in self.conf
and ATTR_TOPIC in self.conf[CONF_WILL_MESSAGE]
):
will_message = Message(**self.conf[CONF_WILL_MESSAGE])
else:
will_message = None
Expand Down Expand Up @@ -749,7 +770,7 @@ async def async_disconnect(self):

def stop():
"""Stop the MQTT client."""
self._mqttc.disconnect()
# Do not disconnect, we want the broker to always publish will
self._mqttc.loop_stop()

await self.hass.async_add_executor_job(stop)
Expand Down Expand Up @@ -848,7 +869,10 @@ def _mqtt_on_connect(self, _mqttc, _userdata, _flags, result_code: int) -> None:
max_qos = max(subscription.qos for subscription in subs)
self.hass.add_job(self._async_perform_subscription, topic, max_qos)

if CONF_BIRTH_MESSAGE in self.conf:
if (
CONF_BIRTH_MESSAGE in self.conf
and ATTR_TOPIC in self.conf[CONF_BIRTH_MESSAGE]
):
birth_message = Message(**self.conf[CONF_BIRTH_MESSAGE])
self.hass.add_job(
self.async_publish( # pylint: disable=no-value-for-parameter
Expand Down
58 changes: 57 additions & 1 deletion tests/components/mqtt/test_init.py
Expand Up @@ -755,7 +755,7 @@ def mock_tls_set(certificate, certfile=None, keyfile=None, tls_version=None):
}
],
)
async def test_birth_message(hass, mqtt_client_mock, mqtt_mock):
async def test_custom_birth_message(hass, mqtt_client_mock, mqtt_mock):
"""Test sending birth message."""
calls = []
mqtt_client_mock.publish.side_effect = lambda *args: calls.append(args)
Expand All @@ -764,6 +764,62 @@ async def test_birth_message(hass, mqtt_client_mock, mqtt_mock):
assert calls[-1] == ("birth", "birth", 0, False)


async def test_default_birth_message(hass, mqtt_client_mock, mqtt_mock):
"""Test sending birth message."""
calls = []
mqtt_client_mock.publish.side_effect = lambda *args: calls.append(args)
mqtt_mock._mqtt_on_connect(None, None, 0, 0)
await hass.async_block_till_done()
assert calls[-1] == ("homeassistant/status", "online", 0, False)


@pytest.mark.parametrize(
"mqtt_config", [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_BIRTH_MESSAGE: {}}],
)
async def test_no_birth_message(hass, mqtt_client_mock, mqtt_mock):
"""Test sending birth message."""
calls = []
mqtt_client_mock.publish.side_effect = lambda *args: calls.append(args)
mqtt_mock._mqtt_on_connect(None, None, 0, 0)
await hass.async_block_till_done()
assert not calls


@pytest.mark.parametrize(
"mqtt_config",
[
{
mqtt.CONF_BROKER: "mock-broker",
mqtt.CONF_WILL_MESSAGE: {
mqtt.ATTR_TOPIC: "death",
mqtt.ATTR_PAYLOAD: "death",
},
}
],
)
async def test_custom_will_message(hass, mqtt_client_mock, mqtt_mock):
"""Test will message."""
mqtt_client_mock.will_set.assert_called_with("death", "death", 0, False, None)


async def test_default_will_message(hass, mqtt_client_mock, mqtt_mock):
"""Test will message."""
mqtt_client_mock.will_set.assert_called_with(
"homeassistant/status", "offline", 0, False, None
)


@pytest.mark.parametrize(
"mqtt_config", [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_WILL_MESSAGE: {}}],
)
async def test_no_will_message(hass, mqtt_client_mock, mqtt_mock):
"""Test will message."""
mqtt_client_mock.will_set.assert_not_called()


@pytest.mark.parametrize(
"mqtt_config", [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_BIRTH_MESSAGE: {}}],
)
async def test_mqtt_subscribes_topics_on_connect(hass, mqtt_client_mock, mqtt_mock):
"""Test subscription to topic on connect."""
await mqtt.async_subscribe(hass, "topic/test", None)
Expand Down

0 comments on commit b636550

Please sign in to comment.