Update ZHA device card (#3411)

* add ability to subscribe to individual updates

* catch changes on area, device name and entity ids

* subscribe directly

* remove entity registry change

* add type

* remove device subscription
This commit is contained in:
David F. Mulcahey 2019-07-24 23:05:33 -04:00 committed by Paulus Schoutsen
parent 0cd263c532
commit ff80eef25d

View File

@ -30,13 +30,18 @@ import {
DeviceRegistryEntryMutableParams, DeviceRegistryEntryMutableParams,
updateDeviceRegistryEntry, updateDeviceRegistryEntry,
} from "../../../data/device_registry"; } from "../../../data/device_registry";
import { reconfigureNode, ZHADevice } from "../../../data/zha"; import {
reconfigureNode,
ZHADevice,
ZHAEntityReference,
} from "../../../data/zha";
import { haStyle } from "../../../resources/styles"; import { haStyle } from "../../../resources/styles";
import { HomeAssistant } from "../../../types"; import { HomeAssistant } from "../../../types";
import { ItemSelectedEvent, NodeServiceData } from "./types"; import { ItemSelectedEvent, NodeServiceData } from "./types";
import { navigate } from "../../../common/navigate"; import { navigate } from "../../../common/navigate";
import { UnsubscribeFunc } from "home-assistant-js-websocket"; import { UnsubscribeFunc, HassEvent } from "home-assistant-js-websocket";
import { formatAsPaddedHex } from "./functions"; import { formatAsPaddedHex } from "./functions";
import computeStateName from "../../../common/entity/compute_state_name";
declare global { declare global {
// for fire event // for fire event
@ -60,12 +65,34 @@ class ZHADeviceCard extends LitElement {
@property() private _selectedAreaIndex: number = -1; @property() private _selectedAreaIndex: number = -1;
@property() private _userGivenName?: string; @property() private _userGivenName?: string;
private _unsubAreas?: UnsubscribeFunc; private _unsubAreas?: UnsubscribeFunc;
private _unsubEntities?: UnsubscribeFunc;
public disconnectedCallback() { public disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
if (this._unsubAreas) { if (this._unsubAreas) {
this._unsubAreas(); this._unsubAreas();
} }
if (this._unsubEntities) {
this._unsubEntities();
}
}
public connectedCallback() {
super.connectedCallback();
this._unsubAreas = subscribeAreaRegistry(this.hass.connection, (areas) => {
this._areas = areas;
});
this.hass.connection
.subscribeEvents((event: HassEvent) => {
if (this.device) {
this.device!.entities.forEach((deviceEntity) => {
if (event.data.old_entity_id === deviceEntity.entity_id) {
deviceEntity.entity_id = event.data.entity_id;
}
});
}
}, "entity_registry_updated")
.then((unsub) => (this._unsubEntities = unsub));
} }
protected firstUpdated(changedProperties: PropertyValues): void { protected firstUpdated(changedProperties: PropertyValues): void {
@ -90,14 +117,6 @@ class ZHADeviceCard extends LitElement {
} }
this._userGivenName = this.device!.user_given_name; this._userGivenName = this.device!.user_given_name;
} }
if (!this._unsubAreas) {
this._unsubAreas = subscribeAreaRegistry(
this.hass.connection,
(areas) => {
this._areas = areas;
}
);
}
super.update(changedProperties); super.update(changedProperties);
} }
@ -168,7 +187,9 @@ class ZHADeviceCard extends LitElement {
${!this.isJoinPage ${!this.isJoinPage
? html` ? html`
<paper-item-body> <paper-item-body>
<div class="name">${entity.name}</div> <div class="name">
${this._computeEntityName(entity)}
</div>
<div class="secondary entity-id"> <div class="secondary entity-id">
${entity.entity_id} ${entity.entity_id}
</div> </div>
@ -280,6 +301,13 @@ class ZHADeviceCard extends LitElement {
} }
} }
private _computeEntityName(entity: ZHAEntityReference): string {
if (this.hass.states[entity.entity_id]) {
return computeStateName(this.hass.states[entity.entity_id]);
}
return entity.name;
}
private async _saveCustomName(event): Promise<void> { private async _saveCustomName(event): Promise<void> {
if (this.hass) { if (this.hass) {
const values: DeviceRegistryEntryMutableParams = { const values: DeviceRegistryEntryMutableParams = {