mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 09:58:07 -04:00
Set up Almond Web to connect to HA (#28603)
* Set up Almond Web to connect to HA * Add missing string * Add type
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
"""Tests for Almond set up."""
|
||||
from unittest.mock import patch
|
||||
from time import time
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries, core
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.almond import const
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from tests.common import MockConfigEntry, mock_coro, async_fire_time_changed
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def patch_hass_state(hass):
|
||||
"""Mock the hass.state to be not_running."""
|
||||
hass.state = core.CoreState.not_running
|
||||
|
||||
|
||||
async def test_set_up_oauth_remote_url(hass, aioclient_mock):
|
||||
"""Test we set up Almond to connect to HA if we have external url."""
|
||||
entry = MockConfigEntry(
|
||||
domain="almond",
|
||||
data={
|
||||
"type": const.TYPE_OAUTH2,
|
||||
"auth_implementation": "local",
|
||||
"host": "http://localhost:9999",
|
||||
"token": {"expires_at": time() + 1000, "access_token": "abcd"},
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
|
||||
return_value=mock_coro(),
|
||||
):
|
||||
assert await async_setup_component(hass, "almond", {})
|
||||
|
||||
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
||||
|
||||
with patch("homeassistant.components.almond.ALMOND_SETUP_DELAY", 0), patch(
|
||||
"homeassistant.helpers.network.async_get_external_url",
|
||||
return_value="https://example.nabu.casa",
|
||||
), patch(
|
||||
"pyalmond.WebAlmondAPI.async_create_device", return_value=mock_coro()
|
||||
) as mock_create_device:
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_time_changed(hass, utcnow())
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_create_device.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_set_up_oauth_no_external_url(hass, aioclient_mock):
|
||||
"""Test we do not set up Almond to connect to HA if we have no external url."""
|
||||
entry = MockConfigEntry(
|
||||
domain="almond",
|
||||
data={
|
||||
"type": const.TYPE_OAUTH2,
|
||||
"auth_implementation": "local",
|
||||
"host": "http://localhost:9999",
|
||||
"token": {"expires_at": time() + 1000, "access_token": "abcd"},
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
|
||||
return_value=mock_coro(),
|
||||
), patch("pyalmond.WebAlmondAPI.async_create_device") as mock_create_device:
|
||||
assert await async_setup_component(hass, "almond", {})
|
||||
|
||||
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
||||
assert len(mock_create_device.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_set_up_hassio(hass, aioclient_mock):
|
||||
"""Test we do not set up Almond to connect to HA if we use hassio."""
|
||||
entry = MockConfigEntry(
|
||||
domain="almond",
|
||||
data={
|
||||
"is_hassio": True,
|
||||
"type": const.TYPE_LOCAL,
|
||||
"host": "http://localhost:9999",
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch("pyalmond.WebAlmondAPI.async_create_device") as mock_create_device:
|
||||
assert await async_setup_component(hass, "almond", {})
|
||||
|
||||
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
||||
assert len(mock_create_device.mock_calls) == 0
|
||||
|
||||
|
||||
async def test_set_up_local(hass, aioclient_mock):
|
||||
"""Test we do not set up Almond to connect to HA if we use hassio."""
|
||||
entry = MockConfigEntry(
|
||||
domain="almond",
|
||||
data={"type": const.TYPE_LOCAL, "host": "http://localhost:9999"},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"pyalmond.WebAlmondAPI.async_create_device", return_value=mock_coro()
|
||||
) as mock_create_device:
|
||||
assert await async_setup_component(hass, "almond", {})
|
||||
|
||||
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
||||
assert len(mock_create_device.mock_calls) == 1
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Test network helper."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant.helpers import network
|
||||
from homeassistant.components import cloud
|
||||
|
||||
|
||||
async def test_get_external_url(hass):
|
||||
"""Test get_external_url."""
|
||||
hass.config.api = Mock(base_url="http://192.168.1.100:8123")
|
||||
|
||||
assert network.async_get_external_url(hass) is None
|
||||
|
||||
hass.config.api = Mock(base_url="http://example.duckdns.org:8123")
|
||||
|
||||
assert network.async_get_external_url(hass) == "http://example.duckdns.org:8123"
|
||||
|
||||
hass.config.components.add("cloud")
|
||||
|
||||
assert network.async_get_external_url(hass) == "http://example.duckdns.org:8123"
|
||||
|
||||
with patch.object(
|
||||
hass.components.cloud,
|
||||
"async_remote_ui_url",
|
||||
side_effect=cloud.CloudNotAvailable,
|
||||
):
|
||||
assert network.async_get_external_url(hass) == "http://example.duckdns.org:8123"
|
||||
|
||||
with patch.object(
|
||||
hass.components.cloud,
|
||||
"async_remote_ui_url",
|
||||
return_value="https://example.nabu.casa",
|
||||
):
|
||||
assert network.async_get_external_url(hass) == "https://example.nabu.casa"
|
||||
Reference in New Issue
Block a user