mirror of
https://github.com/home-assistant/core.git
synced 2026-11-29 01:02:03 -04:00
Improve Elgato code quality (#46505)
This commit is contained in:
@@ -14,27 +14,26 @@ async def init_integration(
|
||||
skip_setup: bool = False,
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the Elgato Key Light integration in Home Assistant."""
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://1.2.3.4:9123/elgato/accessory-info",
|
||||
"http://127.0.0.1:9123/elgato/accessory-info",
|
||||
text=load_fixture("elgato/info.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
aioclient_mock.put(
|
||||
"http://1.2.3.4:9123/elgato/lights",
|
||||
"http://127.0.0.1:9123/elgato/lights",
|
||||
text=load_fixture("elgato/state.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://1.2.3.4:9123/elgato/lights",
|
||||
"http://127.0.0.1:9123/elgato/lights",
|
||||
text=load_fixture("elgato/state.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
aioclient_mock.get(
|
||||
"http://5.6.7.8:9123/elgato/accessory-info",
|
||||
"http://127.0.0.2:9123/elgato/accessory-info",
|
||||
text=load_fixture("elgato/info.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
@@ -43,7 +42,7 @@ async def init_integration(
|
||||
domain=DOMAIN,
|
||||
unique_id="CN11A1A00001",
|
||||
data={
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_HOST: "127.0.0.1",
|
||||
CONF_PORT: 9123,
|
||||
CONF_SERIAL_NUMBER: "CN11A1A00001",
|
||||
},
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
import aiohttp
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.elgato import config_flow
|
||||
from homeassistant.components.elgato.const import CONF_SERIAL_NUMBER
|
||||
from homeassistant.components.elgato.const import CONF_SERIAL_NUMBER, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SOURCE, CONTENT_TYPE_JSON
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import init_integration
|
||||
@@ -14,62 +13,97 @@ from tests.common import load_fixture
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
|
||||
async def test_show_user_form(hass: HomeAssistant) -> None:
|
||||
"""Test that the user set up form is served."""
|
||||
async def test_full_user_flow_implementation(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the full manual user flow from start to finish."""
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1:9123/elgato/accessory-info",
|
||||
text=load_fixture("elgato/info.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
# Start a discovered configuration flow, to guarantee a user flow doesn't abort
|
||||
await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_ZEROCONF},
|
||||
data={
|
||||
"host": "127.0.0.1",
|
||||
"hostname": "example.local.",
|
||||
"port": 9123,
|
||||
"properties": {},
|
||||
},
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
config_flow.DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
)
|
||||
|
||||
assert result["step_id"] == "user"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_HOST: "127.0.0.1", CONF_PORT: 9123}
|
||||
)
|
||||
|
||||
async def test_show_zeroconf_confirm_form(hass: HomeAssistant) -> None:
|
||||
"""Test that the zeroconf confirmation form is served."""
|
||||
flow = config_flow.ElgatoFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.context = {"source": SOURCE_ZEROCONF, CONF_SERIAL_NUMBER: "12345"}
|
||||
result = await flow.async_step_zeroconf_confirm()
|
||||
assert result["data"][CONF_HOST] == "127.0.0.1"
|
||||
assert result["data"][CONF_PORT] == 9123
|
||||
assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
||||
assert result["title"] == "CN11A1A00001"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
|
||||
assert result["description_placeholders"] == {CONF_SERIAL_NUMBER: "12345"}
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
assert entries[0].unique_id == "CN11A1A00001"
|
||||
|
||||
|
||||
async def test_show_zerconf_form(
|
||||
async def test_full_zeroconf_flow_implementation(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test that the zeroconf confirmation form is served."""
|
||||
"""Test the zeroconf flow from start to finish."""
|
||||
aioclient_mock.get(
|
||||
"http://1.2.3.4:9123/elgato/accessory-info",
|
||||
"http://127.0.0.1:9123/elgato/accessory-info",
|
||||
text=load_fixture("elgato/info.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
flow = config_flow.ElgatoFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.context = {"source": SOURCE_ZEROCONF}
|
||||
result = await flow.async_step_zeroconf({"host": "1.2.3.4", "port": 9123})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_ZEROCONF},
|
||||
data={
|
||||
"host": "127.0.0.1",
|
||||
"hostname": "example.local.",
|
||||
"port": 9123,
|
||||
"properties": {},
|
||||
},
|
||||
)
|
||||
|
||||
assert flow.context[CONF_HOST] == "1.2.3.4"
|
||||
assert flow.context[CONF_PORT] == 9123
|
||||
assert flow.context[CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
||||
assert result["description_placeholders"] == {CONF_SERIAL_NUMBER: "CN11A1A00001"}
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={}
|
||||
)
|
||||
assert result["data"][CONF_HOST] == "127.0.0.1"
|
||||
assert result["data"][CONF_PORT] == 9123
|
||||
assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
||||
assert result["title"] == "CN11A1A00001"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
|
||||
|
||||
async def test_connection_error(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we show user form on Elgato Key Light connection error."""
|
||||
aioclient_mock.get("http://1.2.3.4/elgato/accessory-info", exc=aiohttp.ClientError)
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/elgato/accessory-info", exc=aiohttp.ClientError
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
config_flow.DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={CONF_HOST: "1.2.3.4", CONF_PORT: 9123},
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data={CONF_HOST: "127.0.0.1", CONF_PORT: 9123},
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
@@ -81,51 +115,20 @@ async def test_zeroconf_connection_error(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we abort zeroconf flow on Elgato Key Light connection error."""
|
||||
aioclient_mock.get("http://1.2.3.4/elgato/accessory-info", exc=aiohttp.ClientError)
|
||||
aioclient_mock.get(
|
||||
"http://127.0.0.1/elgato/accessory-info", exc=aiohttp.ClientError
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
config_flow.DOMAIN,
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data={"host": "1.2.3.4", "port": 9123},
|
||||
data={"host": "127.0.0.1", "port": 9123},
|
||||
)
|
||||
|
||||
assert result["reason"] == "cannot_connect"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
|
||||
|
||||
async def test_zeroconf_confirm_connection_error(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we abort zeroconf flow on Elgato Key Light connection error."""
|
||||
aioclient_mock.get("http://1.2.3.4/elgato/accessory-info", exc=aiohttp.ClientError)
|
||||
|
||||
flow = config_flow.ElgatoFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.context = {
|
||||
"source": SOURCE_ZEROCONF,
|
||||
CONF_HOST: "1.2.3.4",
|
||||
CONF_PORT: 9123,
|
||||
}
|
||||
result = await flow.async_step_zeroconf_confirm(
|
||||
user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 9123}
|
||||
)
|
||||
|
||||
assert result["reason"] == "cannot_connect"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
|
||||
|
||||
async def test_zeroconf_no_data(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test we abort if zeroconf provides no data."""
|
||||
flow = config_flow.ElgatoFlowHandler()
|
||||
flow.hass = hass
|
||||
result = await flow.async_step_zeroconf()
|
||||
|
||||
assert result["reason"] == "cannot_connect"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
|
||||
|
||||
async def test_user_device_exists_abort(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
@@ -133,9 +136,9 @@ async def test_user_device_exists_abort(
|
||||
await init_integration(hass, aioclient_mock)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
config_flow.DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={CONF_HOST: "1.2.3.4", CONF_PORT: 9123},
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data={CONF_HOST: "127.0.0.1", CONF_PORT: 9123},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
@@ -148,84 +151,22 @@ async def test_zeroconf_device_exists_abort(
|
||||
await init_integration(hass, aioclient_mock)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
config_flow.DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF},
|
||||
data={"host": "1.2.3.4", "port": 9123},
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_ZEROCONF},
|
||||
data={"host": "127.0.0.1", "port": 9123},
|
||||
)
|
||||
|
||||
assert result["reason"] == "already_configured"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
config_flow.DOMAIN,
|
||||
context={"source": SOURCE_ZEROCONF, CONF_HOST: "1.2.3.4", "port": 9123},
|
||||
data={"host": "5.6.7.8", "port": 9123},
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_ZEROCONF},
|
||||
data={"host": "127.0.0.2", "port": 9123},
|
||||
)
|
||||
|
||||
assert result["reason"] == "already_configured"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
|
||||
entries = hass.config_entries.async_entries(config_flow.DOMAIN)
|
||||
assert entries[0].data[CONF_HOST] == "5.6.7.8"
|
||||
|
||||
|
||||
async def test_full_user_flow_implementation(
|
||||
hass: HomeAssistant, aioclient_mock
|
||||
) -> None:
|
||||
"""Test the full manual user flow from start to finish."""
|
||||
aioclient_mock.get(
|
||||
"http://1.2.3.4:9123/elgato/accessory-info",
|
||||
text=load_fixture("elgato/info.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
config_flow.DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
)
|
||||
|
||||
assert result["step_id"] == "user"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 9123}
|
||||
)
|
||||
|
||||
assert result["data"][CONF_HOST] == "1.2.3.4"
|
||||
assert result["data"][CONF_PORT] == 9123
|
||||
assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
||||
assert result["title"] == "CN11A1A00001"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
|
||||
entries = hass.config_entries.async_entries(config_flow.DOMAIN)
|
||||
assert entries[0].unique_id == "CN11A1A00001"
|
||||
|
||||
|
||||
async def test_full_zeroconf_flow_implementation(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the full manual user flow from start to finish."""
|
||||
aioclient_mock.get(
|
||||
"http://1.2.3.4:9123/elgato/accessory-info",
|
||||
text=load_fixture("elgato/info.json"),
|
||||
headers={"Content-Type": CONTENT_TYPE_JSON},
|
||||
)
|
||||
|
||||
flow = config_flow.ElgatoFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.context = {"source": SOURCE_ZEROCONF}
|
||||
result = await flow.async_step_zeroconf({"host": "1.2.3.4", "port": 9123})
|
||||
|
||||
assert flow.context[CONF_HOST] == "1.2.3.4"
|
||||
assert flow.context[CONF_PORT] == 9123
|
||||
assert flow.context[CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
||||
assert result["description_placeholders"] == {CONF_SERIAL_NUMBER: "CN11A1A00001"}
|
||||
assert result["step_id"] == "zeroconf_confirm"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
result = await flow.async_step_zeroconf_confirm(user_input={CONF_HOST: "1.2.3.4"})
|
||||
assert result["data"][CONF_HOST] == "1.2.3.4"
|
||||
assert result["data"][CONF_PORT] == 9123
|
||||
assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001"
|
||||
assert result["title"] == "CN11A1A00001"
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
assert entries[0].data[CONF_HOST] == "127.0.0.2"
|
||||
|
||||
@@ -14,7 +14,7 @@ async def test_config_entry_not_ready(
|
||||
) -> None:
|
||||
"""Test the Elgato Key Light configuration entry not ready."""
|
||||
aioclient_mock.get(
|
||||
"http://1.2.3.4:9123/elgato/accessory-info", exc=aiohttp.ClientError
|
||||
"http://127.0.0.1:9123/elgato/accessory-info", exc=aiohttp.ClientError
|
||||
)
|
||||
|
||||
entry = await init_integration(hass, aioclient_mock)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"""Tests for the Elgato Key Light light platform."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.elgato.light import ElgatoError
|
||||
from elgato import ElgatoError
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_COLOR_TEMP,
|
||||
|
||||
Reference in New Issue
Block a user