device name (#2901)

This commit is contained in:
David F. Mulcahey 2019-03-11 15:12:03 -04:00 committed by Paulus Schoutsen
parent 262537c287
commit cdde6f6f4c
4 changed files with 76 additions and 4 deletions

View File

@ -10,10 +10,12 @@ export interface DeviceRegistryEntry {
sw_version?: string;
hub_device_id?: string;
area_id?: string;
name_by_user?: string;
}
export interface DeviceRegistryEntryMutableParams {
area_id: string;
area_id?: string;
name_by_user?: string;
}
export const fetchDeviceRegistry = (hass: HomeAssistant) =>

View File

@ -14,6 +14,8 @@ export interface ZHADevice {
quirk_class: string;
entities: ZHAEntityReference[];
manufacturer_code: number;
device_reg_id: string;
user_given_name: string;
}
export interface Attribute {

View File

@ -10,6 +10,7 @@ import {
import "@material/mwc-button";
import "@polymer/paper-card/paper-card";
import "@polymer/paper-icon-button/paper-icon-button";
import "@polymer/paper-input/paper-input";
import "@polymer/paper-item/paper-item";
import "@polymer/paper-listbox/paper-listbox";
import { fireEvent } from "../../../common/dom/fire_event";
@ -18,9 +19,13 @@ import "../../../components/ha-service-description";
import { haStyle } from "../../../resources/styles";
import { HomeAssistant } from "../../../types";
import "../ha-config-section";
import { ItemSelectedEvent, NodeServiceData } from "./types";
import { ItemSelectedEvent, NodeServiceData, ChangeEvent } from "./types";
import "./zha-clusters";
import "./zha-device-card";
import {
updateDeviceRegistryEntry,
DeviceRegistryEntryMutableParams,
} from "../../../data/device_registry";
import { reconfigureNode, fetchDevices, ZHADevice } from "../../../data/zha";
declare global {
@ -40,6 +45,7 @@ export class ZHANode extends LitElement {
private _selectedNode?: ZHADevice;
private _serviceData?: {};
private _nodes: ZHADevice[];
private _userSelectedName?: string;
constructor() {
super();
@ -58,6 +64,7 @@ export class ZHANode extends LitElement {
_entities: {},
_serviceData: {},
_nodes: {},
_userSelectedName: {},
};
}
@ -109,7 +116,11 @@ export class ZHANode extends LitElement {
>
${this._nodes.map(
(entry) => html`
<paper-item>${entry.name}</paper-item>
<paper-item
>${entry.user_given_name
? entry.user_given_name
: entry.name}</paper-item
>
`
)}
</paper-listbox>
@ -132,6 +143,18 @@ export class ZHANode extends LitElement {
></zha-device-card>
`
: ""}
${this._selectedNodeIndex !== -1
? html`
<div class="input-text">
<paper-input
type="string"
.value="${this._userSelectedName}"
@value-changed="${this._onUserSelectedNameChanged}"
placeholder="User given name"
></paper-input>
</div>
`
: ""}
${this._selectedNodeIndex !== -1 ? this._renderNodeActions() : ""}
${this._selectedNode ? this._renderClusters() : ""}
</paper-card>
@ -170,6 +193,21 @@ export class ZHANode extends LitElement {
/>
`
: ""}
<mwc-button
@click="${this._onUpdateDeviceNameClick}"
.disabled="${!this._userSelectedName ||
this._userSelectedName === ""}"
>Update Name</mwc-button
>
${this._showHelp
? html`
<div class="helpText">
${this.hass!.localize(
"ui.panel.config.zha.services.updateDeviceName"
)}
</div>
`
: ""}
</div>
`;
}
@ -191,6 +229,7 @@ export class ZHANode extends LitElement {
private _selectedNodeChanged(event: ItemSelectedEvent): void {
this._selectedNodeIndex = event!.target!.selected;
this._selectedNode = this._nodes[this._selectedNodeIndex];
this._userSelectedName = "";
fireEvent(this, "zha-node-selected", { node: this._selectedNode });
this._serviceData = this._computeNodeServiceData();
}
@ -201,6 +240,27 @@ export class ZHANode extends LitElement {
}
}
private _onUserSelectedNameChanged(value: ChangeEvent): void {
this._userSelectedName = value.detail!.value;
}
private async _onUpdateDeviceNameClick(): Promise<void> {
if (this.hass) {
const values: DeviceRegistryEntryMutableParams = {
name_by_user: this._userSelectedName,
};
await updateDeviceRegistryEntry(
this.hass,
this._selectedNode!.device_reg_id,
values
);
this._selectedNode!.user_given_name = this._userSelectedName!;
this._userSelectedName = "";
}
}
private _computeNodeServiceData(): NodeServiceData {
return {
ieee_address: this._selectedNode!.ieee,
@ -277,6 +337,7 @@ export class ZHANode extends LitElement {
padding-left: 28px;
padding-right: 28px;
padding-bottom: 10px;
word-wrap: break-word;
}
ha-service-description {
@ -294,6 +355,12 @@ export class ZHANode extends LitElement {
right: 0;
color: var(--primary-color);
}
.input-text {
padding-left: 28px;
padding-right: 28px;
padding-bottom: 10px;
}
`,
];
}

View File

@ -874,7 +874,8 @@
"caption": "ZHA",
"description": "Zigbee Home Automation network management",
"services": {
"reconfigure": "Reconfigure ZHA device (heal device). Use this if you are having issues with the device. If the device in question is a battery powered device please ensure it is awake and accepting commands when you use this service."
"reconfigure": "Reconfigure ZHA device (heal device). Use this if you are having issues with the device. If the device in question is a battery powered device please ensure it is awake and accepting commands when you use this service.",
"updateDeviceName": "Set a custom name for this device in the device registry."
}
},
"zwave": {