Skip to content
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

Allow overriding device class #10777

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/data/entity_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface ExtEntityRegistryEntry extends EntityRegistryEntry {
capabilities: Record<string, unknown>;
original_name?: string;
original_icon?: string;
device_class?: string;
original_device_class?: string;
}

export interface UpdateEntityRegistryEntryResult {
Expand All @@ -32,6 +34,7 @@ export interface UpdateEntityRegistryEntryResult {
export interface EntityRegistryEntryUpdateParams {
name?: string | null;
icon?: string | null;
device_class?: string | null;
area_id?: string | null;
disabled_by?: string | null;
new_entity_id?: string;
Expand Down
57 changes: 54 additions & 3 deletions src/panels/config/entities/entity-registry-settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "@material/mwc-button/mwc-button";
import "@polymer/paper-input/paper-input";
import type { PaperItemElement } from "@polymer/paper-item/paper-item";
import { HassEntity, UnsubscribeFunc } from "home-assistant-js-websocket";
import {
css,
Expand All @@ -16,6 +17,7 @@ import { domainIcon } from "../../../common/entity/domain_icon";
import "../../../components/ha-area-picker";
import "../../../components/ha-expansion-panel";
import "../../../components/ha-icon-picker";
import "../../../components/ha-paper-dropdown-menu";
import "../../../components/ha-switch";
import type { HaSwitch } from "../../../components/ha-switch";
import {
Expand All @@ -39,6 +41,11 @@ import { haStyle } from "../../../resources/styles";
import type { HomeAssistant } from "../../../types";
import { showDeviceRegistryDetailDialog } from "../devices/device-registry-detail/show-dialog-device-registry-detail";

const OVERRIDE_DEVICE_CLASSES = {
cover: ["window", "door", "garage"],
binary_sensor: ["window", "door", "garage_door", "opening"],
};

@customElement("entity-registry-settings")
export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant;
Expand All @@ -51,6 +58,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {

@state() private _entityId!: string;

@state() private _deviceClass?: string;

@state() private _areaId?: string | null;

@state() private _disabledBy!: string | null;
Expand Down Expand Up @@ -85,6 +94,8 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
this._error = undefined;
this._name = this.entry.name || "";
this._icon = this.entry.icon || "";
this._deviceClass =
this.entry.device_class || this.entry.original_device_class;
this._origEntityId = this.entry.entity_id;
this._areaId = this.entry.area_id;
this._entityId = this.entry.entity_id;
Expand All @@ -102,9 +113,11 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
}
const stateObj: HassEntity | undefined =
this.hass.states[this.entry.entity_id];
const invalidDomainUpdate =
computeDomain(this._entityId.trim()) !==
computeDomain(this.entry.entity_id);

const domain = computeDomain(this.entry.entity_id);

const invalidDomainUpdate = computeDomain(this._entityId.trim()) !== domain;

return html`
${!stateObj
? html`
Expand Down Expand Up @@ -143,6 +156,31 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
: undefined}
.disabled=${this._submitting}
></ha-icon-picker>
${OVERRIDE_DEVICE_CLASSES[domain]?.includes(this._deviceClass) ||
(domain === "cover" && this.entry.original_device_class === null)
? html`<ha-paper-dropdown-menu
.label=${this.hass.localize(
"ui.dialogs.entity_registry.editor.device_class"
)}
>
<paper-listbox
slot="dropdown-content"
attr-for-selected="item-value"
.selected=${this._deviceClass}
@selected-item-changed=${this._deviceClassChanged}
>
${OVERRIDE_DEVICE_CLASSES[domain].map(
(deviceClass: string) => html`
<paper-item .itemValue=${String(deviceClass)}>
${this.hass.localize(
`ui.dialogs.entity_registry.editor.device_classes.${domain}.${deviceClass}`
)}
</paper-item>
`
)}
</paper-listbox>
</ha-paper-dropdown-menu>`
: ""}
<paper-input
.value=${this._entityId}
@value-changed=${this._entityIdChanged}
Expand Down Expand Up @@ -264,6 +302,15 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
this._entityId = ev.detail.value;
}

private _deviceClassChanged(ev: PolymerChangedEvent<PaperItemElement>): void {
this._error = undefined;
if (ev.detail.value === null) {
return;
}
const value = (ev.detail.value as any).itemValue;
this._deviceClass = value === "null" ? null : value;
}

private _areaPicked(ev: CustomEvent) {
this._error = undefined;
this._areaId = ev.detail.value;
Expand All @@ -289,6 +336,7 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
name: this._name.trim() || null,
icon: this._icon.trim() || null,
area_id: this._areaId || null,
device_class: this._deviceClass || null,
new_entity_id: this._entityId.trim(),
};
if (
Expand Down Expand Up @@ -378,6 +426,9 @@ export class EntityRegistrySettings extends SubscribeMixin(LitElement) {
padding-bottom: max(env(safe-area-inset-bottom), 8px);
background-color: var(--mdc-theme-surface, #fff);
}
ha-paper-dropdown-menu {
width: 100%;
}
ha-switch {
margin-right: 16px;
}
Expand Down
14 changes: 14 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,20 @@
"icon": "Icon",
"icon_error": "Icons should be in the format 'prefix:iconname', e.g. 'mdi:home'",
"entity_id": "Entity ID",
"device_class": "Show as",
"device_classes": {
"binary_sensor": {
"door": "Door",
"garage_door": "Garage door",
"window": "Window",
"opening": "Other"
},
"cover": {
"door": "Door",
"garage": "Garage door",
"window": "Window"
}
},
"unavailable": "This entity is unavailable.",
"enabled_label": "Enable entity",
"enabled_cause": "Disabled by {cause}.",
Expand Down