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
f3bd13d1
Unverified
Commit
f3bd13d1
authored
3 years ago
by
Erik Montnemery
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix regression in MQTT discovery (#58684)
* Fix regression in MQTT discovery * Update test
parent
72d7817d
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/discovery.py
+4
-1
4 additions, 1 deletion
homeassistant/components/mqtt/discovery.py
tests/components/mqtt/test_discovery.py
+70
-0
70 additions, 0 deletions
tests/components/mqtt/test_discovery.py
with
74 additions
and
1 deletion
homeassistant/components/mqtt/discovery.py
+
4
−
1
View file @
f3bd13d1
...
...
@@ -10,6 +10,7 @@ import time
from
homeassistant.const
import
CONF_DEVICE
,
CONF_PLATFORM
from
homeassistant.core
import
HomeAssistant
from
homeassistant.data_entry_flow
import
RESULT_TYPE_ABORT
import
homeassistant.helpers.config_validation
as
cv
from
homeassistant.helpers.dispatcher
import
(
async_dispatcher_connect
,
async_dispatcher_send
,
...
...
@@ -141,7 +142,9 @@ async def async_start( # noqa: C901
if
value
[
-
1
]
==
TOPIC_BASE
and
key
.
endswith
(
"
topic
"
):
payload
[
key
]
=
f
"
{
value
[
:
-
1
]
}{
base
}
"
if
payload
.
get
(
CONF_AVAILABILITY
):
for
availability_conf
in
payload
[
CONF_AVAILABILITY
]:
for
availability_conf
in
cv
.
ensure_list
(
payload
[
CONF_AVAILABILITY
]):
if
not
isinstance
(
availability_conf
,
dict
):
continue
if
topic
:
=
availability_conf
.
get
(
CONF_TOPIC
):
if
topic
[
0
]
==
TOPIC_BASE
:
availability_conf
[
CONF_TOPIC
]
=
f
"
{
base
}{
topic
[
1
:
]
}
"
...
...
This diff is collapsed.
Click to expand it.
tests/components/mqtt/test_discovery.py
+
70
−
0
View file @
f3bd13d1
...
...
@@ -504,6 +504,76 @@ async def test_discovery_expansion(hass, mqtt_mock, caplog):
assert
state
.
state
==
STATE_UNAVAILABLE
async
def
test_discovery_expansion_2
(
hass
,
mqtt_mock
,
caplog
):
"""
Test expansion of abbreviated discovery payload.
"""
data
=
(
'
{
"
~
"
:
"
some/base/topic
"
,
'
'
"
name
"
:
"
DiscoveryExpansionTest1
"
,
'
'
"
stat_t
"
:
"
test_topic/~
"
,
'
'
"
cmd_t
"
:
"
~/test_topic
"
,
'
'
"
availability
"
: {
'
'
"
topic
"
:
"
~/avail_item1
"
,
'
'
"
payload_available
"
:
"
available
"
,
'
'
"
payload_not_available
"
:
"
not_available
"'
"
},
"
'
"
dev
"
:{
'
'
"
ids
"
:[
"
5706DF
"
],
'
'
"
name
"
:
"
DiscoveryExpansionTest1 Device
"
,
'
'
"
mdl
"
:
"
Generic
"
,
'
'
"
sw
"
:
"
1.2.3.4
"
,
'
'
"
mf
"
:
"
None
"
,
'
'
"
sa
"
:
"
default_area
"'
"
}
"
"
}
"
)
async_fire_mqtt_message
(
hass
,
"
homeassistant/switch/bla/config
"
,
data
)
await
hass
.
async_block_till_done
()
state
=
hass
.
states
.
get
(
"
switch.DiscoveryExpansionTest1
"
)
assert
state
.
state
==
STATE_UNAVAILABLE
async_fire_mqtt_message
(
hass
,
"
some/base/topic/avail_item1
"
,
"
available
"
)
await
hass
.
async_block_till_done
()
state
=
hass
.
states
.
get
(
"
switch.DiscoveryExpansionTest1
"
)
assert
state
is
not
None
assert
state
.
name
==
"
DiscoveryExpansionTest1
"
assert
(
"
switch
"
,
"
bla
"
)
in
hass
.
data
[
ALREADY_DISCOVERED
]
assert
state
.
state
==
STATE_OFF
@pytest.mark.no_fail_on_log_exception
async
def
test_discovery_expansion_3
(
hass
,
mqtt_mock
,
caplog
):
"""
Test expansion of broken discovery payload.
"""
data
=
(
'
{
"
~
"
:
"
some/base/topic
"
,
'
'
"
name
"
:
"
DiscoveryExpansionTest1
"
,
'
'
"
stat_t
"
:
"
test_topic/~
"
,
'
'
"
cmd_t
"
:
"
~/test_topic
"
,
'
'
"
availability
"
:
"
incorrect
"
,
'
'
"
dev
"
:{
'
'
"
ids
"
:[
"
5706DF
"
],
'
'
"
name
"
:
"
DiscoveryExpansionTest1 Device
"
,
'
'
"
mdl
"
:
"
Generic
"
,
'
'
"
sw
"
:
"
1.2.3.4
"
,
'
'
"
mf
"
:
"
None
"
,
'
'
"
sa
"
:
"
default_area
"'
"
}
"
"
}
"
)
async_fire_mqtt_message
(
hass
,
"
homeassistant/switch/bla/config
"
,
data
)
await
hass
.
async_block_till_done
()
assert
hass
.
states
.
get
(
"
switch.DiscoveryExpansionTest1
"
)
is
None
# Make sure the malformed availability data does not trip up discovery by asserting
# there are schema valdiation errors in the log
assert
(
"
voluptuous.error.MultipleInvalid: expected a dictionary @ data[
'
availability
'
][0]
"
in
caplog
.
text
)
ABBREVIATIONS_WHITE_LIST
=
[
# MQTT client/server/trigger settings
"
CONF_BIRTH_MESSAGE
"
,
...
...
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