mirror of
https://github.com/home-assistant/core.git
synced 2025-11-12 20:40:18 +00:00
Move Insteon configuration panel to config entry (#105581)
* Move Insteon panel to the config menu * Bump pyinsteon to 1.5.3 * Undo devcontainer.json changes * Bump Insteon frontend * Update config_flow.py * Code cleanup * Code review changes * Fix failing tests * Fix format * Remove unnecessary exception * codecov * Remove return from try * Fix merge mistake --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
@@ -18,48 +18,29 @@ from homeassistant.components.insteon.api.device import (
|
||||
TYPE,
|
||||
async_device_name,
|
||||
)
|
||||
from homeassistant.components.insteon.const import DOMAIN, MULTIPLE
|
||||
from homeassistant.components.insteon.const import (
|
||||
CONF_OVERRIDE,
|
||||
CONF_X10,
|
||||
DOMAIN,
|
||||
MULTIPLE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .const import MOCK_USER_INPUT_PLM
|
||||
from .mock_devices import MockDevices
|
||||
from .mock_setup import async_mock_setup
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.typing import WebSocketGenerator
|
||||
|
||||
|
||||
async def _async_setup(hass, hass_ws_client):
|
||||
"""Set up for tests."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
entry_id="abcde12345",
|
||||
data=MOCK_USER_INPUT_PLM,
|
||||
options={},
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
async_load_api(hass)
|
||||
|
||||
ws_client = await hass_ws_client(hass)
|
||||
devices = MockDevices()
|
||||
await devices.async_load()
|
||||
|
||||
dev_reg = dr.async_get(hass)
|
||||
# Create device registry entry for mock node
|
||||
ha_device = dev_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
identifiers={(DOMAIN, "11.11.11")},
|
||||
name="Device 11.11.11",
|
||||
)
|
||||
return ws_client, devices, ha_device, dev_reg
|
||||
|
||||
|
||||
async def test_get_device_api(
|
||||
async def test_get_config(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test getting an Insteon device."""
|
||||
|
||||
ws_client, devices, ha_device, _ = await _async_setup(hass, hass_ws_client)
|
||||
ws_client, devices, ha_device, _ = await async_mock_setup(hass, hass_ws_client)
|
||||
with patch.object(insteon.api.device, "devices", devices):
|
||||
await ws_client.send_json(
|
||||
{ID: 2, TYPE: "insteon/device/get", DEVICE_ID: ha_device.id}
|
||||
@@ -76,7 +57,7 @@ async def test_no_ha_device(
|
||||
) -> None:
|
||||
"""Test response when no HA device exists."""
|
||||
|
||||
ws_client, devices, _, _ = await _async_setup(hass, hass_ws_client)
|
||||
ws_client, devices, _, _ = await async_mock_setup(hass, hass_ws_client)
|
||||
with patch.object(insteon.api.device, "devices", devices):
|
||||
await ws_client.send_json(
|
||||
{ID: 2, TYPE: "insteon/device/get", DEVICE_ID: "not_a_device"}
|
||||
@@ -141,7 +122,7 @@ async def test_get_ha_device_name(
|
||||
) -> None:
|
||||
"""Test getting the HA device name from an Insteon address."""
|
||||
|
||||
_, devices, _, device_reg = await _async_setup(hass, hass_ws_client)
|
||||
_, devices, _, device_reg = await async_mock_setup(hass, hass_ws_client)
|
||||
|
||||
with patch.object(insteon.api.device, "devices", devices):
|
||||
# Test a real HA and Insteon device
|
||||
@@ -164,7 +145,7 @@ async def test_add_device_api(
|
||||
) -> None:
|
||||
"""Test adding an Insteon device."""
|
||||
|
||||
ws_client, devices, _, _ = await _async_setup(hass, hass_ws_client)
|
||||
ws_client, devices, _, _ = await async_mock_setup(hass, hass_ws_client)
|
||||
with patch.object(insteon.api.device, "devices", devices):
|
||||
await ws_client.send_json({ID: 2, TYPE: "insteon/device/add", MULTIPLE: True})
|
||||
|
||||
@@ -194,7 +175,7 @@ async def test_cancel_add_device(
|
||||
) -> None:
|
||||
"""Test cancelling adding of a new device."""
|
||||
|
||||
ws_client, devices, _, _ = await _async_setup(hass, hass_ws_client)
|
||||
ws_client, devices, _, _ = await async_mock_setup(hass, hass_ws_client)
|
||||
|
||||
with patch.object(insteon.api.aldb, "devices", devices):
|
||||
await ws_client.send_json(
|
||||
@@ -205,3 +186,127 @@ async def test_cancel_add_device(
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["success"]
|
||||
|
||||
|
||||
async def test_add_x10_device(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test adding an X10 device."""
|
||||
|
||||
ws_client, _, _, _ = await async_mock_setup(hass, hass_ws_client)
|
||||
x10_device = {"housecode": "a", "unitcode": 1, "platform": "switch"}
|
||||
await ws_client.send_json(
|
||||
{ID: 2, TYPE: "insteon/device/add_x10", "x10_device": x10_device}
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["success"]
|
||||
|
||||
config_entry = hass.config_entries.async_get_entry("abcde12345")
|
||||
assert len(config_entry.options[CONF_X10]) == 1
|
||||
assert config_entry.options[CONF_X10][0]["housecode"] == "a"
|
||||
assert config_entry.options[CONF_X10][0]["unitcode"] == 1
|
||||
assert config_entry.options[CONF_X10][0]["platform"] == "switch"
|
||||
|
||||
|
||||
async def test_add_x10_device_duplicate(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test adding a duplicate X10 device."""
|
||||
|
||||
x10_device = {"housecode": "a", "unitcode": 1, "platform": "switch"}
|
||||
|
||||
ws_client, _, _, _ = await async_mock_setup(
|
||||
hass, hass_ws_client, config_options={CONF_X10: [x10_device]}
|
||||
)
|
||||
await ws_client.send_json(
|
||||
{ID: 2, TYPE: "insteon/device/add_x10", "x10_device": x10_device}
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["error"]
|
||||
assert msg["error"]["code"] == "duplicate"
|
||||
|
||||
|
||||
async def test_remove_device(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test removing an Insteon device."""
|
||||
ws_client, _, _, _ = await async_mock_setup(hass, hass_ws_client)
|
||||
await ws_client.send_json(
|
||||
{
|
||||
ID: 2,
|
||||
TYPE: "insteon/device/remove",
|
||||
"device_address": "11.22.33",
|
||||
"remove_all_refs": True,
|
||||
}
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["success"]
|
||||
|
||||
|
||||
async def test_remove_x10_device(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test removing an X10 device."""
|
||||
ws_client, _, _, _ = await async_mock_setup(hass, hass_ws_client)
|
||||
await ws_client.send_json(
|
||||
{
|
||||
ID: 2,
|
||||
TYPE: "insteon/device/remove",
|
||||
"device_address": "X10.A.01",
|
||||
"remove_all_refs": True,
|
||||
}
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["success"]
|
||||
|
||||
|
||||
async def test_remove_one_x10_device(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test one X10 device without removing others."""
|
||||
x10_device = {"housecode": "a", "unitcode": 1, "platform": "light", "dim_steps": 22}
|
||||
x10_devices = [
|
||||
x10_device,
|
||||
{"housecode": "a", "unitcode": 2, "platform": "switch"},
|
||||
]
|
||||
ws_client, _, _, _ = await async_mock_setup(
|
||||
hass, hass_ws_client, config_options={CONF_X10: x10_devices}
|
||||
)
|
||||
await ws_client.send_json(
|
||||
{
|
||||
ID: 2,
|
||||
TYPE: "insteon/device/remove",
|
||||
"device_address": "X10.A.01",
|
||||
"remove_all_refs": True,
|
||||
}
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["success"]
|
||||
config_entry = hass.config_entries.async_get_entry("abcde12345")
|
||||
assert len(config_entry.options[CONF_X10]) == 1
|
||||
assert config_entry.options[CONF_X10][0]["housecode"] == "a"
|
||||
assert config_entry.options[CONF_X10][0]["unitcode"] == 2
|
||||
|
||||
|
||||
async def test_remove_device_with_overload(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test removing an Insteon device that has a device overload."""
|
||||
overload = {"address": "99.99.99", "cat": 1, "subcat": 3}
|
||||
overloads = {CONF_OVERRIDE: [overload]}
|
||||
ws_client, _, _, _ = await async_mock_setup(
|
||||
hass, hass_ws_client, config_options=overloads
|
||||
)
|
||||
await ws_client.send_json(
|
||||
{
|
||||
ID: 2,
|
||||
TYPE: "insteon/device/remove",
|
||||
"device_address": "99.99.99",
|
||||
"remove_all_refs": True,
|
||||
}
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["success"]
|
||||
|
||||
config_entry = hass.config_entries.async_get_entry("abcde12345")
|
||||
assert not config_entry.options.get(CONF_OVERRIDE)
|
||||
|
||||
Reference in New Issue
Block a user