Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom panel for LCN configuration #108664

Merged
merged 22 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use entry_id instead of host_id
  • Loading branch information
alengwenus committed Aug 16, 2024
commit 50210a12d712185ffd04d80659710b617a3487cc
18 changes: 9 additions & 9 deletions homeassistant/components/lcn/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def get_entry(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict
) -> None:
"""Get config_entry."""
if not (config_entry := hass.config_entries.async_get_entry(msg["host_id"])):
if not (config_entry := hass.config_entries.async_get_entry(msg["entry_id"])):
connection.send_result(msg["id"], False)
else:
await func(hass, connection, msg, config_entry)
Expand All @@ -114,7 +114,7 @@ async def get_entry(

@websocket_api.require_admin
@websocket_api.websocket_command(
{vol.Required("type"): "lcn/devices", vol.Required("host_id"): cv.string}
{vol.Required("type"): "lcn/devices", vol.Required("entry_id"): cv.string}
)
@websocket_api.async_response
@get_config_entry
Expand All @@ -132,7 +132,7 @@ async def websocket_get_device_configs(
@websocket_api.websocket_command(
{
vol.Required("type"): "lcn/entities",
vol.Required("host_id"): cv.string,
vol.Required("entry_id"): cv.string,
vol.Optional(CONF_ADDRESS): ADDRESS_SCHEMA,
}
)
Expand Down Expand Up @@ -172,7 +172,7 @@ async def websocket_get_entity_configs(

@websocket_api.require_admin
@websocket_api.websocket_command(
{vol.Required("type"): "lcn/devices/scan", vol.Required("host_id"): cv.string}
{vol.Required("type"): "lcn/devices/scan", vol.Required("entry_id"): cv.string}
)
@websocket_api.async_response
@get_config_entry
Expand Down Expand Up @@ -202,7 +202,7 @@ async def websocket_scan_devices(
@websocket_api.websocket_command(
{
vol.Required("type"): "lcn/devices/add",
vol.Required("host_id"): cv.string,
vol.Required("entry_id"): cv.string,
vol.Required(CONF_ADDRESS): ADDRESS_SCHEMA,
}
)
Expand Down Expand Up @@ -248,7 +248,7 @@ async def websocket_add_device(
@websocket_api.websocket_command(
{
vol.Required("type"): "lcn/devices/delete",
vol.Required("host_id"): cv.string,
vol.Required("entry_id"): cv.string,
vol.Required(CONF_ADDRESS): ADDRESS_SCHEMA,
}
)
Expand Down Expand Up @@ -299,7 +299,7 @@ async def websocket_delete_device(
@websocket_api.websocket_command(
{
vol.Required("type"): "lcn/entities/add",
vol.Required("host_id"): cv.string,
vol.Required("entry_id"): cv.string,
vol.Required(CONF_ADDRESS): ADDRESS_SCHEMA,
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_DOMAIN): cv.string,
Expand Down Expand Up @@ -350,7 +350,7 @@ async def websocket_add_entity(
}

# Create new entity and add to corresponding component
callbacks = hass.data[DOMAIN][msg["host_id"]][ADD_ENTITIES_CALLBACKS]
callbacks = hass.data[DOMAIN][msg["entry_id"]][ADD_ENTITIES_CALLBACKS]
async_add_entities, create_lcn_entity = callbacks[msg[CONF_DOMAIN]]

entity = create_lcn_entity(hass, entity_config, config_entry)
Expand All @@ -370,7 +370,7 @@ async def websocket_add_entity(
@websocket_api.websocket_command(
{
vol.Required("type"): "lcn/entities/delete",
vol.Required("host_id"): cv.string,
vol.Required("entry_id"): cv.string,
vol.Required(CONF_ADDRESS): ADDRESS_SCHEMA,
vol.Required(CONF_DOMAIN): cv.string,
vol.Required(CONF_RESOURCE): cv.string,
Expand Down
70 changes: 37 additions & 33 deletions tests/components/lcn/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,47 @@
CONF_DEVICES,
CONF_DOMAIN,
CONF_ENTITIES,
CONF_ID,
CONF_NAME,
CONF_RESOURCE,
CONF_TYPE,
)
from homeassistant.core import HomeAssistant

from tests.typing import WebSocketGenerator

DEVICES_PAYLOAD = {"id": 42, "type": "lcn/devices", "host_id": ""}
DEVICES_PAYLOAD = {CONF_ID: 42, CONF_TYPE: "lcn/devices", "entry_id": ""}
ENTITIES_PAYLOAD = {
"id": 42,
"type": "lcn/entities",
"host_id": "",
CONF_ID: 42,
alengwenus marked this conversation as resolved.
Show resolved Hide resolved
CONF_TYPE: "lcn/entities",
"entry_id": "",
}
SCAN_PAYLOAD = {"id": 42, "type": "lcn/devices/scan", "host_id": ""}
SCAN_PAYLOAD = {CONF_ID: 42, CONF_TYPE: "lcn/devices/scan", "entry_id": ""}
DEVICES_ADD_PAYLOAD = {
"id": 42,
"type": "lcn/devices/add",
"host_id": "",
CONF_ID: 42,
CONF_TYPE: "lcn/devices/add",
"entry_id": "",
CONF_ADDRESS: (0, 10, False),
}
DEVICES_DELETE_PAYLOAD = {
"id": 42,
"type": "lcn/devices/delete",
"host_id": "",
CONF_ID: 42,
CONF_TYPE: "lcn/devices/delete",
"entry_id": "",
CONF_ADDRESS: (0, 7, False),
}
ENTITIES_ADD_PAYLOAD = {
"id": 42,
"type": "lcn/entities/add",
"host_id": "",
CONF_ID: 42,
CONF_TYPE: "lcn/entities/add",
"entry_id": "",
CONF_ADDRESS: (0, 7, False),
CONF_NAME: "test_switch",
CONF_DOMAIN: "switch",
CONF_DOMAIN_DATA: {"output": "RELAY5"},
}
ENTITIES_DELETE_PAYLOAD = {
"id": 42,
"type": "lcn/entities/delete",
"host_id": "",
CONF_ID: 42,
CONF_TYPE: "lcn/entities/delete",
"entry_id": "",
CONF_ADDRESS: (0, 7, False),
CONF_DOMAIN: "switch",
CONF_RESOURCE: "relay1",
Expand All @@ -61,7 +63,7 @@ async def test_lcn_devices_command(
"""Test lcn/devices command."""
client = await hass_ws_client(hass)

await client.send_json({**DEVICES_PAYLOAD, "host_id": entry.entry_id})
await client.send_json({**DEVICES_PAYLOAD, "entry_id": entry.entry_id})

res = await client.receive_json()
assert res["success"], res
Expand Down Expand Up @@ -92,7 +94,7 @@ async def test_lcn_entities_command(
await client.send_json(
{
**payload,
"host_id": entry.entry_id,
"entry_id": entry.entry_id,
}
)

Expand All @@ -101,7 +103,7 @@ async def test_lcn_entities_command(
entities = [
entity
for entity in entry.data[CONF_ENTITIES]
if CONF_ADDRESS not in payload or entity["address"] == payload[CONF_ADDRESS]
if CONF_ADDRESS not in payload or entity[CONF_ADDRESS] == payload[CONF_ADDRESS]
]
assert len(res["result"]) == len(entities)
assert all(
Expand All @@ -118,7 +120,7 @@ async def test_lcn_devices_scan_command(
lcn_connection.get_address_conn(LcnAddr(0, 10, False))

client = await hass_ws_client(hass)
await client.send_json({**SCAN_PAYLOAD, "host_id": entry.entry_id})
await client.send_json({**SCAN_PAYLOAD, "entry_id": entry.entry_id})

res = await client.receive_json()
assert res["success"], res
Expand All @@ -139,7 +141,7 @@ async def test_lcn_devices_add_command(
client = await hass_ws_client(hass)
assert get_device_config((0, 10, False), entry) is None

await client.send_json({**DEVICES_ADD_PAYLOAD, "host_id": entry.entry_id})
await client.send_json({**DEVICES_ADD_PAYLOAD, "entry_id": entry.entry_id})

res = await client.receive_json()
assert res["success"], res
Expand All @@ -154,7 +156,7 @@ async def test_lcn_devices_delete_command(
client = await hass_ws_client(hass)
assert get_device_config((0, 7, False), entry)

await client.send_json({**DEVICES_DELETE_PAYLOAD, "host_id": entry.entry_id})
await client.send_json({**DEVICES_DELETE_PAYLOAD, "entry_id": entry.entry_id})

res = await client.receive_json()
assert res["success"], res
Expand All @@ -173,17 +175,17 @@ async def test_lcn_entities_add_command(
}

resource = get_resource(
ENTITIES_ADD_PAYLOAD["domain"], ENTITIES_ADD_PAYLOAD["domain_data"]
ENTITIES_ADD_PAYLOAD[CONF_DOMAIN], ENTITIES_ADD_PAYLOAD[CONF_DOMAIN_DATA]
).lower()

assert {**entity_config, "resource": resource} not in entry.data[CONF_ENTITIES]
assert {**entity_config, CONF_RESOURCE: resource} not in entry.data[CONF_ENTITIES]

await client.send_json({**ENTITIES_ADD_PAYLOAD, "host_id": entry.entry_id})
await client.send_json({**ENTITIES_ADD_PAYLOAD, "entry_id": entry.entry_id})

res = await client.receive_json()
assert res["success"], res

assert {**entity_config, "resource": resource} in entry.data[CONF_ENTITIES]
assert {**entity_config, CONF_RESOURCE: resource} in entry.data[CONF_ENTITIES]


async def test_lcn_entities_delete_command(
Expand All @@ -205,7 +207,7 @@ async def test_lcn_entities_delete_command(
== 1
)

await client.send_json({**ENTITIES_DELETE_PAYLOAD, "host_id": entry.entry_id})
await client.send_json({**ENTITIES_DELETE_PAYLOAD, "entry_id": entry.entry_id})

res = await client.receive_json()
assert res["success"], res
Expand All @@ -225,7 +227,7 @@ async def test_lcn_entities_delete_command(


@pytest.mark.parametrize(
("payload", "host_id", "result"),
("payload", "entity_id", "result"),
[
(DEVICES_PAYLOAD, "12345", False),
(ENTITIES_PAYLOAD, "12345", False),
Expand All @@ -241,12 +243,12 @@ async def test_lcn_command_host_error(
hass_ws_client: WebSocketGenerator,
lcn_connection,
payload,
host_id,
entity_id,
result,
) -> None:
"""Test lcn commands for unknown host."""
client = await hass_ws_client(hass)
await client.send_json({**payload, "host_id": host_id})
await client.send_json({**payload, "entry_id": entity_id})

res = await client.receive_json()
assert res["success"], res
Expand All @@ -273,7 +275,9 @@ async def test_lcn_command_address_error(
) -> None:
"""Test lcn commands for address error."""
client = await hass_ws_client(hass)
await client.send_json({**payload, "host_id": entry.entry_id, "address": address})
await client.send_json(
{**payload, "entry_id": entry.entry_id, CONF_ADDRESS: address}
)

res = await client.receive_json()
assert res["success"], res
Expand All @@ -291,7 +295,7 @@ async def test_lcn_entities_add_existing_error(
await client.send_json(
{
**ENTITIES_ADD_PAYLOAD,
"host_id": entry.entry_id,
"entry_id": entry.entry_id,
CONF_DOMAIN_DATA: {"output": "RELAY1"},
}
)
Expand Down