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

Update xiaomi-vacuum-map-card.ts #3

Merged
merged 1 commit into from
Nov 26, 2023
Merged
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
76 changes: 50 additions & 26 deletions src/xiaomi-vacuum-map-card.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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),
);
Expand Down Expand Up @@ -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<void> {
const currentPreset = this._getCurrentPreset();
const currentMode = this._getCurrentMode();
Expand Down Expand Up @@ -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<PredefinedZoneConfig>();
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<void> {
Expand Down
Loading