Adjust zwave_js WS API commands for logging (#51096)
This commit is contained in:
parent
06c2e541c4
commit
1e18011603
2 changed files with 72 additions and 32 deletions
|
@ -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),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -1553,15 +1553,15 @@ async def test_view_invalid_node_id(integration, hass_client, method, url):
|
|||
assert resp.status == 404
|
||||
|
||||
|
||||
async def test_subscribe_logs(hass, integration, client, hass_ws_client):
|
||||
"""Test the subscribe_logs websocket command."""
|
||||
async def test_subscribe_log_updates(hass, integration, client, hass_ws_client):
|
||||
"""Test the subscribe_log_updates websocket command."""
|
||||
entry = integration
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
client.async_send_command.return_value = {}
|
||||
|
||||
await ws_client.send_json(
|
||||
{ID: 1, TYPE: "zwave_js/subscribe_logs", ENTRY_ID: entry.entry_id}
|
||||
{ID: 1, TYPE: "zwave_js/subscribe_log_updates", ENTRY_ID: entry.entry_id}
|
||||
)
|
||||
|
||||
msg = await ws_client.receive_json()
|
||||
|
@ -1588,10 +1588,41 @@ async def test_subscribe_logs(hass, integration, client, hass_ws_client):
|
|||
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["event"] == {
|
||||
"message": ["test"],
|
||||
"level": "debug",
|
||||
"primary_tags": "tag",
|
||||
"timestamp": "time",
|
||||
"type": "log_message",
|
||||
"log_message": {
|
||||
"message": ["test"],
|
||||
"level": "debug",
|
||||
"primary_tags": "tag",
|
||||
"timestamp": "time",
|
||||
},
|
||||
}
|
||||
|
||||
event = Event(
|
||||
type="log config updated",
|
||||
data={
|
||||
"source": "driver",
|
||||
"event": "log config updated",
|
||||
"config": {
|
||||
"enabled": False,
|
||||
"level": "error",
|
||||
"logToFile": True,
|
||||
"filename": "test",
|
||||
"forceConsole": True,
|
||||
},
|
||||
},
|
||||
)
|
||||
client.driver.receive_event(event)
|
||||
|
||||
msg = await ws_client.receive_json()
|
||||
assert msg["event"] == {
|
||||
"type": "log_config",
|
||||
"log_config": {
|
||||
"enabled": False,
|
||||
"level": "error",
|
||||
"log_to_file": True,
|
||||
"filename": "test",
|
||||
"force_console": True,
|
||||
},
|
||||
}
|
||||
|
||||
# Test sending command with not loaded entry fails
|
||||
|
@ -1599,7 +1630,7 @@ async def test_subscribe_logs(hass, integration, client, hass_ws_client):
|
|||
await hass.async_block_till_done()
|
||||
|
||||
await ws_client.send_json(
|
||||
{ID: 2, TYPE: "zwave_js/subscribe_logs", ENTRY_ID: entry.entry_id}
|
||||
{ID: 2, TYPE: "zwave_js/subscribe_log_updates", ENTRY_ID: entry.entry_id}
|
||||
)
|
||||
msg = await ws_client.receive_json()
|
||||
|
||||
|
@ -1750,16 +1781,6 @@ async def test_get_log_config(hass, client, integration, hass_ws_client):
|
|||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
# Test we can get log configuration
|
||||
client.async_send_command.return_value = {
|
||||
"success": True,
|
||||
"config": {
|
||||
"enabled": True,
|
||||
"level": "error",
|
||||
"logToFile": False,
|
||||
"filename": "/test.txt",
|
||||
"forceConsole": False,
|
||||
},
|
||||
}
|
||||
await ws_client.send_json(
|
||||
{
|
||||
ID: 1,
|
||||
|
@ -1773,9 +1794,9 @@ async def test_get_log_config(hass, client, integration, hass_ws_client):
|
|||
|
||||
log_config = msg["result"]
|
||||
assert log_config["enabled"]
|
||||
assert log_config["level"] == LogLevel.ERROR
|
||||
assert log_config["level"] == LogLevel.INFO
|
||||
assert log_config["log_to_file"] is False
|
||||
assert log_config["filename"] == "/test.txt"
|
||||
assert log_config["filename"] == ""
|
||||
assert log_config["force_console"] is False
|
||||
|
||||
# Test sending command with not loaded entry fails
|
||||
|
|
Loading…
Add table
Reference in a new issue