From 2be8513a5d6b7c655c881c87c7d5b64e0c354792 Mon Sep 17 00:00:00 2001 From: SCA075 <82227818+sca075@users.noreply.github.com> Date: Sun, 26 Nov 2023 11:39:05 +0100 Subject: [PATCH] Update xiaomi-vacuum-map-card.ts Reverted changes for topic, added getpredefifinedzones --- src/xiaomi-vacuum-map-card.ts | 76 +++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/src/xiaomi-vacuum-map-card.ts b/src/xiaomi-vacuum-map-card.ts index 759e18b3..03962d21 100644 --- a/src/xiaomi-vacuum-map-card.ts +++ b/src/xiaomi-vacuum-map-card.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-explicit-any */ import { css, CSSResultGroup, html, LitElement, PropertyValues, svg, SVGTemplateResult, TemplateResult } from "lit"; import { customElement, property, query, queryAll, state } from "lit/decorators"; @@ -21,11 +22,13 @@ import { CalibrationPoint, CardPresetConfig, MapExtractorRoom, + MapExtractorZone, PredefinedZoneConfig, + PredefinedZoneConfigEventData, TranslatableString, VariablesStorage, } from "./types/types"; -import { actionHandler } from "./action-handler-directive"; +import { actionHandler, XiaomiVacuumMapCardActionHandler } from "./action-handler-directive"; import { CARD_CUSTOM_ELEMENT_NAME, CARD_VERSION, @@ -39,6 +42,8 @@ import { EVENT_LOVELACE_DOM_DETAIL, EVENT_ROOM_CONFIG, EVENT_ROOM_CONFIG_GET, + EVENT_PREDEFINED_ZONE_CONFIG, + EVENT_PREDEFINED_ZONE_CONFIG_GET, EVENT_SELECTION_CHANGED, EVENT_SERVICE_CALL, EVENT_SERVICE_CALL_GET, @@ -84,6 +89,8 @@ import { DropdownMenu } from "./components/dropdown-menu"; import { TilesWrapper } from "./components/tiles-wrapper"; import { IconsWrapper } from "./components/icons-wrapper"; import { PresetSelector } from "./components/preset-selector"; +import { configColl } from "home-assistant-js-websocket"; +import { XiaomiVacuumMapCardEditor } from "./editor"; const line1 = " XIAOMI-VACUUM-MAP-CARD"; const line2 = ` ${localize("common.version")} ${CARD_VERSION}`; @@ -144,6 +151,7 @@ export class XiaomiVacuumMapCard extends LitElement { super(); this._handleAutogeneratedConfigGet = this._handleAutogeneratedConfigGet.bind(this); this._handleRoomsConfigGet = this._handleRoomsConfigGet.bind(this); + this._handlePredefinedZoneConfigGet = this._handlePredefinedZoneConfigGet.bind(this) this._handleServiceCallGet = this._handleServiceCallGet.bind(this); this._handleLovelaceDomEvent = this._handleLovelaceDomEvent.bind(this); } @@ -172,32 +180,9 @@ export class XiaomiVacuumMapCard extends LitElement { const cameras = entities .filter(e => e.substring(0, e.indexOf(".")) === "camera") .filter(e => hass?.states[e].attributes["calibration_points"]); - const mqtt_cam = entities - .filter(e => e.substring(0, e.indexOf(".")) === "camera") - .filter(e => hass?.states[e].attributes["vacuum_topic"]); const vacuums = entities.filter(e => e.substring(0, e.indexOf(".")) === "vacuum"); if (cameras.length === 0 || vacuums.length === 0) { return undefined; - } - console.log("cameras:", cameras); - console.log("mqtt_cam:", mqtt_cam); - if (mqtt_cam.length > 0){ - const topicValue = mqtt_cam.map(cameraId => hass?.states[cameraId]?.attributes["vacuum_topic"]); - console.log("Topic Value:", topicValue); - return { - type: "custom:" + CARD_CUSTOM_ELEMENT_NAME, - map_source: { - camera: cameras[0], - }, - calibration_source: { - camera: true, - }, - entity: vacuums[0], - vacuum_platform: PlatformGenerator.XIAOMI_MIIO_PLATFORM, - internal_variables: { - topic: topicValue[0] - } - }; } else { return { type: "custom:" + CARD_CUSTOM_ELEMENT_NAME, @@ -240,6 +225,7 @@ export class XiaomiVacuumMapCard extends LitElement { if (this._isInEditor()) { window.addEventListener(EVENT_AUTOGENERATED_CONFIG_GET, this._handleAutogeneratedConfigGet); window.addEventListener(EVENT_ROOM_CONFIG_GET, this._handleRoomsConfigGet); + window.addEventListener(EVENT_PREDEFINED_ZONE_CONFIG_GET, this._handlePredefinedZoneConfigGet); window.addEventListener(EVENT_SERVICE_CALL_GET, this._handleServiceCallGet) this.isInEditor = true; } @@ -254,6 +240,7 @@ export class XiaomiVacuumMapCard extends LitElement { if (this._isInEditor()) { window.removeEventListener(EVENT_AUTOGENERATED_CONFIG_GET, this._handleAutogeneratedConfigGet); window.removeEventListener(EVENT_ROOM_CONFIG_GET, this._handleRoomsConfigGet); + window.removeEventListener(EVENT_PREDEFINED_ZONE_CONFIG_GET, this._handlePredefinedZoneConfigGet); window.removeEventListener(EVENT_SERVICE_CALL_GET, this._handleServiceCallGet); } document.removeEventListener(EVENT_LOVELACE_DOM, this._handleLovelaceDomEvent); @@ -487,7 +474,6 @@ export class XiaomiVacuumMapCard extends LitElement { private _getAllAvailablePresets(): CardPresetConfig[] { const allPresets = this._getAllPresets(); - console.log(allPresets) const available = allPresets.filter( p => (p.conditions?.length ?? 0) === 0 || areConditionsMet(p, this.internalVariables, this.hass), ); @@ -888,6 +874,12 @@ export class XiaomiVacuumMapCard extends LitElement { window.dispatchEvent(event); } + private _handlePredefinedZoneConfigGet(): void { + const event = new Event(EVENT_PREDEFINED_ZONE_CONFIG); + (event as any).PredefinedZoneConfig = this._getPredefinedZonesConfig(); + window.dispatchEvent(event); + } + private async _handleServiceCallGet(): Promise { const currentPreset = this._getCurrentPreset(); const currentMode = this._getCurrentMode(); @@ -1017,13 +1009,45 @@ export class XiaomiVacuumMapCard extends LitElement { return undefined; } + private _getPredefinedZonesConfig(): PredefinedZoneConfigEventData | undefined { + const config = this._getCurrentPreset(); + const zones = this.hass.states[config.map_source?.camera ?? ""]?.attributes["zones"] as Record< + string, + MapExtractorZone + >; + const predefinedZonesConfig = new Array(); + if (zones) { + const mode = this.modes.filter(m => m.selectionType === SelectionType.PREDEFINED_RECTANGLE).reverse()[0]; + const modeIndex = mode ? this.modes.indexOf(mode) : -1; + for (const zone_id in zones) { + if (!zones.hasOwnProperty(zone_id)) continue; + const zone = zones[zone_id]; + const predefinedZoneConfig = { + id: zone.name ?? `${zone.name}`, + label: { + text: zone.name ?? `Zone ${zone.name}`, + x: zone.x, + y: zone.y, + }, + zones: zone.zones, + } as PredefinedZoneConfig; + + predefinedZonesConfig.push(predefinedZoneConfig); + } + return { modeIndex: modeIndex, zones: predefinedZonesConfig }; + } + return undefined; + } + + private static adjustRoomId(roomId: string | number, config: MapMode): string | number { if (config.idType === "number") { return +roomId; const roomIdAsNumber = +roomId; return isNaN(roomIdAsNumber) ? roomId : roomIdAsNumber; + } else { + return roomId; } - return roomId; } private async _run(debug: boolean): Promise {