Add WebSocket API Foundation for zwave_js (#45151)
This commit is contained in:
parent
071c8cc67d
commit
9dbf14188a
4 changed files with 91 additions and 1 deletions
|
@ -16,6 +16,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|||
|
||||
from .const import DATA_CLIENT, DATA_UNSUBSCRIBE, DOMAIN, PLATFORMS
|
||||
from .discovery import async_discover_values
|
||||
from .websocket_api import async_register_api
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
CONNECT_TIMEOUT = 10
|
||||
|
@ -127,6 +128,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
DATA_UNSUBSCRIBE: unsubs,
|
||||
}
|
||||
|
||||
# Set up websocket API
|
||||
async_register_api(hass)
|
||||
|
||||
async def start_platforms() -> None:
|
||||
"""Start platforms and perform discovery."""
|
||||
# wait until all required platforms are ready
|
||||
|
|
52
homeassistant/components/zwave_js/websocket_api.py
Normal file
52
homeassistant/components/zwave_js/websocket_api.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
"""Websocket API for Z-Wave JS."""
|
||||
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.components.websocket_api.connection import ActiveConnection
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from .const import DATA_CLIENT, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ID = "id"
|
||||
ENTRY_ID = "entry_id"
|
||||
TYPE = "type"
|
||||
|
||||
|
||||
@callback
|
||||
def async_register_api(hass: HomeAssistant) -> None:
|
||||
"""Register all of our api endpoints."""
|
||||
websocket_api.async_register_command(hass, websocket_network_status)
|
||||
|
||||
|
||||
@websocket_api.require_admin
|
||||
@websocket_api.websocket_command(
|
||||
{vol.Required(TYPE): "zwave_js/network_status", vol.Required(ENTRY_ID): str}
|
||||
)
|
||||
@callback
|
||||
def websocket_network_status(
|
||||
hass: HomeAssistant, connection: ActiveConnection, msg: dict
|
||||
) -> None:
|
||||
"""Get the status of the Z-Wave JS network."""
|
||||
entry_id = msg[ENTRY_ID]
|
||||
client = hass.data[DOMAIN][entry_id][DATA_CLIENT]
|
||||
data = {
|
||||
"client": {
|
||||
"ws_server_url": client.ws_server_url,
|
||||
"state": client.state,
|
||||
"driver_version": client.version.driver_version,
|
||||
"server_version": client.version.server_version,
|
||||
},
|
||||
"controller": {
|
||||
"home_id": client.driver.controller.data["homeId"],
|
||||
"node_count": len(client.driver.controller.nodes),
|
||||
},
|
||||
}
|
||||
connection.send_result(
|
||||
msg[ID],
|
||||
data,
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue