Skip to content

Commit

Permalink
Add multiple condition types
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Oct 24, 2023
1 parent 8af8db4 commit a49728b
Show file tree
Hide file tree
Showing 12 changed files with 498 additions and 222 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"start": "rollup -c --watch --bundleConfigAsCjs",
"build": "rollup -c --bundleConfigAsCjs",
"format": "prettier --write .",
"start:hass": "docker run --rm -p8123:8123 -v ${PWD}/.hass_dev:/config homeassistant/home-assistant:beta",
"start:hass-cmd": "docker run --rm -p8123:8123 -v %cd%/.hass_dev:/config homeassistant/home-assistant:beta"
"start:hass": "docker run --rm -p8123:8123 -v ${PWD}/.hass_dev:/config homeassistant/home-assistant:dev",
"start:hass-cmd": "docker run --rm -p8123:8123 -v %cd%/.hass_dev:/config homeassistant/home-assistant:dev"
},
"author": "Paul Bottein",
"repository": {
Expand Down
24 changes: 8 additions & 16 deletions src/cards/chips-card/chips-card-chips-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,31 +241,23 @@ export class ChipsCardEditorChips extends MushroomBaseElement {

private _renderChipLabel(chipConf: LovelaceChipConfig): string {
const customLocalize = setupCustomlocalize(this.hass);
let label = customLocalize(`editor.chip.chip-picker.types.${chipConf.type}`);
if (chipConf.type === "conditional" && chipConf.conditions.length > 0) {
const condition = chipConf.conditions[0];
const entity = this.getEntityName(condition.entity) ?? condition.entity;
label += ` - ${entity} ${
condition.state
? `= ${condition.state}`
: condition.state_not
? `≠ ${condition.state_not}`
: null
}`;
}
return label;
return customLocalize(`editor.chip.chip-picker.types.${chipConf.type}`);
}

private _renderChipSecondary(chipConf: LovelaceChipConfig): string | undefined {
const customLocalize = setupCustomlocalize(this.hass);
if ("entity" in chipConf && chipConf.entity) {
return `${this.getEntityName(chipConf.entity) ?? chipConf.entity}`;
return `${this.getEntityName(chipConf.entity) ?? chipConf.entity ?? ""}`;
}
if ("chip" in chipConf && chipConf.chip) {
const label = customLocalize(`editor.chip.chip-picker.types.${chipConf.chip.type}`);
return `${this._renderChipSecondary(chipConf.chip)} (via ${label})`;
const chipSecondary = this._renderChipSecondary(chipConf.chip);
if (chipSecondary) {
return `${this._renderChipSecondary(chipConf.chip)} (via ${label})`;
}
return label;
}
return undefined;
return "";
}

private getEntityName(entity_id: string): string | undefined {
Expand Down
8 changes: 1 addition & 7 deletions src/cards/chips-card/chips-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,10 @@ const weatherChipConfigStruct = object({
show_conditions: optional(boolean()),
});

const conditionStruct = object({
entity: string(),
state: optional(string()),
state_not: optional(string()),
});

const conditionChipConfigStruct = object({
type: literal("conditional"),
chip: optional(any()),
conditions: optional(array(conditionStruct)),
conditions: optional(array(any())),
});

const lightChipConfigStruct = object({
Expand Down
7 changes: 5 additions & 2 deletions src/cards/chips-card/chips-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import { customElement, state } from "lit/decorators.js";
import { customElement, property, state } from "lit/decorators.js";
import {
computeRTL,
HomeAssistant,
Expand All @@ -13,7 +13,7 @@ import { registerCustomCard } from "../../utils/custom-cards";
import { createChipElement } from "../../utils/lovelace/chip/chip-element";
import { LovelaceChip, LovelaceChipConfig } from "../../utils/lovelace/chip/types";
import "./chips";
import { EntityChip } from "./chips";
import { EntityChip } from "./chips/entity-chip";
import { CHIPS_CARD_EDITOR_NAME, CHIPS_CARD_NAME } from "./const";

export interface ChipsCardConfig extends LovelaceCardConfig {
Expand Down Expand Up @@ -42,6 +42,8 @@ export class ChipsCard extends LitElement implements LovelaceCard {
};
}

@property() public editMode?: boolean;

@state() private _config?: ChipsCardConfig;

private _hass?: HomeAssistant;
Expand Down Expand Up @@ -94,6 +96,7 @@ export class ChipsCard extends LitElement implements LovelaceCard {
}
if (this._hass) {
element.hass = this._hass;
element.editMode = this.editMode;
}
return html`${element}`;
}
Expand Down
Loading

0 comments on commit a49728b

Please sign in to comment.