Add Ring config flow (#30564)
* Add Ring config flow * Address comments + migrate platforms to config entry * Migrate camera too * Address comments * Fix order config flows * setup -> async_setup
This commit is contained in:
1 parent
3348f4f6d1
commit
3f29c234b8
17 files changed
+435
-146
No files matched your search
@@ -1,14 +1,15 @@
|
||||
"""Common methods used across the tests for ring devices."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.ring import DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def setup_platform(hass, platform):
|
||||
"""Set up the ring platform and prerequisites."""
|
||||
config = {
|
||||
DOMAIN: {CONF_USERNAME: "foo", CONF_PASSWORD: "bar", CONF_SCAN_INTERVAL: 1000},
|
||||
platform: {"platform": DOMAIN},
|
||||
}
|
||||
assert await async_setup_component(hass, platform, config)
|
||||
MockConfigEntry(domain=DOMAIN, data={"username": "foo"}).add_to_hass(hass)
|
||||
with patch("homeassistant.components.ring.PLATFORMS", [platform]):
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
@@ -36,6 +36,10 @@ def requests_mock_fixture(ring_mock):
|
||||
"https://api.ring.com/clients_api/ring_devices",
|
||||
text=load_fixture("ring_devices.json"),
|
||||
)
|
||||
mock.get(
|
||||
"https://api.ring.com/clients_api/dings/active",
|
||||
text=load_fixture("ring_ding_active.json"),
|
||||
)
|
||||
# Mocks the response for getting the history of a device
|
||||
mock.get(
|
||||
"https://api.ring.com/clients_api/doorbots/987652/history",
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
"""The tests for the Ring binary sensor platform."""
|
||||
from asyncio import run_coroutine_threadsafe
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import requests_mock
|
||||
|
||||
from homeassistant.components import ring as base_ring
|
||||
from homeassistant.components.ring import binary_sensor as ring
|
||||
|
||||
from tests.common import get_test_config_dir, get_test_home_assistant, load_fixture
|
||||
from tests.common import (
|
||||
get_test_config_dir,
|
||||
get_test_home_assistant,
|
||||
load_fixture,
|
||||
mock_storage,
|
||||
)
|
||||
from tests.components.ring.test_init import ATTRIBUTION, VALID_CONFIG
|
||||
|
||||
|
||||
@@ -68,8 +75,17 @@ class TestRingBinarySensorSetup(unittest.TestCase):
|
||||
text=load_fixture("ring_chime_health_attrs.json"),
|
||||
)
|
||||
|
||||
base_ring.setup(self.hass, VALID_CONFIG)
|
||||
ring.setup_platform(self.hass, self.config, self.add_entities, None)
|
||||
with mock_storage(), patch("homeassistant.components.ring.PLATFORMS", []):
|
||||
run_coroutine_threadsafe(
|
||||
base_ring.async_setup(self.hass, VALID_CONFIG), self.hass.loop
|
||||
).result()
|
||||
run_coroutine_threadsafe(
|
||||
self.hass.async_block_till_done(), self.hass.loop
|
||||
).result()
|
||||
run_coroutine_threadsafe(
|
||||
ring.async_setup_entry(self.hass, None, self.add_entities),
|
||||
self.hass.loop,
|
||||
).result()
|
||||
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Test the Ring config flow."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.ring import DOMAIN
|
||||
from homeassistant.components.ring.config_flow import InvalidAuth
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
async def test_form(hass):
|
||||
"""Test we get the form."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.ring.config_flow.Ring",
|
||||
return_value=Mock(is_connected=True),
|
||||
), patch(
|
||||
"homeassistant.components.ring.async_setup", return_value=mock_coro(True)
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.ring.async_setup_entry", return_value=mock_coro(True),
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "hello@home-assistant.io", "password": "test-password"},
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "hello@home-assistant.io"
|
||||
assert result2["data"] == {
|
||||
"username": "hello@home-assistant.io",
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass):
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.ring.config_flow.Ring", side_effect=InvalidAuth,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "hello@home-assistant.io", "password": "test-password"},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
@@ -1,4 +1,5 @@
|
||||
"""The tests for the Ring component."""
|
||||
from asyncio import run_coroutine_threadsafe
|
||||
from copy import deepcopy
|
||||
from datetime import timedelta
|
||||
import os
|
||||
@@ -59,7 +60,10 @@ class TestRing(unittest.TestCase):
|
||||
"https://api.ring.com/clients_api/doorbots/987652/health",
|
||||
text=load_fixture("ring_doorboot_health_attrs.json"),
|
||||
)
|
||||
response = ring.setup(self.hass, self.config)
|
||||
response = run_coroutine_threadsafe(
|
||||
ring.async_setup(self.hass, self.config), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert response
|
||||
|
||||
@requests_mock.Mocker()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"""The tests for the Ring sensor platform."""
|
||||
from asyncio import run_coroutine_threadsafe
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import requests_mock
|
||||
|
||||
@@ -8,7 +10,12 @@ from homeassistant.components import ring as base_ring
|
||||
import homeassistant.components.ring.sensor as ring
|
||||
from homeassistant.helpers.icon import icon_for_battery_level
|
||||
|
||||
from tests.common import get_test_config_dir, get_test_home_assistant, load_fixture
|
||||
from tests.common import (
|
||||
get_test_config_dir,
|
||||
get_test_home_assistant,
|
||||
load_fixture,
|
||||
mock_storage,
|
||||
)
|
||||
from tests.components.ring.test_init import ATTRIBUTION, VALID_CONFIG
|
||||
|
||||
|
||||
@@ -76,8 +83,18 @@ class TestRingSensorSetup(unittest.TestCase):
|
||||
"https://api.ring.com/clients_api/chimes/999999/health",
|
||||
text=load_fixture("ring_chime_health_attrs.json"),
|
||||
)
|
||||
base_ring.setup(self.hass, VALID_CONFIG)
|
||||
ring.setup_platform(self.hass, self.config, self.add_entities, None)
|
||||
|
||||
with mock_storage(), patch("homeassistant.components.ring.PLATFORMS", []):
|
||||
run_coroutine_threadsafe(
|
||||
base_ring.async_setup(self.hass, VALID_CONFIG), self.hass.loop
|
||||
).result()
|
||||
run_coroutine_threadsafe(
|
||||
self.hass.async_block_till_done(), self.hass.loop
|
||||
).result()
|
||||
run_coroutine_threadsafe(
|
||||
ring.async_setup_entry(self.hass, None, self.add_entities),
|
||||
self.hass.loop,
|
||||
).result()
|
||||
|
||||
for device in self.DEVICES:
|
||||
device.update()
|
||||
|
||||
Reference in new issue
Block a user