Add Wilight integration with SSDP (#36694)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Leonardo FigueiroandMartin Hjelmare authored and GitHub committed 2020-08-24 14:15:07 +02:00
1 parent fefa1a7259
commit a47f73244c
17 files changed
+1259

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
"""Tests for the WiLight component."""
from homeassistant.components.ssdp import (
ATTR_SSDP_LOCATION,
ATTR_UPNP_MANUFACTURER,
ATTR_UPNP_MODEL_NAME,
ATTR_UPNP_MODEL_NUMBER,
ATTR_UPNP_SERIAL,
)
from homeassistant.components.wilight.config_flow import (
CONF_MODEL_NAME,
CONF_SERIAL_NUMBER,
)
from homeassistant.components.wilight.const import DOMAIN
from homeassistant.const import CONF_HOST
from homeassistant.helpers.typing import HomeAssistantType
from tests.common import MockConfigEntry
HOST = "127.0.0.1"
WILIGHT_ID = "000000000099"
SSDP_LOCATION = "http://127.0.0.1/"
UPNP_MANUFACTURER = "All Automacao Ltda"
UPNP_MODEL_NAME_P_B = "WiLight 0102001800010009-10010010"
UPNP_MODEL_NAME_DIMMER = "WiLight 0100001700020009-10010010"
UPNP_MODEL_NAME_COLOR = "WiLight 0107001800020009-11010"
UPNP_MODEL_NAME_LIGHT_FAN = "WiLight 0104001800010009-10"
UPNP_MODEL_NUMBER = "123456789012345678901234567890123456"
UPNP_SERIAL = "000000000099"
UPNP_MAC_ADDRESS = "5C:CF:7F:8B:CA:56"
UPNP_MANUFACTURER_NOT_WILIGHT = "Test"
CONF_COMPONENTS = "components"
MOCK_SSDP_DISCOVERY_INFO_P_B = {
ATTR_SSDP_LOCATION: SSDP_LOCATION,
ATTR_UPNP_MANUFACTURER: UPNP_MANUFACTURER,
ATTR_UPNP_MODEL_NAME: UPNP_MODEL_NAME_P_B,
ATTR_UPNP_MODEL_NUMBER: UPNP_MODEL_NUMBER,
ATTR_UPNP_SERIAL: UPNP_SERIAL,
}
MOCK_SSDP_DISCOVERY_INFO_WRONG_MANUFACTORER = {
ATTR_SSDP_LOCATION: SSDP_LOCATION,
ATTR_UPNP_MANUFACTURER: UPNP_MANUFACTURER_NOT_WILIGHT,
ATTR_UPNP_MODEL_NAME: UPNP_MODEL_NAME_P_B,
ATTR_UPNP_MODEL_NUMBER: UPNP_MODEL_NUMBER,
ATTR_UPNP_SERIAL: ATTR_UPNP_SERIAL,
}
MOCK_SSDP_DISCOVERY_INFO_MISSING_MANUFACTORER = {
ATTR_SSDP_LOCATION: SSDP_LOCATION,
ATTR_UPNP_MODEL_NAME: UPNP_MODEL_NAME_P_B,
ATTR_UPNP_MODEL_NUMBER: UPNP_MODEL_NUMBER,
ATTR_UPNP_SERIAL: ATTR_UPNP_SERIAL,
}
MOCK_SSDP_DISCOVERY_INFO_LIGHT_FAN = {
ATTR_SSDP_LOCATION: SSDP_LOCATION,
ATTR_UPNP_MANUFACTURER: UPNP_MANUFACTURER,
ATTR_UPNP_MODEL_NAME: UPNP_MODEL_NAME_LIGHT_FAN,
ATTR_UPNP_MODEL_NUMBER: UPNP_MODEL_NUMBER,
ATTR_UPNP_SERIAL: ATTR_UPNP_SERIAL,
}
async def setup_integration(hass: HomeAssistantType,) -> MockConfigEntry:
"""Mock ConfigEntry in Home Assistant."""
entry = MockConfigEntry(
domain=DOMAIN,
unique_id=WILIGHT_ID,
data={
CONF_HOST: HOST,
CONF_SERIAL_NUMBER: UPNP_SERIAL,
CONF_MODEL_NAME: UPNP_MODEL_NAME_P_B,
},
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry
@@ -0,0 +1,157 @@
"""Test the WiLight config flow."""
from asynctest import patch
import pytest
from homeassistant.components.wilight.config_flow import (
CONF_MODEL_NAME,
CONF_SERIAL_NUMBER,
)
from homeassistant.components.wilight.const import DOMAIN
from homeassistant.config_entries import SOURCE_SSDP
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_SOURCE
from homeassistant.data_entry_flow import (
RESULT_TYPE_ABORT,
RESULT_TYPE_CREATE_ENTRY,
RESULT_TYPE_FORM,
)
from homeassistant.helpers.typing import HomeAssistantType
from tests.common import MockConfigEntry
from tests.components.wilight import (
CONF_COMPONENTS,
HOST,
MOCK_SSDP_DISCOVERY_INFO_LIGHT_FAN,
MOCK_SSDP_DISCOVERY_INFO_MISSING_MANUFACTORER,
MOCK_SSDP_DISCOVERY_INFO_P_B,
MOCK_SSDP_DISCOVERY_INFO_WRONG_MANUFACTORER,
UPNP_MODEL_NAME_P_B,
UPNP_SERIAL,
WILIGHT_ID,
)
@pytest.fixture(name="dummy_get_components_from_model_clear")
def mock_dummy_get_components_from_model():
"""Mock a clear components list."""
components = []
with patch(
"pywilight.get_components_from_model", return_value=components,
):
yield components
async def test_show_ssdp_form(hass: HomeAssistantType) -> None:
"""Test that the ssdp confirmation form is served."""
discovery_info = MOCK_SSDP_DISCOVERY_INFO_P_B.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "confirm"
assert result["description_placeholders"] == {
CONF_NAME: f"WL{WILIGHT_ID}",
CONF_COMPONENTS: "light",
}
async def test_ssdp_not_wilight_abort_1(hass: HomeAssistantType) -> None:
"""Test that the ssdp aborts not_wilight."""
discovery_info = MOCK_SSDP_DISCOVERY_INFO_WRONG_MANUFACTORER.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "not_wilight_device"
async def test_ssdp_not_wilight_abort_2(hass: HomeAssistantType) -> None:
"""Test that the ssdp aborts not_wilight."""
discovery_info = MOCK_SSDP_DISCOVERY_INFO_MISSING_MANUFACTORER.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "not_wilight_device"
async def test_ssdp_not_wilight_abort_3(
hass: HomeAssistantType, dummy_get_components_from_model_clear
) -> None:
"""Test that the ssdp aborts not_wilight."""
discovery_info = MOCK_SSDP_DISCOVERY_INFO_P_B.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "not_wilight_device"
async def test_ssdp_not_supported_abort(hass: HomeAssistantType) -> None:
"""Test that the ssdp aborts not_supported."""
discovery_info = MOCK_SSDP_DISCOVERY_INFO_LIGHT_FAN.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "not_supported_device"
async def test_ssdp_device_exists_abort(hass: HomeAssistantType) -> None:
"""Test abort SSDP flow if WiLight already configured."""
entry = MockConfigEntry(
domain=DOMAIN,
unique_id=WILIGHT_ID,
data={
CONF_HOST: HOST,
CONF_SERIAL_NUMBER: UPNP_SERIAL,
CONF_MODEL_NAME: UPNP_MODEL_NAME_P_B,
},
)
entry.add_to_hass(hass)
discovery_info = MOCK_SSDP_DISCOVERY_INFO_P_B.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info,
)
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
async def test_full_ssdp_flow_implementation(hass: HomeAssistantType) -> None:
"""Test the full SSDP flow from start to finish."""
discovery_info = MOCK_SSDP_DISCOVERY_INFO_P_B.copy()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={CONF_SOURCE: SOURCE_SSDP}, data=discovery_info
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "confirm"
assert result["description_placeholders"] == {
CONF_NAME: f"WL{WILIGHT_ID}",
"components": "light",
}
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
assert result["title"] == f"WL{WILIGHT_ID}"
assert result["data"]
assert result["data"][CONF_HOST] == HOST
assert result["data"][CONF_SERIAL_NUMBER] == UPNP_SERIAL
assert result["data"][CONF_MODEL_NAME] == UPNP_MODEL_NAME_P_B
+65
View File
@@ -0,0 +1,65 @@
"""Tests for the WiLight integration."""
from asynctest import patch
import pytest
import pywilight
from homeassistant.components.wilight.const import DOMAIN
from homeassistant.config_entries import (
ENTRY_STATE_LOADED,
ENTRY_STATE_NOT_LOADED,
ENTRY_STATE_SETUP_RETRY,
)
from homeassistant.helpers.typing import HomeAssistantType
from tests.components.wilight import (
HOST,
UPNP_MAC_ADDRESS,
UPNP_MODEL_NAME_P_B,
UPNP_MODEL_NUMBER,
UPNP_SERIAL,
setup_integration,
)
@pytest.fixture(name="dummy_device_from_host")
def mock_dummy_device_from_host():
"""Mock a valid api_devce."""
device = pywilight.wilight_from_discovery(
f"http://{HOST}:45995/wilight.xml",
UPNP_MAC_ADDRESS,
UPNP_MODEL_NAME_P_B,
UPNP_SERIAL,
UPNP_MODEL_NUMBER,
)
device.set_dummy(True)
with patch(
"pywilight.device_from_host", return_value=device,
):
yield device
async def test_config_entry_not_ready(hass: HomeAssistantType) -> None:
"""Test the WiLight configuration entry not ready."""
entry = await setup_integration(hass)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_unload_config_entry(
hass: HomeAssistantType, dummy_device_from_host
) -> None:
"""Test the WiLight configuration entry unloading."""
entry = await setup_integration(hass)
assert entry.entry_id in hass.data[DOMAIN]
assert entry.state == ENTRY_STATE_LOADED
await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
if DOMAIN in hass.data:
assert entry.entry_id not in hass.data[DOMAIN]
assert entry.state == ENTRY_STATE_NOT_LOADED
+369
View File
@@ -0,0 +1,369 @@
"""Tests for the WiLight integration."""
from asynctest import patch
import pytest
import pywilight
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_HS_COLOR,
DOMAIN as LIGHT_DOMAIN,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
)
from homeassistant.helpers.typing import HomeAssistantType
from tests.components.wilight import (
HOST,
UPNP_MAC_ADDRESS,
UPNP_MODEL_NAME_COLOR,
UPNP_MODEL_NAME_DIMMER,
UPNP_MODEL_NAME_LIGHT_FAN,
UPNP_MODEL_NAME_P_B,
UPNP_MODEL_NUMBER,
UPNP_SERIAL,
WILIGHT_ID,
setup_integration,
)
@pytest.fixture(name="dummy_get_components_from_model_light")
def mock_dummy_get_components_from_model_light():
"""Mock a components list with light."""
components = ["light"]
with patch(
"pywilight.get_components_from_model", return_value=components,
):
yield components
@pytest.fixture(name="dummy_device_from_host_light_fan")
def mock_dummy_device_from_host_light_fan():
"""Mock a valid api_devce."""
device = pywilight.wilight_from_discovery(
f"http://{HOST}:45995/wilight.xml",
UPNP_MAC_ADDRESS,
UPNP_MODEL_NAME_LIGHT_FAN,
UPNP_SERIAL,
UPNP_MODEL_NUMBER,
)
device.set_dummy(True)
with patch(
"pywilight.device_from_host", return_value=device,
):
yield device
@pytest.fixture(name="dummy_device_from_host_pb")
def mock_dummy_device_from_host_pb():
"""Mock a valid api_devce."""
device = pywilight.wilight_from_discovery(
f"http://{HOST}:45995/wilight.xml",
UPNP_MAC_ADDRESS,
UPNP_MODEL_NAME_P_B,
UPNP_SERIAL,
UPNP_MODEL_NUMBER,
)
device.set_dummy(True)
with patch(
"pywilight.device_from_host", return_value=device,
):
yield device
@pytest.fixture(name="dummy_device_from_host_dimmer")
def mock_dummy_device_from_host_dimmer():
"""Mock a valid api_devce."""
device = pywilight.wilight_from_discovery(
f"http://{HOST}:45995/wilight.xml",
UPNP_MAC_ADDRESS,
UPNP_MODEL_NAME_DIMMER,
UPNP_SERIAL,
UPNP_MODEL_NUMBER,
)
device.set_dummy(True)
with patch(
"pywilight.device_from_host", return_value=device,
):
yield device
@pytest.fixture(name="dummy_device_from_host_color")
def mock_dummy_device_from_host_color():
"""Mock a valid api_devce."""
device = pywilight.wilight_from_discovery(
f"http://{HOST}:45995/wilight.xml",
UPNP_MAC_ADDRESS,
UPNP_MODEL_NAME_COLOR,
UPNP_SERIAL,
UPNP_MODEL_NUMBER,
)
device.set_dummy(True)
with patch(
"pywilight.device_from_host", return_value=device,
):
yield device
async def test_loading_light(
hass: HomeAssistantType,
dummy_device_from_host_light_fan,
dummy_get_components_from_model_light,
) -> None:
"""Test the WiLight configuration entry loading."""
# Using light_fan and removind fan from get_components_from_model
# to test light.py line 28
entry = await setup_integration(hass)
assert entry
assert entry.unique_id == WILIGHT_ID
entity_registry = await hass.helpers.entity_registry.async_get_registry()
# First segment of the strip
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_OFF
entry = entity_registry.async_get("light.wl000000000099_1")
assert entry
assert entry.unique_id == "WL000000000099_0"
async def test_on_off_light_state(
hass: HomeAssistantType, dummy_device_from_host_pb
) -> None:
"""Test the change of state of the light switches."""
await setup_integration(hass)
# Turn on
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
# Turn off
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_OFF
async def test_dimmer_light_state(
hass: HomeAssistantType, dummy_device_from_host_dimmer
) -> None:
"""Test the change of state of the light switches."""
await setup_integration(hass)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_BRIGHTNESS: 42, ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
assert state.attributes.get(ATTR_BRIGHTNESS) == 42
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_BRIGHTNESS: 0, ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_OFF
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_BRIGHTNESS: 100, ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
assert state.attributes.get(ATTR_BRIGHTNESS) == 100
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_OFF
# Turn on
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
async def test_color_light_state(
hass: HomeAssistantType, dummy_device_from_host_color
) -> None:
"""Test the change of state of the light switches."""
await setup_integration(hass)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_BRIGHTNESS: 42,
ATTR_HS_COLOR: [0, 100],
ATTR_ENTITY_ID: "light.wl000000000099_1",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
assert state.attributes.get(ATTR_BRIGHTNESS) == 42
state_color = [
round(state.attributes.get(ATTR_HS_COLOR)[0]),
round(state.attributes.get(ATTR_HS_COLOR)[1]),
]
assert state_color == [0, 100]
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_BRIGHTNESS: 0, ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_OFF
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_BRIGHTNESS: 100,
ATTR_HS_COLOR: [270, 50],
ATTR_ENTITY_ID: "light.wl000000000099_1",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
assert state.attributes.get(ATTR_BRIGHTNESS) == 100
state_color = [
round(state.attributes.get(ATTR_HS_COLOR)[0]),
round(state.attributes.get(ATTR_HS_COLOR)[1]),
]
assert state_color == [270, 50]
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_OFF
# Turn on
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
# Hue = 0, Saturation = 100
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_HS_COLOR: [0, 100], ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
state_color = [
round(state.attributes.get(ATTR_HS_COLOR)[0]),
round(state.attributes.get(ATTR_HS_COLOR)[1]),
]
assert state_color == [0, 100]
# Brightness = 60
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_BRIGHTNESS: 60, ATTR_ENTITY_ID: "light.wl000000000099_1"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wl000000000099_1")
assert state
assert state.state == STATE_ON
assert state.attributes.get(ATTR_BRIGHTNESS) == 60