Add Elgato Key Light integration (#29592)

* Add Elgato Key Light integration

* Remove passing in of hass loop

* Tweaks a comment

* Tweaks a function name

* Ensure domain namespace in data exists in entry setup
This commit is contained in:
Franck Nijhof authored and Paulus Schoutsen committed 2019-12-08 09:26:31 +01:00
1 parent 7f4baab3f6
commit cc9589cff2
18 files changed
+894

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
"""Tests for the Elgato Key Light integration."""
from homeassistant.components.elgato.const import CONF_SERIAL_NUMBER, DOMAIN
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
async def init_integration(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, skip_setup: bool = False,
) -> MockConfigEntry:
"""Set up the Elgato Key Light integration in Home Assistant."""
aioclient_mock.get(
"http://example.local:9123/elgato/accessory-info",
text=load_fixture("elgato/info.json"),
headers={"Content-Type": "application/json"},
)
aioclient_mock.put(
"http://example.local:9123/elgato/lights",
text=load_fixture("elgato/state.json"),
headers={"Content-Type": "application/json"},
)
aioclient_mock.get(
"http://example.local:9123/elgato/lights",
text=load_fixture("elgato/state.json"),
headers={"Content-Type": "application/json"},
)
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "example.local",
CONF_PORT: 9123,
CONF_SERIAL_NUMBER: "CN11A1A00001",
},
)
entry.add_to_hass(hass)
if not skip_setup:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry
+238
View File
@@ -0,0 +1,238 @@
"""Tests for the Elgato Key Light config flow."""
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.config_entries import SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from . import init_integration
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."""
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=None)
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
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["description_placeholders"] == {CONF_SERIAL_NUMBER: "12345"}
assert result["step_id"] == "zeroconf_confirm"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
async def test_show_zerconf_form(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test that the zeroconf confirmation form is served."""
aioclient_mock.get(
"http://example.local:9123/elgato/accessory-info",
text=load_fixture("elgato/info.json"),
headers={"Content-Type": "application/json"},
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf(
{"hostname": "example.local.", "port": 9123}
)
assert flow.context[CONF_HOST] == "example.local"
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
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://example.local/elgato/accessory-info", exc=aiohttp.ClientError
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(
user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
)
assert result["errors"] == {"base": "connection_error"}
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
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://example.local/elgato/accessory-info", exc=aiohttp.ClientError
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf(
user_input={"hostname": "example.local.", "port": 9123}
)
assert result["reason"] == "connection_error"
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://example.local/elgato/accessory-info", exc=aiohttp.ClientError
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {
"source": SOURCE_ZEROCONF,
CONF_HOST: "example.local",
CONF_PORT: 9123,
}
result = await flow.async_step_zeroconf_confirm(
user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
)
assert result["reason"] == "connection_error"
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"] == "connection_error"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
async def test_user_device_exists_abort(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
await init_integration(hass, aioclient_mock)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user({CONF_HOST: "example.local", CONF_PORT: 9123})
assert result["reason"] == "already_configured"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
async def test_zeroconf_device_exists_abort(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test we abort zeroconf flow if Elgato Key Light device already configured."""
await init_integration(hass, aioclient_mock)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf(
{"hostname": "example.local.", "port": 9123}
)
assert result["reason"] == "already_configured"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
flow.context = {"source": SOURCE_ZEROCONF, CONF_HOST: "example.local", "port": 9123}
result = await flow.async_step_zeroconf_confirm(
{"hostname": "example.local.", "port": 9123}
)
assert result["reason"] == "already_configured"
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
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://example.local:9123/elgato/accessory-info",
text=load_fixture("elgato/info.json"),
headers={"Content-Type": "application/json"},
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_USER}
result = await flow.async_step_user(user_input=None)
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
result = await flow.async_step_user(
user_input={CONF_HOST: "example.local", CONF_PORT: 9123}
)
assert result["data"][CONF_HOST] == "example.local"
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_full_zeroconf_flow_implementation(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the full manual user flow from start to finish."""
aioclient_mock.get(
"http://example.local:9123/elgato/accessory-info",
text=load_fixture("elgato/info.json"),
headers={"Content-Type": "application/json"},
)
flow = config_flow.ElgatoFlowHandler()
flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf(
{"hostname": "example.local.", "port": 9123}
)
assert flow.context[CONF_HOST] == "example.local"
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: "example.local"}
)
assert result["data"][CONF_HOST] == "example.local"
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
+33
View File
@@ -0,0 +1,33 @@
"""Tests for the Elgato Key Light integration."""
import aiohttp
from homeassistant.components.elgato.const import DOMAIN
from homeassistant.config_entries import ENTRY_STATE_SETUP_RETRY
from homeassistant.core import HomeAssistant
from tests.components.elgato import init_integration
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_config_entry_not_ready(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the Elgato Key Light configuration entry not ready."""
aioclient_mock.get(
"http://example.local:9123/elgato/accessory-info", exc=aiohttp.ClientError
)
entry = await init_integration(hass, aioclient_mock)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_unload_config_entry(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the Elgato Key Light configuration entry unloading."""
entry = await init_integration(hass, aioclient_mock)
assert hass.data[DOMAIN]
await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert not hass.data.get(DOMAIN)
+104
View File
@@ -0,0 +1,104 @@
"""Tests for the Elgato Key Light light platform."""
from unittest.mock import patch
from homeassistant.components.elgato.light import ElgatoError
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
DOMAIN as LIGHT_DOMAIN,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_ON,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from tests.common import mock_coro
from tests.components.elgato import init_integration
from tests.test_util.aiohttp import AiohttpClientMocker
async def test_light_state(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the creation and values of the Elgato Key Lights."""
await init_integration(hass, aioclient_mock)
entity_registry = await hass.helpers.entity_registry.async_get_registry()
# First segment of the strip
state = hass.states.get("light.frenck")
assert state
assert state.attributes.get(ATTR_BRIGHTNESS) == 54
assert state.attributes.get(ATTR_COLOR_TEMP) == 297
assert state.state == STATE_ON
entry = entity_registry.async_get("light.frenck")
assert entry
assert entry.unique_id == "CN11A1A00001"
async def test_light_change_state(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test the change of state of a Elgato Key Light device."""
await init_integration(hass, aioclient_mock)
state = hass.states.get("light.frenck")
assert state.state == STATE_ON
with patch(
"homeassistant.components.elgato.light.Elgato.light", return_value=mock_coro(),
) as mock_light:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: "light.frenck",
ATTR_BRIGHTNESS: 255,
ATTR_COLOR_TEMP: 100,
},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_light.mock_calls) == 1
mock_light.assert_called_with(on=True, brightness=100, temperature=100)
with patch(
"homeassistant.components.elgato.light.Elgato.light", return_value=mock_coro(),
) as mock_light:
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.frenck"},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_light.mock_calls) == 1
mock_light.assert_called_with(on=False)
async def test_light_unavailable(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test error/unavailable handling of an Elgato Key Light."""
await init_integration(hass, aioclient_mock)
with patch(
"homeassistant.components.elgato.light.Elgato.light", side_effect=ElgatoError,
):
with patch(
"homeassistant.components.elgato.light.Elgato.state",
side_effect=ElgatoError,
):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.frenck"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.frenck")
assert state.state == STATE_UNAVAILABLE