Skip to content

Commit

Permalink
Simplify entity deletion (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
alengwenus authored May 24, 2024
1 parent a604656 commit 38b5cbd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
22 changes: 4 additions & 18 deletions src/lcn-entities-data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import memoizeOne from "memoize-one";
import { mdiDelete } from "@mdi/js";
import { computeRTLDirection } from "@ha/common/util/compute_rtl";
import type { HomeAssistant } from "@ha/types";
import { LCN, LcnEntityConfig, deleteEntity, LcnDeviceConfig, LcnAddress } from "types/lcn";
import { LCN, LcnEntityConfig, deleteEntity, LcnDeviceConfig } from "types/lcn";
import {
DataTableColumnContainer,
DataTableRowData,
Expand Down Expand Up @@ -105,32 +105,18 @@ export class LCNEntitiesDataTable extends LitElement {
.hass=${this.hass}
.columns=${this._columns(this.narrow)}
.data=${this._entities(this.entities) as DataTableRowData[]}
.id=${"unique_id"}
.noDataText=${this.lcn.localize("dashboard-entities-table-no-data")}
.dir=${computeRTLDirection(this.hass)}
auto-height
clickable
.id=${"entity_id"}
></ha-data-table>
`;
}

private _onEntityDelete(ev, entity: LcnEntityConfig) {
private async _onEntityDelete(ev, entity: LcnEntityConfig) {
ev.stopPropagation();
this._deleteEntity(entity.address, entity.domain, entity.resource);
}

private async _deleteEntity(address: LcnAddress, domain: string, resource: string) {
const entity_to_delete = this.entities.find(
(entity) =>
entity.address[0] === address[0] &&
entity.address[1] === address[1] &&
entity.address[2] === address[2] &&
entity.domain === domain &&
entity.resource === resource,
)!;

await deleteEntity(this.hass, this.lcn.host.id, entity_to_delete);

await deleteEntity(this.hass, this.lcn.host.id, entity);
this.dispatchEvent(
new CustomEvent("lcn-configuration-changed", {
bubbles: true,
Expand Down
10 changes: 9 additions & 1 deletion src/types/lcn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ export interface LcnEntityConfig {
name: string;
domain: string;
resource: string;
domain_data: CoverConfig[] | LightConfig[] | SensorConfig[] | SwitchConfig[];
domain_data:
| BinarySensorConfig[]
| ClimateConfig[]
| CoverConfig[]
| LightConfig[]
| SceneConfig[]
| SensorConfig[]
| SwitchConfig[];
entity_id: string;
}

export interface LcnDeviceConfig {
Expand Down

0 comments on commit 38b5cbd

Please sign in to comment.