Skip to content

Commit

Permalink
remove array comparison logic from is_state_attr and is_device_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerwyn committed Oct 9, 2024
1 parent 884acbf commit e05c757
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Functions used to determine an entity's state or an attribute.
| states | function, filter | entity_id, rounded (optional), with_unit (optional) | Returns the state string of the given entity. Optionally round numerical states and append the unit of measurement. |
| is_state | function, | entity_id, value | Compares an entity's state with a specified state or list of states and returns `true` or `false`. |
| state_attr | function, filter | entity_id, attribute | Returns the value of the attribute or `undefined` if it doesn't exist. |
| is_state_attr | function, | entity_id, attribute, value | Tests if the given entity attribute is the specified value. |
| is_state_attr | function | entity_id, attribute, value | Tests if the given entity attribute is the specified value. |
| has_value | function, filter | entity_id | Tests if the given entity is not unknown or unavailable. |

### [State Translated](https://www.home-assistant.io/docs/configuration/templating/#state-translated)
Expand Down
11 changes: 1 addition & 10 deletions dist/utils/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ export function device_attr(hass, device_or_entity_id, attr_name) {
export function is_device_attr(hass, device_or_entity_id, attr_name, attr_value) {
try {
if (attr_value != undefined) {
const deviceAttr = device_attr(hass, device_or_entity_id, attr_name);
if (typeof attr_value == 'string' &&
attr_value.startsWith('[') &&
attr_value.endsWith(']')) {
attr_value = JSON.parse(attr_value);
}
if (Array.isArray(attr_value)) {
return attr_value.includes(deviceAttr);
}
return deviceAttr == attr_value;
return (device_attr(hass, device_or_entity_id, attr_name) == attr_value);
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/utils/states.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { HomeAssistant } from '../models/hass';
export declare function states(hass: HomeAssistant, entity_id: string, rounded?: boolean | Record<string, boolean>, with_unit?: boolean): string | undefined;
export declare function is_state(hass: HomeAssistant, entity_id: string, value: string | string[]): boolean;
export declare function state_attr(hass: HomeAssistant, entity_id: string, attribute: string): any;
export declare function is_state_attr(hass: HomeAssistant, entity_id: string, attribute: string, value: string | string[]): boolean;
export declare function is_state_attr(hass: HomeAssistant, entity_id: string, attribute: string, value: string): boolean;
export declare function has_value(hass: HomeAssistant, entity_id: string): boolean;
export declare function buildStatesObject(hass: HomeAssistant): Record<string, Record<string, HassEntity>>;
11 changes: 1 addition & 10 deletions dist/utils/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,7 @@ export function state_attr(hass, entity_id, attribute) {
}
export function is_state_attr(hass, entity_id, attribute, value) {
try {
const stateAttr = state_attr(hass, entity_id, attribute);
if (typeof value == 'string' &&
value.startsWith('[') &&
value.endsWith(']')) {
value = JSON.parse(value);
}
if (Array.isArray(value)) {
return value.includes(stateAttr);
}
return stateAttr == value;
return state_attr(hass, entity_id, attribute) == value;
}
catch {
return false;
Expand Down
17 changes: 2 additions & 15 deletions src/utils/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,9 @@ export function is_device_attr(
) {
try {
if (attr_value != undefined) {
const deviceAttr = device_attr(
hass,
device_or_entity_id,
attr_name,
return (
device_attr(hass, device_or_entity_id, attr_name) == attr_value
);
if (
typeof attr_value == 'string' &&
attr_value.startsWith('[') &&
attr_value.endsWith(']')
) {
attr_value = JSON.parse(attr_value);
}
if (Array.isArray(attr_value)) {
return attr_value.includes(deviceAttr);
}
return deviceAttr == attr_value;
}
return false;
} catch {
Expand Down
1 change: 0 additions & 1 deletion src/utils/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function labels(hass: HomeAssistant, lookup_value?: string) {
if (!lookup_value) {
return Object.keys(labelRegistry);
}

return (
hass.entities[lookup_value]?.labels ??
hass.devices[lookup_value]?.labels ??
Expand Down
15 changes: 2 additions & 13 deletions src/utils/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,10 @@ export function is_state_attr(
hass: HomeAssistant,
entity_id: string,
attribute: string,
value: string | string[],
value: string,
) {
try {
const stateAttr = state_attr(hass, entity_id, attribute);
if (
typeof value == 'string' &&
value.startsWith('[') &&
value.endsWith(']')
) {
value = JSON.parse(value);
}
if (Array.isArray(value)) {
return value.includes(stateAttr);
}
return stateAttr == value;
return state_attr(hass, entity_id, attribute) == value;
} catch {
return false;
}
Expand Down
8 changes: 0 additions & 8 deletions tests/utils/devices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ describe('is_device_attr', () => {
),
true,
);

assert.equal(
renderTemplate(
hass,
'{{ is_device_attr("08d6a7f58fc934fba97d2ec2a66e7bba", "name" ,["foobar", 7, "Bar Light 1"]) }}',
),
true,
);
});

it('is_device_attr should act as is_state_attr if provided ID is not for a device', () => {
Expand Down
16 changes: 0 additions & 16 deletions tests/utils/states.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,6 @@ describe('is_state_attr', () => {
false,
);
});
it('should also work with arrays', () => {
assert.equal(
renderTemplate(
hass,
'{{ is_state_attr("light.lounge", "brightness", [155, "bar"]) }}',
),
true,
);
assert.equal(
renderTemplate(
hass,
'{{ is_state_attr("light.lounge", "brightness", [154, "bar"]) }}',
),
false,
);
});
});

describe('has_value', () => {
Expand Down

0 comments on commit e05c757

Please sign in to comment.