Add twinkly DHCP support (#61434)

* Add twinkly DHCP support

* fix typing import

* fix format

* Fix imports v2

* Using IP

* Fix tests

* Apply suggestions from code review

Thanks @bdraco

Co-authored-by: J. Nick Koston <nick@koston.org>

* fix black

* Add confirm step

* Add more tests

* Update homeassistant/components/twinkly/config_flow.py

Co-authored-by: J. Nick Koston <nick@koston.org>

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Pascal Vizeli 2021-12-14 15:12:19 +01:00 committed by GitHub
parent 36d3fb15f7
commit c02aae58fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 152 additions and 11 deletions

View file

@ -1,8 +1,8 @@
"""Tests for the config_flow of the twinly component."""
from unittest.mock import patch
from homeassistant import config_entries
from homeassistant.components import dhcp
from homeassistant.components.twinkly.const import (
CONF_ENTRY_HOST,
CONF_ENTRY_ID,
@ -13,6 +13,8 @@ from homeassistant.components.twinkly.const import (
from . import TEST_MODEL, ClientMock
from tests.common import MockConfigEntry
async def test_invalid_host(hass):
"""Test the failure when invalid host provided."""
@ -60,3 +62,84 @@ async def test_success_flow(hass):
CONF_ENTRY_NAME: client.id,
CONF_ENTRY_MODEL: TEST_MODEL,
}
async def test_dhcp_can_confirm(hass):
"""Test DHCP discovery flow can confirm right away."""
client = ClientMock()
with patch("twinkly_client.TwinklyClient", return_value=client):
result = await hass.config_entries.flow.async_init(
TWINKLY_DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data=dhcp.DhcpServiceInfo(
hostname="Twinkly_XYZ",
ip="1.2.3.4",
macaddress="aa:bb:cc:dd:ee:ff",
),
)
await hass.async_block_till_done()
assert result["type"] == "form"
assert result["step_id"] == "discovery_confirm"
async def test_dhcp_success(hass):
"""Test DHCP discovery flow success."""
client = ClientMock()
with patch("twinkly_client.TwinklyClient", return_value=client):
result = await hass.config_entries.flow.async_init(
TWINKLY_DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data=dhcp.DhcpServiceInfo(
hostname="Twinkly_XYZ",
ip="1.2.3.4",
macaddress="aa:bb:cc:dd:ee:ff",
),
)
await hass.async_block_till_done()
assert result["type"] == "form"
assert result["step_id"] == "discovery_confirm"
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
assert result["type"] == "create_entry"
assert result["title"] == client.id
assert result["data"] == {
CONF_ENTRY_HOST: "1.2.3.4",
CONF_ENTRY_ID: client.id,
CONF_ENTRY_NAME: client.id,
CONF_ENTRY_MODEL: TEST_MODEL,
}
async def test_dhcp_already_exists(hass):
"""Test DHCP discovery flow that fails to connect."""
client = ClientMock()
entry = MockConfigEntry(
domain=TWINKLY_DOMAIN,
data={
CONF_ENTRY_HOST: "1.2.3.4",
CONF_ENTRY_ID: client.id,
CONF_ENTRY_NAME: client.id,
CONF_ENTRY_MODEL: TEST_MODEL,
},
unique_id=client.id,
)
entry.add_to_hass(hass)
with patch("twinkly_client.TwinklyClient", return_value=client):
result = await hass.config_entries.flow.async_init(
TWINKLY_DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data=dhcp.DhcpServiceInfo(
hostname="Twinkly_XYZ",
ip="1.2.3.4",
macaddress="aa:bb:cc:dd:ee:ff",
),
)
await hass.async_block_till_done()
assert result["type"] == "abort"
assert result["reason"] == "already_configured"