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
853fab0a
Unverified
Commit
853fab0a
authored
2 years ago
by
Erik Montnemery
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Mock MQTT setup in hassio tests (#77245)
* Mock MQTT setup in hassio tests * Tweak
parent
c26d6879
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/components/hassio/test_discovery.py
+71
-56
71 additions, 56 deletions
tests/components/hassio/test_discovery.py
with
71 additions
and
56 deletions
tests/components/hassio/test_discovery.py
+
71
−
56
View file @
853fab0a
"""
Test config flow.
"""
from
http
import
HTTPStatus
from
unittest.mock
import
Mock
,
patch
from
unittest.mock
import
AsyncMock
,
Mock
,
patch
import
pytest
from
homeassistant
import
config_entries
from
homeassistant.components.hassio
import
HassioServiceInfo
from
homeassistant.components.hassio.handler
import
HassioAPIError
from
homeassistant.components.mqtt
import
DOMAIN
as
MQTT_DOMAIN
from
homeassistant.const
import
EVENT_HOMEASSISTANT_START
,
EVENT_HOMEASSISTANT_STARTED
from
homeassistant.setup
import
async_setup_component
from
tests.common
import
MockModule
,
mock_entity_platform
,
mock_integration
@pytest.fixture
async
def
mock_mqtt
(
hass
):
"""
Mock the MQTT integration
'
s config flow.
"""
mock_integration
(
hass
,
MockModule
(
MQTT_DOMAIN
))
mock_entity_platform
(
hass
,
f
"
config_flow.
{
MQTT_DOMAIN
}
"
,
None
)
with
patch
.
dict
(
config_entries
.
HANDLERS
):
class
MqttFlow
(
config_entries
.
ConfigFlow
,
domain
=
MQTT_DOMAIN
):
"""
Test flow.
"""
VERSION
=
1
async
def
test_hassio_discovery_startup
(
hass
,
aioclient_mock
,
hassio_client
):
async_step_hassio
=
AsyncMock
(
return_value
=
{
"
type
"
:
"
abort
"
})
yield
MqttFlow
async
def
test_hassio_discovery_startup
(
hass
,
aioclient_mock
,
hassio_client
,
mock_mqtt
):
"""
Test startup and discovery after event.
"""
aioclient_mock
.
get
(
"
http://127.0.0.1/discovery
"
,
...
...
@@ -39,31 +63,29 @@ async def test_hassio_discovery_startup(hass, aioclient_mock, hassio_client):
assert
aioclient_mock
.
call_count
==
0
with
patch
(
"
homeassistant.components.mqtt.config_flow.FlowHandler.async_step_hassio
"
,
return_value
=
{
"
type
"
:
"
abort
"
},
)
as
mock_mqtt
:
hass
.
bus
.
async_fire
(
EVENT_HOMEASSISTANT_START
)
await
hass
.
async_block_till_done
()
hass
.
bus
.
async_fire
(
EVENT_HOMEASSISTANT_STARTED
)
await
hass
.
async_block_till_done
()
assert
aioclient_mock
.
call_count
==
2
assert
mock_mqtt
.
called
mock_mqtt
.
assert_called_with
(
HassioServiceInfo
(
config
=
{
"
broker
"
:
"
mock-broker
"
,
"
port
"
:
1883
,
"
username
"
:
"
mock-user
"
,
"
password
"
:
"
mock-pass
"
,
"
protocol
"
:
"
3.1.1
"
,
"
addon
"
:
"
Mosquitto Test
"
,
}
)
hass
.
bus
.
async_fire
(
EVENT_HOMEASSISTANT_START
)
await
hass
.
async_block_till_done
()
hass
.
bus
.
async_fire
(
EVENT_HOMEASSISTANT_STARTED
)
await
hass
.
async_block_till_done
()
assert
aioclient_mock
.
call_count
==
2
assert
mock_mqtt
.
async_step_hassio
.
called
mock_mqtt
.
async_step_hassio
.
assert_called_with
(
HassioServiceInfo
(
config
=
{
"
broker
"
:
"
mock-broker
"
,
"
port
"
:
1883
,
"
username
"
:
"
mock-user
"
,
"
password
"
:
"
mock-pass
"
,
"
protocol
"
:
"
3.1.1
"
,
"
addon
"
:
"
Mosquitto Test
"
,
}
)
)
async
def
test_hassio_discovery_startup_done
(
hass
,
aioclient_mock
,
hassio_client
):
async
def
test_hassio_discovery_startup_done
(
hass
,
aioclient_mock
,
hassio_client
,
mock_mqtt
):
"""
Test startup and discovery with hass discovery.
"""
aioclient_mock
.
post
(
"
http://127.0.0.1/supervisor/options
"
,
...
...
@@ -102,17 +124,14 @@ async def test_hassio_discovery_startup_done(hass, aioclient_mock, hassio_client
),
patch
(
"
homeassistant.components.hassio.HassIO.get_info
"
,
Mock
(
side_effect
=
HassioAPIError
()),
),
patch
(
"
homeassistant.components.mqtt.config_flow.FlowHandler.async_step_hassio
"
,
return_value
=
{
"
type
"
:
"
abort
"
},
)
as
mock_mqtt
:
):
await
hass
.
async_start
()
await
async_setup_component
(
hass
,
"
hassio
"
,
{})
await
hass
.
async_block_till_done
()
assert
aioclient_mock
.
call_count
==
2
assert
mock_mqtt
.
called
mock_mqtt
.
assert_called_with
(
assert
mock_mqtt
.
async_step_hassio
.
called
mock_mqtt
.
async_step_hassio
.
assert_called_with
(
HassioServiceInfo
(
config
=
{
"
broker
"
:
"
mock-broker
"
,
...
...
@@ -126,7 +145,7 @@ async def test_hassio_discovery_startup_done(hass, aioclient_mock, hassio_client
)
async
def
test_hassio_discovery_webhook
(
hass
,
aioclient_mock
,
hassio_client
):
async
def
test_hassio_discovery_webhook
(
hass
,
aioclient_mock
,
hassio_client
,
mock_mqtt
):
"""
Test discovery webhook.
"""
aioclient_mock
.
get
(
"
http://127.0.0.1/discovery/testuuid
"
,
...
...
@@ -151,30 +170,26 @@ async def test_hassio_discovery_webhook(hass, aioclient_mock, hassio_client):
json
=
{
"
result
"
:
"
ok
"
,
"
data
"
:
{
"
name
"
:
"
Mosquitto Test
"
}},
)
with
patch
(
"
homeassistant.components.mqtt.config_flow.FlowHandler.async_step_hassio
"
,
return_value
=
{
"
type
"
:
"
abort
"
},
)
as
mock_mqtt
:
resp
=
await
hassio_client
.
post
(
"
/api/hassio_push/discovery/testuuid
"
,
json
=
{
"
addon
"
:
"
mosquitto
"
,
"
service
"
:
"
mqtt
"
,
"
uuid
"
:
"
testuuid
"
},
)
await
hass
.
async_block_till_done
()
hass
.
bus
.
async_fire
(
EVENT_HOMEASSISTANT_STARTED
)
await
hass
.
async_block_till_done
()
resp
=
await
hassio_client
.
post
(
"
/api/hassio_push/discovery/testuuid
"
,
json
=
{
"
addon
"
:
"
mosquitto
"
,
"
service
"
:
"
mqtt
"
,
"
uuid
"
:
"
testuuid
"
},
)
await
hass
.
async_block_till_done
()
hass
.
bus
.
async_fire
(
EVENT_HOMEASSISTANT_STARTED
)
await
hass
.
async_block_till_done
()
assert
resp
.
status
==
HTTPStatus
.
OK
assert
aioclient_mock
.
call_count
==
2
assert
mock_mqtt
.
called
mock_mqtt
.
assert_called_with
(
HassioServiceInfo
(
config
=
{
"
broker
"
:
"
mock-broker
"
,
"
port
"
:
1883
,
"
username
"
:
"
mock-user
"
,
"
password
"
:
"
mock-pass
"
,
"
protocol
"
:
"
3.1.1
"
,
"
addon
"
:
"
Mosquitto Test
"
,
}
)
assert
resp
.
status
==
HTTPStatus
.
OK
assert
aioclient_mock
.
call_count
==
2
assert
mock_mqtt
.
async_step_hassio
.
called
mock_mqtt
.
async_step_hassio
.
assert_called_with
(
HassioServiceInfo
(
config
=
{
"
broker
"
:
"
mock-broker
"
,
"
port
"
:
1883
,
"
username
"
:
"
mock-user
"
,
"
password
"
:
"
mock-pass
"
,
"
protocol
"
:
"
3.1.1
"
,
"
addon
"
:
"
Mosquitto Test
"
,
}
)
)
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