Allow binary sensor device class updates (#12124)

This commit is contained in:
Zack Barett 2022-03-28 10:44:21 -05:00 committed by GitHub
parent a44b8981e1
commit 61f6e8855b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 137 additions and 61 deletions

View File

@ -1,6 +1,5 @@
import "@material/mwc-formfield/mwc-formfield";
import "../../../components/ha-radio";
import "@material/mwc-button/mwc-button"; import "@material/mwc-button/mwc-button";
import "@material/mwc-formfield/mwc-formfield";
import "@material/mwc-list/mwc-list-item"; import "@material/mwc-list/mwc-list-item";
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket"; import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
import { import {
@ -20,9 +19,15 @@ import "../../../components/ha-alert";
import "../../../components/ha-area-picker"; import "../../../components/ha-area-picker";
import "../../../components/ha-expansion-panel"; import "../../../components/ha-expansion-panel";
import "../../../components/ha-icon-picker"; import "../../../components/ha-icon-picker";
import "../../../components/ha-radio";
import "../../../components/ha-select"; import "../../../components/ha-select";
import "../../../components/ha-switch"; import "../../../components/ha-switch";
import "../../../components/ha-textfield"; import "../../../components/ha-textfield";
import {
ConfigEntry,
deleteConfigEntry,
getConfigEntries,
} from "../../../data/config_entries";
import { import {
DeviceRegistryEntry, DeviceRegistryEntry,
subscribeDeviceRegistry, subscribeDeviceRegistry,
@ -34,6 +39,7 @@ import {
removeEntityRegistryEntry, removeEntityRegistryEntry,
updateEntityRegistryEntry, updateEntityRegistryEntry,
} from "../../../data/entity_registry"; } from "../../../data/entity_registry";
import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow";
import { import {
showAlertDialog, showAlertDialog,
showConfirmationDialog, showConfirmationDialog,
@ -42,27 +48,39 @@ import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
import { haStyle } from "../../../resources/styles"; import { haStyle } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types"; import type { HomeAssistant } from "../../../types";
import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail"; import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail";
import {
ConfigEntry,
deleteConfigEntry,
getConfigEntries,
} from "../../../data/config_entries";
import { showOptionsFlowDialog } from "../../../dialogs/config-flow/show-dialog-options-flow";
const OVERRIDE_DEVICE_CLASSES = { const OVERRIDE_DEVICE_CLASSES = {
cover: [ cover: [
"awning", [
"blind", "awning",
"curtain", "blind",
"damper", "curtain",
"door", "damper",
"garage", "door",
"gate", "garage",
"shade", "gate",
"shutter", "shade",
"window", "shutter",
"window",
],
],
binary_sensor: [
["lock"], // Lock
["window", "door", "garage_door", "opening"], // Door
["battery", "battery_charging"], // Battery
["cold", "gas", "heat"], // Climate
["running", "motion", "moving", "occupancy", "presence", "vibration"], // Presence
["power", "plug", "light"], // Power
[
"smoke",
"safety",
"sound",
"problem",
"tamper",
"carbon_monoxide",
"moisture",
], // Alarm
], ],
binary_sensor: ["window", "door", "garage_door", "opening"],
}; };
@customElement("entity-registry-settings") @customElement("entity-registry-settings")
@ -85,8 +103,6 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
@state() private _hiddenBy!: string | null; @state() private _hiddenBy!: string | null;
private _deviceLookup?: Record<string, DeviceRegistryEntry>;
@state() private _device?: DeviceRegistryEntry; @state() private _device?: DeviceRegistryEntry;
@state() private _helperConfigEntry?: ConfigEntry; @state() private _helperConfigEntry?: ConfigEntry;
@ -97,6 +113,10 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
private _origEntityId!: string; private _origEntityId!: string;
private _deviceLookup?: Record<string, DeviceRegistryEntry>;
private _deviceClassOptions?: string[][];
public hassSubscribe(): UnsubscribeFunc[] { public hassSubscribe(): UnsubscribeFunc[] {
return [ return [
subscribeDeviceRegistry(this.hass.connection!, (devices) => { subscribeDeviceRegistry(this.hass.connection!, (devices) => {
@ -125,23 +145,41 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
} }
} }
protected updated(changedProperties: PropertyValues) { protected willUpdate(changedProperties: PropertyValues) {
super.updated(changedProperties); super.willUpdate(changedProperties);
if (changedProperties.has("entry")) { if (!changedProperties.has("entry")) {
this._error = undefined; return;
this._name = this.entry.name || ""; }
this._icon = this.entry.icon || "";
this._deviceClass = this._error = undefined;
this.entry.device_class || this.entry.original_device_class; this._name = this.entry.name || "";
this._origEntityId = this.entry.entity_id; this._icon = this.entry.icon || "";
this._areaId = this.entry.area_id; this._deviceClass =
this._entityId = this.entry.entity_id; this.entry.device_class || this.entry.original_device_class;
this._disabledBy = this.entry.disabled_by; this._origEntityId = this.entry.entity_id;
this._hiddenBy = this.entry.hidden_by; this._areaId = this.entry.area_id;
this._device = this._entityId = this.entry.entity_id;
this.entry.device_id && this._deviceLookup this._disabledBy = this.entry.disabled_by;
? this._deviceLookup[this.entry.device_id] this._hiddenBy = this.entry.hidden_by;
: undefined; this._device =
this.entry.device_id && this._deviceLookup
? this._deviceLookup[this.entry.device_id]
: undefined;
const domain = computeDomain(this.entry.entity_id);
const deviceClasses: string[][] = OVERRIDE_DEVICE_CLASSES[domain];
if (!deviceClasses) {
return;
}
this._deviceClassOptions = [[], []];
for (const deviceClass of deviceClasses) {
if (deviceClass.includes(this.entry.original_device_class!)) {
this._deviceClassOptions[0] = deviceClass;
} else {
this._deviceClassOptions[1].push(...deviceClass);
}
} }
} }
@ -197,28 +235,39 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
: undefined} : undefined}
.disabled=${this._submitting} .disabled=${this._submitting}
></ha-icon-picker> ></ha-icon-picker>
${OVERRIDE_DEVICE_CLASSES[domain]?.includes(this._deviceClass) || ${this._deviceClassOptions
(domain === "cover" && this.entry.original_device_class === null) ? html`
? html`<ha-select <ha-select
.label=${this.hass.localize( .label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.device_class" "ui.dialogs.entity_registry.editor.device_class"
)} )}
.value=${this._deviceClass} .value=${this._deviceClass}
naturalMenuWidth naturalMenuWidth
fixedMenuPosition fixedMenuPosition
@selected=${this._deviceClassChanged} @selected=${this._deviceClassChanged}
@closed=${stopPropagation} @closed=${stopPropagation}
> >
${OVERRIDE_DEVICE_CLASSES[domain].map( ${this._deviceClassOptions[0].map(
(deviceClass: string) => html` (deviceClass: string) => html`
<mwc-list-item .value=${deviceClass}> <mwc-list-item .value=${deviceClass} test=${deviceClass}>
${this.hass.localize( ${this.hass.localize(
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}` `ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
)} )}
</mwc-list-item> </mwc-list-item>
` `
)} )}
</ha-select>` <li divider role="separator"></li>
${this._deviceClassOptions[1].map(
(deviceClass: string) => html`
<mwc-list-item .value=${deviceClass} test=${deviceClass}>
${this.hass.localize(
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
)}
</mwc-list-item>
`
)}
</ha-select>
`
: ""} : ""}
<ha-textfield <ha-textfield
error-message="Domain needs to stay the same" error-message="Domain needs to stay the same"
@ -585,6 +634,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
margin: 8px 0; margin: 8px 0;
width: 340px; width: 340px;
} }
li[divider] {
border-bottom-color: var(--divider-color);
}
`, `,
]; ];
} }

View File

@ -790,7 +790,31 @@
"door": "Door", "door": "Door",
"garage_door": "Garage door", "garage_door": "Garage door",
"window": "Window", "window": "Window",
"opening": "Other" "opening": "Opening",
"battery": "Battery",
"battery_charging": "Battery charging",
"carbon_monoxide": "Carbon monoxide",
"cold": "Cold",
"connectivity": "Connectivity",
"gas": "Gas",
"heat": "Heat",
"light": "Light",
"lock": "Lock",
"moisture": "Moisture",
"motion": "Motion",
"moving": "Moving",
"occupancy": "Occupancy",
"plug": "Plug",
"power": "Power",
"presence": "Presence",
"problem": "Problem",
"running": "Running",
"safety": "Safety",
"smoke": "Smoke",
"sound": "Sound",
"tamper": "Tamper",
"update": "Update",
"vibration": "Vibration"
}, },
"cover": { "cover": {
"door": "Door", "door": "Door",