Skip to content

Commit

Permalink
improve conditions for person entities
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsfaber committed Feb 5, 2022
1 parent 309fb46 commit c84fef7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/standard-configuration/standardStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function standardStates(entity_id: string, hass: HomeAssistant): Variable
let options = [...stateConfig.options];
options = options.map(e =>
Object.assign(e, {
icon: e.icon ? e.icon : stateIcon(stateObj, e.value, DefaultActionIcon),
icon: e.icon ? e.icon : stateIcon(stateObj, e.value, hass, DefaultActionIcon),
name: e.name ? e.name : getStateName(stateObj, e.value, hass),
})
);
Expand Down
43 changes: 31 additions & 12 deletions src/standard-configuration/state_icons.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { computeDomain, stateIcon as HaStateIcon } from 'custom-card-helpers';
import { computeDomain, computeEntity, HomeAssistant, stateIcon as HaStateIcon } from 'custom-card-helpers';
import { HassEntity } from 'home-assistant-js-websocket';
import { DefaultEntityIcon } from '../const';

type IconItem = string | ((stateObj: HassEntity, state: string) => string);

type IconList = Record<string, Record<string, IconItem>>;
type Template = (stateObj: HassEntity, state: string, hass: HomeAssistant) => string;
type IconItem = string | Template;
type IconList = Record<string, Record<string, IconItem> | Template>;

const binarySensorIcon = (stateObj: HassEntity, state: string) => {
return HaStateIcon({ ...stateObj, state: state });
Expand All @@ -26,6 +26,24 @@ const coverIcon = (stateObj: HassEntity, state: string) => {
}
};

const personIcon = (_stateObj: HassEntity, state: string, hass: HomeAssistant) => {
let stateIcons: Record<string, string> = {
home: 'mdi:home-outline',
not_home: 'mdi:exit-run',
};

Object.keys(hass.states)
.filter(e => computeDomain(e) == 'zone')
.forEach(e => {
const name = computeEntity(e);
const icon = hass.states[e].attributes.icon;
if (!icon) return;
stateIcons[name] = icon;
});

return state in stateIcons ? stateIcons[state] : 'mdi:flash';
};

export const stateIcons: IconList = {
alarm_control_panel: {
disarmed: 'mdi:lock-open-variant-outline',
Expand Down Expand Up @@ -79,10 +97,7 @@ export const stateIcons: IconList = {
unlocked: 'mdi:lock-outline',
locked: 'mdi:lock-open-variant-outline',
},
person: {
home: 'mdi:home-outline',
not_home: 'mdi:exit-run',
},
person: personIcon,
sensor: {
unit: 'attributes.unit_of_measurement',
},
Expand All @@ -96,13 +111,17 @@ export const stateIcons: IconList = {
},
};

export const stateIcon = (stateObj: HassEntity, state?: string, fallback?: string) => {
export const stateIcon = (stateObj: HassEntity, state: string | undefined, hass: HomeAssistant, fallback?: string) => {
const domain = computeDomain(stateObj.entity_id);
if (!state) state = stateObj.state;

if (domain in stateIcons && state in stateIcons[domain]) {
const entry = stateIcons[domain][state];
return typeof entry == 'string' ? entry : entry(stateObj, state);
if (domain in stateIcons) {
if (state in stateIcons[domain]) {
const entry = stateIcons[domain][state];
return typeof entry == 'string' ? entry : entry(stateObj, state, hass);
} else if (typeof stateIcons[domain] == 'function') {
return (stateIcons[domain] as Template)(stateObj, state, hass);
}
}
return fallback || DefaultEntityIcon;
};
11 changes: 10 additions & 1 deletion src/standard-configuration/states.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { computeDomain, computeEntity } from 'custom-card-helpers';
import { numericAttribute, stringAttribute } from './attribute';
import { VariableConfig } from './variables';

Expand Down Expand Up @@ -41,7 +42,15 @@ export const statesList: Record<string, VariableConfig> = {
max: 'max',
step: 'step',
},
person: { options: ['home', 'not_home'] },
person: {
template: (_stateObj, hass) => {
let modes = ['home', 'not_home'];
let zones = Object.keys(hass.states)
.filter(e => computeDomain(e) == 'zone')
.map(computeEntity);
return { options: [...new Set([...modes, ...zones])] };
},
},
proximity: {
unit: 'unit_of_measurement',
},
Expand Down
12 changes: 2 additions & 10 deletions src/standard-configuration/variable_icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,8 @@ const actionIcons: IconList = {
},
};

export const getVariableOptionIcon = (
domain: string,
variable: string,
option: string
) => {
if (
domain in actionIcons &&
variable in actionIcons[domain] &&
option in actionIcons[domain][variable]
)
export const getVariableOptionIcon = (domain: string, variable: string, option: string) => {
if (domain in actionIcons && variable in actionIcons[domain] && option in actionIcons[domain][variable])
return actionIcons[domain][variable][option];
return;
};

0 comments on commit c84fef7

Please sign in to comment.