Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions zha-network-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,54 @@ class ZHANetworkCard extends HTMLElement {
});
}

set hass(hass) {
const config = this._config;
const root = this.shadowRoot;

hass
async init(hass, config, root) {
await hass
.callWS({
type: "zha/devices",
})
.then((devices) => {
.then(async (devices) => {
// `raw_rows` to be filled with data here, due to 'attr_as_list' it is possible to have
// multiple data `raw_rows` acquired into one cell(.raw_data), so re-iterate all rows
// to---if applicable---spawn new DataRowZHA objects for these accordingly
let raw_rows = devices.map(
const raw_rows = devices.map(
(e) => new DataRowZHA({ attributes: e }, config.strict)
);
raw_rows.forEach((e) => e.get_raw_data(config.columns));
const read_sw_build_id = config.columns.filter((col) => col.prop == "sw_build_id");
await Promise.allSettled(raw_rows.map(async (e) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This blocks the rendering of the table until all cluster attribute requests are done (successes and fails).

This is needed as the rendering currently does not accept promises, only actual values.

if (read_sw_build_id && e.device.attributes.available) {
// retrieving cluster attributes requires additional ws calls
await hass.callWS({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These hass.callWS() invocations of zha/devices/clusters and zha/devices/clusters/attributes/value types could probably be cached to avoid hammering the zigbee network and devices.

type: "zha/devices/clusters",
ieee: e.device.attributes.ieee
}).then(async (clusters) => {
for (const cluster of clusters) {
if (cluster["type"] == "in" && cluster["name"] == "Basic") {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be made dynamic to support other clusters.

e.device.sw_build_id = await hass.callWS({
type: "zha/devices/clusters/attributes/value",
"ieee": e.device.attributes.ieee,
"endpoint_id": cluster.endpoint_id,
"cluster_id": cluster.id,
"cluster_type": "in",
"attribute": 0x4000,
Copy link
Author

@pdecat pdecat Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be made dynamic to support other attributes.

}).then((value) => {
console.log("DEBUG: %s (%s) has sw_build_id %s", e.device.attributes.ieee, JSON.stringify(e.device.attributes.user_given_name || e.device.attributes.name), value)
return value
}).catch((error) => {
console.log("DEBUG: failed to retrieve sw_build_id for device %s: %s", e.device.attributes.ieee, JSON.stringify(error))
return error.message
});
}
}
}).catch((error) => {
console.log("DEBUG: failed to retrieve clusters for device %s: %s", e.device.attributes.ieee, JSON.stringify(error))
return error.message
});
} else {
console.log("DEBUG: %s", JSON.stringify(error))
e.device.sw_build_id = "N/A"
}
e.get_raw_data(config.columns)
}));

// now add() the raw_data rows to the DataTableZHA
this.tbl.clear_rows();
Expand All @@ -350,6 +382,17 @@ class ZHANetworkCard extends HTMLElement {
});
}

set hass(hass) {
console.log("DEBUG: set hass(hass) is called")
const config = this._config;
const root = this.shadowRoot;

if (!this.initialized) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary to avoid hammering the zigbee devices with actual cluster attribute requests as set hass(hass) is invoked very often.

this.initialized = true
this.init(hass, config, root);
}
}

_setCardSize(num_rows) {
this.card_height = parseInt(num_rows * 0.5);
}
Expand Down