2020-12-15 08:59:23 +00:00
|
|
|
"""Tests for the wemo component."""
|
2020-12-24 11:25:14 +00:00
|
|
|
from datetime import timedelta
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import create_autospec, patch
|
2020-12-24 11:25:14 +00:00
|
|
|
|
|
|
|
import pywemo
|
|
|
|
|
|
|
|
from homeassistant.components.wemo import CONF_DISCOVERY, CONF_STATIC, WemoDiscovery
|
2020-12-15 08:59:23 +00:00
|
|
|
from homeassistant.components.wemo.const import DOMAIN
|
|
|
|
from homeassistant.setup import async_setup_component
|
2020-12-24 11:25:14 +00:00
|
|
|
from homeassistant.util import dt
|
2020-12-15 08:59:23 +00:00
|
|
|
|
2020-12-24 11:25:14 +00:00
|
|
|
from .conftest import MOCK_HOST, MOCK_NAME, MOCK_PORT, MOCK_SERIAL_NUMBER
|
|
|
|
|
|
|
|
from tests.common import async_fire_time_changed
|
2020-12-15 08:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_config_no_config(hass):
|
|
|
|
"""Component setup succeeds when there are no config entry for the domain."""
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {})
|
|
|
|
|
|
|
|
|
|
|
|
async def test_config_no_static(hass):
|
|
|
|
"""Component setup succeeds when there are no static config entries."""
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_DISCOVERY: False}})
|
|
|
|
|
|
|
|
|
|
|
|
async def test_static_duplicate_static_entry(hass, pywemo_device):
|
|
|
|
"""Duplicate static entries are merged into a single entity."""
|
|
|
|
static_config_entry = f"{MOCK_HOST}:{MOCK_PORT}"
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
CONF_DISCOVERY: False,
|
|
|
|
CONF_STATIC: [
|
|
|
|
static_config_entry,
|
|
|
|
static_config_entry,
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
|
|
|
entity_entries = list(entity_reg.entities.values())
|
|
|
|
assert len(entity_entries) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_static_config_with_port(hass, pywemo_device):
|
|
|
|
"""Static device with host and port is added and removed."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
CONF_DISCOVERY: False,
|
|
|
|
CONF_STATIC: [f"{MOCK_HOST}:{MOCK_PORT}"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
|
|
|
entity_entries = list(entity_reg.entities.values())
|
|
|
|
assert len(entity_entries) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_static_config_without_port(hass, pywemo_device):
|
|
|
|
"""Static device with host and no port is added and removed."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
CONF_DISCOVERY: False,
|
|
|
|
CONF_STATIC: [MOCK_HOST],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
|
|
|
entity_entries = list(entity_reg.entities.values())
|
|
|
|
assert len(entity_entries) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_static_config_with_invalid_host(hass):
|
|
|
|
"""Component setup fails if a static host is invalid."""
|
|
|
|
setup_success = await async_setup_component(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
CONF_DISCOVERY: False,
|
|
|
|
CONF_STATIC: [""],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert not setup_success
|
2020-12-24 11:25:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery(hass, pywemo_registry):
|
|
|
|
"""Verify that discovery dispatches devices to the platform for setup."""
|
|
|
|
|
2021-02-20 21:16:50 +00:00
|
|
|
def create_device(uuid, location):
|
2020-12-24 11:25:14 +00:00
|
|
|
"""Create a unique mock Motion detector device for each counter value."""
|
|
|
|
device = create_autospec(pywemo.Motion, instance=True)
|
2021-02-20 21:16:50 +00:00
|
|
|
device.host = location
|
|
|
|
device.port = MOCK_PORT
|
|
|
|
device.name = f"{MOCK_NAME}_{uuid}"
|
|
|
|
device.serialnumber = f"{MOCK_SERIAL_NUMBER}_{uuid}"
|
2020-12-24 11:25:14 +00:00
|
|
|
device.model_name = "Motion"
|
|
|
|
device.get_state.return_value = 0 # Default to Off
|
|
|
|
return device
|
|
|
|
|
2021-02-20 21:16:50 +00:00
|
|
|
def create_upnp_entry(counter):
|
|
|
|
return pywemo.ssdp.UPNPEntry.from_response(
|
|
|
|
"\r\n".join(
|
|
|
|
[
|
|
|
|
"",
|
|
|
|
f"LOCATION: http://192.168.1.100:{counter}/setup.xml",
|
|
|
|
f"USN: uuid:Socket-1_0-SERIAL{counter}::upnp:rootdevice",
|
|
|
|
"",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
upnp_entries = [create_upnp_entry(0), create_upnp_entry(1)]
|
2020-12-24 11:25:14 +00:00
|
|
|
# Setup the component and start discovery.
|
|
|
|
with patch(
|
2021-02-20 21:16:50 +00:00
|
|
|
"pywemo.discovery.device_from_uuid_and_location", side_effect=create_device
|
|
|
|
), patch("pywemo.ssdp.scan", return_value=upnp_entries) as mock_scan:
|
2020-12-24 11:25:14 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, DOMAIN, {DOMAIN: {CONF_DISCOVERY: True}}
|
|
|
|
)
|
|
|
|
await pywemo_registry.semaphore.acquire() # Returns after platform setup.
|
2021-02-20 21:16:50 +00:00
|
|
|
mock_scan.assert_called()
|
|
|
|
# Add two of the same entries to test deduplication.
|
|
|
|
upnp_entries.extend([create_upnp_entry(2), create_upnp_entry(2)])
|
2020-12-24 11:25:14 +00:00
|
|
|
|
|
|
|
# Test that discovery runs periodically and the async_dispatcher_send code works.
|
|
|
|
async_fire_time_changed(
|
|
|
|
hass,
|
|
|
|
dt.utcnow()
|
|
|
|
+ timedelta(seconds=WemoDiscovery.ADDITIONAL_SECONDS_BETWEEN_SCANS + 1),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Verify that the expected number of devices were setup.
|
|
|
|
entity_reg = await hass.helpers.entity_registry.async_get_registry()
|
|
|
|
entity_entries = list(entity_reg.entities.values())
|
|
|
|
assert len(entity_entries) == 3
|
|
|
|
|
|
|
|
# Verify that hass stops cleanly.
|
|
|
|
await hass.async_stop()
|
|
|
|
await hass.async_block_till_done()
|