Add additional data to zwave_js device statistics WS API (#72520)

* Add additional data to zwave_js device statistics WS API

* Rename variables

* fix logic

* correct typehint

* Update homeassistant/components/zwave_js/api.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* black

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Raman Gupta 2022-05-26 19:21:50 -04:00 committed by GitHub
parent d8295e2fad
commit 5e52b11050
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 16 deletions

View file

@ -71,6 +71,7 @@ from .const import (
from .helpers import (
async_enable_statistics,
async_get_node_from_device_id,
get_device_id,
update_data_collection_preference,
)
@ -2107,15 +2108,42 @@ async def websocket_subscribe_controller_statistics(
)
def _get_node_statistics_dict(statistics: NodeStatistics) -> dict[str, int]:
def _get_node_statistics_dict(
hass: HomeAssistant, statistics: NodeStatistics
) -> dict[str, Any]:
"""Get dictionary of node statistics."""
return {
dev_reg = dr.async_get(hass)
def _convert_node_to_device_id(node: Node) -> str:
"""Convert a node to a device id."""
driver = node.client.driver
assert driver
device = dev_reg.async_get_device({get_device_id(driver, node)})
assert device
return device.id
data: dict = {
"commands_tx": statistics.commands_tx,
"commands_rx": statistics.commands_rx,
"commands_dropped_tx": statistics.commands_dropped_tx,
"commands_dropped_rx": statistics.commands_dropped_rx,
"timeout_response": statistics.timeout_response,
"rtt": statistics.rtt,
"rssi": statistics.rssi,
"lwr": statistics.lwr.as_dict() if statistics.lwr else None,
"nlwr": statistics.nlwr.as_dict() if statistics.nlwr else None,
}
for key in ("lwr", "nlwr"):
if not data[key]:
continue
for key_2 in ("repeaters", "route_failed_between"):
if not data[key][key_2]:
continue
data[key][key_2] = [
_convert_node_to_device_id(node) for node in data[key][key_2]
]
return data
@websocket_api.require_admin
@ -2151,7 +2179,7 @@ async def websocket_subscribe_node_statistics(
"event": event["event"],
"source": "node",
"node_id": node.node_id,
**_get_node_statistics_dict(statistics),
**_get_node_statistics_dict(hass, statistics),
},
)
)
@ -2167,7 +2195,7 @@ async def websocket_subscribe_node_statistics(
"event": "statistics updated",
"source": "node",
"nodeId": node.node_id,
**_get_node_statistics_dict(node.statistics),
**_get_node_statistics_dict(hass, node.statistics),
},
)
)