Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
HomeAssistant
Core
Commits
b6365505
Unverified
Commit
b6365505
authored
4 years ago
by
Erik Montnemery
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Publish birth and will messages by default (#37371)
* Publish birth and will messages by default * Remove useless copy
parent
4b3ad0a1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
homeassistant/components/mqtt/__init__.py
+33
-9
33 additions, 9 deletions
homeassistant/components/mqtt/__init__.py
tests/components/mqtt/test_init.py
+57
-1
57 additions, 1 deletion
tests/components/mqtt/test_init.py
with
90 additions
and
10 deletions
homeassistant/components/mqtt/__init__.py
+
33
−
9
View file @
b6365505
...
...
@@ -110,7 +110,7 @@ PROTOCOL_31 = "3.1"
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
"
...
...
@@ -139,10 +139,24 @@ CLIENT_KEY_AUTH_MSG = (
"
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
,
},
...
...
@@ -188,13 +202,17 @@ CONFIG_SCHEMA = vol.Schema(
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
,
}
),
...
...
@@ -698,7 +716,10 @@ class MQTT:
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
...
...
@@ -749,7 +770,7 @@ class MQTT:
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
)
...
...
@@ -848,7 +869,10 @@ class MQTT:
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
...
...
This diff is collapsed.
Click to expand it.
tests/components/mqtt/test_init.py
+
57
−
1
View file @
b6365505
...
...
@@ -755,7 +755,7 @@ async def test_setup_without_tls_config_uses_tlsv1_under_python36(hass):
}
],
)
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
)
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment