2020-12-01 18:26:43 +00:00
|
|
|
"""Test configuration for Shelly."""
|
2021-01-11 15:45:06 +00:00
|
|
|
from unittest.mock import AsyncMock, Mock, patch
|
2020-12-01 18:26:43 +00:00
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
import pytest
|
2020-12-01 18:26:43 +00:00
|
|
|
|
2021-01-04 22:10:42 +00:00
|
|
|
from homeassistant.components.shelly import ShellyDeviceWrapper
|
|
|
|
from homeassistant.components.shelly.const import (
|
|
|
|
COAP,
|
|
|
|
DATA_CONFIG_ENTRY,
|
|
|
|
DOMAIN,
|
|
|
|
EVENT_SHELLY_CLICK,
|
|
|
|
)
|
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
2021-02-26 21:28:52 +00:00
|
|
|
from tests.common import (
|
|
|
|
MockConfigEntry,
|
|
|
|
async_capture_events,
|
|
|
|
async_mock_service,
|
|
|
|
mock_device_registry,
|
|
|
|
)
|
2021-01-04 22:10:42 +00:00
|
|
|
|
|
|
|
MOCK_SETTINGS = {
|
|
|
|
"name": "Test name",
|
2021-01-11 15:45:06 +00:00
|
|
|
"mode": "relay",
|
2021-01-04 22:10:42 +00:00
|
|
|
"device": {
|
|
|
|
"mac": "test-mac",
|
|
|
|
"hostname": "test-host",
|
|
|
|
"type": "SHSW-25",
|
|
|
|
"num_outputs": 2,
|
|
|
|
},
|
|
|
|
"coiot": {"update_period": 15},
|
|
|
|
"fw": "20201124-092159/v1.9.0@57ac4ad8",
|
|
|
|
"relays": [{"btn_type": "momentary"}, {"btn_type": "toggle"}],
|
2021-01-11 15:45:06 +00:00
|
|
|
"rollers": [{"positioning": True}],
|
2021-01-04 22:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MOCK_BLOCKS = [
|
2021-01-11 15:45:06 +00:00
|
|
|
Mock(
|
|
|
|
sensor_ids={"inputEvent": "S", "inputEventCnt": 2},
|
|
|
|
channel="0",
|
|
|
|
type="relay",
|
|
|
|
set_state=AsyncMock(side_effect=lambda turn: {"ison": turn == "on"}),
|
|
|
|
),
|
|
|
|
Mock(
|
|
|
|
sensor_ids={"roller": "stop", "rollerPos": 0},
|
|
|
|
channel="1",
|
|
|
|
type="roller",
|
|
|
|
set_state=AsyncMock(
|
|
|
|
side_effect=lambda go, roller_pos=0: {
|
|
|
|
"current_pos": roller_pos,
|
|
|
|
"state": go,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
),
|
2021-01-04 22:10:42 +00:00
|
|
|
]
|
|
|
|
|
2020-12-01 18:26:43 +00:00
|
|
|
|
2021-01-11 15:45:06 +00:00
|
|
|
MOCK_SHELLY = {
|
|
|
|
"mac": "test-mac",
|
|
|
|
"auth": False,
|
|
|
|
"fw": "20201124-092854/v1.9.0@57ac4ad8",
|
|
|
|
"num_outputs": 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-01 18:26:43 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_coap():
|
|
|
|
"""Mock out coap."""
|
|
|
|
with patch("homeassistant.components.shelly.get_coap_context"):
|
|
|
|
yield
|
2021-01-04 22:10:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def device_reg(hass):
|
|
|
|
"""Return an empty, loaded, registry."""
|
|
|
|
return mock_device_registry(hass)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def calls(hass):
|
|
|
|
"""Track calls to a mock service."""
|
|
|
|
return async_mock_service(hass, "test", "automation")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def events(hass):
|
|
|
|
"""Yield caught shelly_click events."""
|
2021-02-26 21:28:52 +00:00
|
|
|
return async_capture_events(hass, EVENT_SHELLY_CLICK)
|
2021-01-04 22:10:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def coap_wrapper(hass):
|
|
|
|
"""Setups a coap wrapper with mocked device."""
|
|
|
|
await async_setup_component(hass, "shelly", {})
|
|
|
|
|
2021-02-03 16:03:22 +00:00
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={"sleep_period": 0, "model": "SHSW-25"},
|
|
|
|
unique_id="12345678",
|
|
|
|
)
|
2021-01-04 22:10:42 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
2021-01-11 15:45:06 +00:00
|
|
|
device = Mock(
|
|
|
|
blocks=MOCK_BLOCKS,
|
|
|
|
settings=MOCK_SETTINGS,
|
|
|
|
shelly=MOCK_SHELLY,
|
|
|
|
update=AsyncMock(),
|
2021-02-03 16:03:22 +00:00
|
|
|
initialized=True,
|
2021-01-11 15:45:06 +00:00
|
|
|
)
|
2021-01-04 22:10:42 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}}
|
|
|
|
hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id] = {}
|
|
|
|
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][
|
|
|
|
COAP
|
|
|
|
] = ShellyDeviceWrapper(hass, config_entry, device)
|
|
|
|
|
|
|
|
await wrapper.async_setup()
|
|
|
|
|
|
|
|
return wrapper
|