Adjust zwave_js WS API commands for logging (#51096)

This commit is contained in:
Raman Gupta 2021-06-17 11:19:25 -04:00 committed by GitHub
parent 06c2e541c4
commit 1e18011603
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 32 deletions

View file

@ -159,7 +159,7 @@ def async_register_api(hass: HomeAssistant) -> None:
websocket_api.async_register_command(hass, websocket_heal_node)
websocket_api.async_register_command(hass, websocket_set_config_parameter)
websocket_api.async_register_command(hass, websocket_get_config_parameters)
websocket_api.async_register_command(hass, websocket_subscribe_logs)
websocket_api.async_register_command(hass, websocket_subscribe_log_updates)
websocket_api.async_register_command(hass, websocket_update_log_config)
websocket_api.async_register_command(hass, websocket_get_log_config)
websocket_api.async_register_command(
@ -1022,13 +1022,13 @@ def filename_is_present_if_logging_to_file(obj: dict) -> dict:
@websocket_api.require_admin
@websocket_api.websocket_command(
{
vol.Required(TYPE): "zwave_js/subscribe_logs",
vol.Required(TYPE): "zwave_js/subscribe_log_updates",
vol.Required(ENTRY_ID): str,
}
)
@websocket_api.async_response
@async_get_entry
async def websocket_subscribe_logs(
async def websocket_subscribe_log_updates(
hass: HomeAssistant,
connection: ActiveConnection,
msg: dict,
@ -1042,24 +1042,44 @@ async def websocket_subscribe_logs(
def async_cleanup() -> None:
"""Remove signal listeners."""
hass.async_create_task(driver.async_stop_listening_logs())
unsub()
for unsub in unsubs:
unsub()
@callback
def forward_event(event: dict) -> None:
def log_messages(event: dict) -> None:
log_msg: LogMessage = event["log_message"]
connection.send_message(
websocket_api.event_message(
msg[ID],
{
"timestamp": log_msg.timestamp,
"level": log_msg.level,
"primary_tags": log_msg.primary_tags,
"message": log_msg.formatted_message,
"type": "log_message",
"log_message": {
"timestamp": log_msg.timestamp,
"level": log_msg.level,
"primary_tags": log_msg.primary_tags,
"message": log_msg.formatted_message,
},
},
)
)
unsub = driver.on("logging", forward_event)
@callback
def log_config_updates(event: dict) -> None:
log_config: LogConfig = event["log_config"]
connection.send_message(
websocket_api.event_message(
msg[ID],
{
"type": "log_config",
"log_config": dataclasses.asdict(log_config),
},
)
)
unsubs = [
driver.on("logging", log_messages),
driver.on("log config updated", log_config_updates),
]
connection.subscriptions[msg["id"]] = async_cleanup
await driver.async_start_listening_logs()
@ -1126,10 +1146,9 @@ async def websocket_get_log_config(
client: Client,
) -> None:
"""Get log configuration for the Z-Wave JS driver."""
result = await client.driver.async_get_log_config()
connection.send_result(
msg[ID],
dataclasses.asdict(result),
dataclasses.asdict(client.driver.log_config),
)