-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add network visualization to the ZHA config panel #7802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bramkragten
merged 14 commits into
home-assistant:dev
from
dmulcahey:dm/zha-network-visualization
Nov 26, 2020
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5469305
initial commit
dmulcahey 34ccd9b
review comment
dmulcahey 312118a
review comment
dmulcahey f7cd44a
review comment
dmulcahey 2a73a37
review comment
dmulcahey bc4a824
remove ha-config-section
dmulcahey ea83bfc
Update src/panels/config/integrations/integration-panels/zha/zha-conf…
dmulcahey bac38a0
Update src/panels/config/integrations/integration-panels/zha/zha-netw…
dmulcahey cfeceb6
Update src/panels/config/integrations/integration-panels/zha/zha-netw…
dmulcahey dfd9020
clean up after committing suggestions
dmulcahey 2e6234f
update translation
dmulcahey 0449c09
review comments
dmulcahey ed0617e
type info
dmulcahey d128e24
commit lock file
dmulcahey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
src/panels/config/integrations/integration-panels/zha/zha-network-visualization-page.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| import { | ||
| css, | ||
| CSSResult, | ||
| customElement, | ||
| html, | ||
| internalProperty, | ||
| LitElement, | ||
| property, | ||
| PropertyValues, | ||
| query, | ||
| } from "lit-element"; | ||
|
|
||
| import { navigate } from "../../../../../common/navigate"; | ||
| import { fetchDevices, ZHADevice } from "../../../../../data/zha"; | ||
| import "../../../../../layouts/hass-subpage"; | ||
| import type { HomeAssistant } from "../../../../../types"; | ||
| import { Network, Edge, Node, EdgeOptions } from "vis-network"; | ||
|
|
||
| @customElement("zha-network-visualization-page") | ||
| export class ZHANetworkVisualizationPage extends LitElement { | ||
| @property({ type: Object }) public hass!: HomeAssistant; | ||
|
|
||
| @property({ type: Boolean }) public narrow!: boolean; | ||
|
|
||
| @query("#visualization", true) | ||
| private _visualization?: HTMLElement; | ||
|
|
||
| @internalProperty() | ||
| private _devices: Map<string, ZHADevice> = new Map(); | ||
|
|
||
| @internalProperty() | ||
| private _network?: Network; | ||
|
|
||
| protected firstUpdated(changedProperties: PropertyValues): void { | ||
| super.firstUpdated(changedProperties); | ||
| if (this.hass) { | ||
| this._fetchData(); | ||
| } | ||
| this._network = new Network( | ||
| this._visualization!, | ||
| {}, | ||
| { | ||
| autoResize: true, | ||
| height: window.innerHeight + "px", | ||
| width: window.innerWidth + "px", | ||
| layout: { | ||
| improvedLayout: true, | ||
| }, | ||
| physics: { | ||
| barnesHut: { | ||
| springConstant: 0, | ||
| avoidOverlap: 10, | ||
| damping: 0.09, | ||
| }, | ||
| }, | ||
| nodes: { | ||
| font: { | ||
| multi: "html", | ||
| }, | ||
| }, | ||
| edges: { | ||
| smooth: { | ||
| enabled: true, | ||
| type: "continuous", | ||
| forceDirection: "none", | ||
| roundness: 0.6, | ||
| }, | ||
| }, | ||
| } | ||
| ); | ||
| this._network.on("doubleClick", (properties) => { | ||
| const ieee = properties.nodes[0]; | ||
| if (ieee) { | ||
| const device = this._devices.get(ieee); | ||
| if (device) { | ||
| navigate( | ||
| this, | ||
| `/config/devices/device/${device.device_reg_id}`, | ||
| false | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| protected render() { | ||
| return html` | ||
| <hass-subpage | ||
| .hass=${this.hass} | ||
| .header=${this.hass.localize( | ||
| "ui.panel.config.zha.visualization.header" | ||
| )} | ||
| > | ||
| <div id="visualization"></div> | ||
| </hass-subpage> | ||
| `; | ||
| } | ||
|
|
||
| private async _fetchData() { | ||
| const devices = await fetchDevices(this.hass!); | ||
| this._devices = new Map( | ||
| devices.map((device: ZHADevice) => [device.ieee, device]) | ||
| ); | ||
| this._updateDevices(devices); | ||
| } | ||
|
|
||
| private _updateDevices(devices: ZHADevice[]) { | ||
| const nodes: Node[] = []; | ||
| const edges: Edge[] = []; | ||
|
|
||
| devices.forEach((device) => { | ||
| nodes.push({ | ||
| id: device.ieee, | ||
| label: this._buildLabel(device), | ||
| shape: this._getShape(device), | ||
| mass: this._getMass(device), | ||
| }); | ||
| if (device.neighbors && device.neighbors.length > 0) { | ||
| device.neighbors.forEach((neighbor) => { | ||
| const idx = edges.findIndex(function (e) { | ||
| return device.ieee === e.to && neighbor.ieee === e.from; | ||
| }); | ||
| if (idx === -1) { | ||
| edges.push({ | ||
| from: device.ieee, | ||
| to: neighbor.ieee, | ||
| label: neighbor.lqi + "", | ||
| color: this._getLQI(neighbor.lqi), | ||
| }); | ||
| } else { | ||
| edges[idx].color = this._getLQI( | ||
| (parseInt(edges[idx].label!) + neighbor.lqi) / 2 | ||
| ); | ||
| edges[idx].label += "/" + neighbor.lqi; | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| this._network?.setData({ nodes: nodes, edges: edges }); | ||
| } | ||
|
|
||
| private _getLQI(lqi: number): EdgeOptions["color"] { | ||
| if (lqi > 192) { | ||
| return { color: "#17ab00", highlight: "#17ab00" }; | ||
| } | ||
| if (lqi > 128) { | ||
| return { color: "#e6b402", highlight: "#e6b402" }; | ||
| } | ||
| if (lqi > 80) { | ||
| return { color: "#fc4c4c", highlight: "#fc4c4c" }; | ||
| } | ||
| return { color: "#bfbfbf", highlight: "#bfbfbf" }; | ||
| } | ||
|
|
||
| private _getMass(device: ZHADevice): number { | ||
| if (device.device_type === "Coordinator") { | ||
| return 2; | ||
| } | ||
| if (device.device_type === "Router") { | ||
| return 4; | ||
| } | ||
| return 5; | ||
| } | ||
|
|
||
| private _getShape(device: ZHADevice): string { | ||
| if (device.device_type === "Coordinator") { | ||
| return "box"; | ||
| } | ||
| if (device.device_type === "Router") { | ||
| return "ellipse"; | ||
| } | ||
| return "circle"; | ||
| } | ||
|
|
||
| private _buildLabel(device: ZHADevice): string { | ||
| let label = | ||
| device.user_given_name !== null | ||
| ? `<b>${device.user_given_name}</b>\n` | ||
| : ""; | ||
| label += `<b>IEEE: </b>${device.ieee}`; | ||
| label += `\n<b>Device Type: </b>${device.device_type.replace("_", " ")}`; | ||
| if (device.nwk != null) { | ||
| label += `\n<b>NWK: </b>${device.nwk}`; | ||
| } | ||
| if (device.manufacturer != null && device.model != null) { | ||
| label += `\n<b>Device: </b>${device.manufacturer} ${device.model}`; | ||
| } else { | ||
| label += "\n<b>Device is not in <i>'zigbee.db'</i></b>"; | ||
| } | ||
| if (!device.available) { | ||
| label += "\n<b>Device is <i>Offline</i></b>"; | ||
| } | ||
| return label; | ||
| } | ||
|
|
||
| static get styles(): CSSResult[] { | ||
| return [ | ||
| css` | ||
| .header { | ||
| font-family: var(--paper-font-display1_-_font-family); | ||
| -webkit-font-smoothing: var( | ||
| --paper-font-display1_-_-webkit-font-smoothing | ||
| ); | ||
| font-size: var(--paper-font-display1_-_font-size); | ||
| font-weight: var(--paper-font-display1_-_font-weight); | ||
| letter-spacing: var(--paper-font-display1_-_letter-spacing); | ||
| line-height: var(--paper-font-display1_-_line-height); | ||
| opacity: var(--dark-primary-opacity); | ||
| } | ||
| `, | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should make sure we are connected when creating this, as otherwise the height and width could not be set yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or you should update it in onConnected when
this._networkis defined