mirror of
https://github.com/home-assistant/core.git
synced 2026-09-13 04:01:03 +01:00
Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
"""Websocket API exposing the Modbus connections the integration keeps open."""
|
|
|
|
from typing import Any, Final
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components import websocket_api
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from .connection import async_get_connection_info
|
|
|
|
TYPE_LIST_CONNECTIONS: Final = "modbus/connections/list"
|
|
|
|
|
|
@callback
|
|
def async_setup(hass: HomeAssistant) -> None:
|
|
"""Register the Modbus websocket commands."""
|
|
websocket_api.async_register_command(hass, websocket_list_connections)
|
|
|
|
|
|
@websocket_api.require_admin
|
|
@websocket_api.websocket_command({vol.Required("type"): TYPE_LIST_CONNECTIONS})
|
|
@callback
|
|
def websocket_list_connections(
|
|
hass: HomeAssistant,
|
|
connection: websocket_api.ActiveConnection,
|
|
msg: dict[str, Any],
|
|
) -> None:
|
|
"""List the connections, and which config entries hold units on each."""
|
|
connection.send_result(
|
|
msg["id"],
|
|
{
|
|
"connections": [
|
|
{
|
|
"endpoint": list(info.endpoint),
|
|
"connected": info.connected,
|
|
"units": info.units,
|
|
}
|
|
for info in async_get_connection_info(hass)
|
|
]
|
|
},
|
|
)
|