Skip to content

Commit

Permalink
Merge branch 'next' into zonk
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed Oct 20, 2023
2 parents 618d0a8 + 85610b6 commit 23ec841
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 148 deletions.
98 changes: 49 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@
"@types/jest": "^27.4.0",
"jest": "^24.9.0",
"jest-electron": "^0.1.12",
"rollup": "^2.63.0",
"rollup": "^2.65.0",
"rollup-plugin-import-css": "^3.0.2",
"rollup-plugin-minify-html-literals": "^1.2.6",
"rollup-plugin-serve": "^1.1.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-version-injector": "^1.3.3",
"ts-jest": "^24.3.0",
"tslib": "^2.3.1",
"typescript": "^4.5.4"
"typescript": "^4.5.5"
},
"dependencies": {
"custom-card-helpers": "^1.8.0",
"lit": "^2.0.2"
"lit": "^2.1.1"
}
}
97 changes: 7 additions & 90 deletions src/custom-elements/battery-state-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import entityStyles from "./battery-state-entity.css";
import { handleAction } from "../action";
import { RichStringProcessor } from "../rich-string-processor";
import { getColorForBatteryLevel } from "../colors";
import { getSecondaryInfo } from "../entity-fields/get-secondary-info";
import { getChargingState } from "../entity-fields/charging-state";

/**
* Some sensor may produce string value like "45%". This regex is meant to parse such values.
Expand Down Expand Up @@ -68,7 +70,7 @@ export class BatteryStateEntity extends LovelaceCard<IBatteryEntityConfig> {
public static get styles() {
return css(<any>[sharedStyles + entityStyles]);
}

async internalUpdate() {
this.name = getName(this.config, this.hass);
this.state = getBatteryLevel(this.config, this.hass);
Expand Down Expand Up @@ -116,7 +118,7 @@ export class BatteryStateEntity extends LovelaceCard<IBatteryEntityConfig> {
entityId: this.config.entity,
}, this.hass!);
}

this.addEventListener("click", this.action);
this.classList.add("clickable");
}
Expand Down Expand Up @@ -162,28 +164,6 @@ const getName = (config: IBatteryEntityConfig, hass: HomeAssistant | undefined):
return name;
}

/**
* Gets secondary info text
* @param config Entity config
* @param hass HomeAssistant state object
* @param isCharging Whther battery is in charging mode
* @returns Secondary info text
*/
const getSecondaryInfo = (config: IBatteryEntityConfig, hass: HomeAssistant | undefined, isCharging: boolean): string | Date => {
if (config.secondary_info) {
const processor = new RichStringProcessor(hass, config.entity, {
"charging": isCharging ? (config.charging_state?.secondary_info_text || "Charging") : "" // todo: think about i18n
});

let result = processor.process(config.secondary_info);

const dateVal = Date.parse(result);
return isNaN(dateVal) ? result : new Date(dateVal);
}

return <any>null;
}

/**
* Getts battery level/state
* @param config Entity config
Expand Down Expand Up @@ -218,7 +198,9 @@ const getBatteryLevel = (config: IBatteryEntityConfig, hass?: HomeAssistant): st
entityData.state
];

level = candidates.find(n => n !== null && n !== undefined)?.toString() || UnknownLevel;
level = candidates.find(val => isNumber(val)) ||
candidates.find(val => val !== null && val !== undefined)?.toString() ||
UnknownLevel
}

// check if we should convert value eg. for binary sensors
Expand Down Expand Up @@ -302,68 +284,3 @@ const getIcon = (config: IBatteryEntityConfig, level: number, isCharging: boolea
return (isCharging ? "mdi:battery-charging-" : "mdi:battery-") + roundedLevel;
}
}

/**
* Gets flag indicating charging mode
* @param config Entity config
* @param state Battery level/state
* @param hass HomeAssistant state object
* @returns Whether battery is in chargin mode
*/
const getChargingState = (config: IBatteryEntityConfig, state: string, hass?: HomeAssistant): boolean => {
const chargingConfig = config.charging_state;
if (!chargingConfig || !hass) {
return false;
}

let entityWithChargingState = hass.states[config.entity];

// check whether we should use different entity to get charging state
if (chargingConfig.entity_id) {
entityWithChargingState = hass.states[chargingConfig.entity_id]
if (!entityWithChargingState) {
log(`'charging_state' entity id (${chargingConfig.entity_id}) not found`);
return false;
}

state = entityWithChargingState.state;
}

const attributesLookup = safeGetArray(chargingConfig.attribute);
// check if we should take the state from attribute
if (attributesLookup.length != 0) {
// take first attribute name which exists on entity
const exisitngAttrib = attributesLookup.find(attr => getValueFromJsonPath(entityWithChargingState.attributes, attr.name) !== undefined);
if (exisitngAttrib) {
return exisitngAttrib.value !== undefined ?
getValueFromJsonPath(entityWithChargingState.attributes, exisitngAttrib.name) == exisitngAttrib.value :
true;
}
else {
// if there is no attribute indicating charging it means the charging state is false
return false;
}
}

const statesIndicatingCharging = safeGetArray(chargingConfig.state);

return statesIndicatingCharging.length == 0 ? !!state : statesIndicatingCharging.some(s => s == state);
}

/**
* Returns value from given object and the path
* @param data Data
* @param path JSON path
* @returns Value from the path
*/
const getValueFromJsonPath = (data: any, path: string) => {
if (data === undefined) {
return data;
}

path.split(".").forEach(chunk => {
data = data ? data[chunk] : undefined;
});

return data;
}
2 changes: 1 addition & 1 deletion src/custom-elements/battery-state-entity.views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const batteryHtml = (model: BatteryStateEntity) => html`
${icon(model.icon, model.iconColor)}
<div class="name truncate">
${model.name}
${typeof(model.secondaryInfo) == "string" ? secondaryInfo(model.secondaryInfo) : secondaryInfoTime(model.hass, model.secondaryInfo)}
${model.secondaryInfo instanceof Date ? secondaryInfoTime(model.hass, model.secondaryInfo) : secondaryInfo(model.secondaryInfo)}
</div>
<div class="state">
${model.state}${model.unit}
Expand Down
Loading

0 comments on commit 23ec841

Please sign in to comment.