fix: Assume conditions are state conditions unless otherwise specified (#1552)
This commit is contained in:
@@ -212,7 +212,9 @@ export class ConditionsManager {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const conditions = getAllConditions();
|
const conditions = getAllConditions();
|
||||||
this._hasHAStateConditions = conditions.some((conditionObj) =>
|
this._hasHAStateConditions = conditions.some(
|
||||||
|
(conditionObj) =>
|
||||||
|
!conditionObj.condition ||
|
||||||
['state', 'numeric_state', 'user'].includes(conditionObj.condition),
|
['state', 'numeric_state', 'user'].includes(conditionObj.condition),
|
||||||
);
|
);
|
||||||
conditions.forEach((conditionObj) => {
|
conditions.forEach((conditionObj) => {
|
||||||
@@ -263,16 +265,7 @@ export class ConditionsManager {
|
|||||||
};
|
};
|
||||||
|
|
||||||
switch (conditionObj.condition) {
|
switch (conditionObj.condition) {
|
||||||
case 'view':
|
case undefined:
|
||||||
return !!state?.view && conditionObj.views.includes(state.view);
|
|
||||||
case 'fullscreen':
|
|
||||||
return (
|
|
||||||
state.fullscreen !== undefined && conditionObj.fullscreen === state.fullscreen
|
|
||||||
);
|
|
||||||
case 'expand':
|
|
||||||
return state.expand !== undefined && conditionObj.expand === state.expand;
|
|
||||||
case 'camera':
|
|
||||||
return !!state.camera && conditionObj.cameras.includes(state.camera);
|
|
||||||
case 'state':
|
case 'state':
|
||||||
return (
|
return (
|
||||||
!!state.state &&
|
!!state.state &&
|
||||||
@@ -289,6 +282,16 @@ export class ConditionsManager {
|
|||||||
)
|
)
|
||||||
: conditionObj.state_not !== state.state[conditionObj.entity].state))))
|
: conditionObj.state_not !== state.state[conditionObj.entity].state))))
|
||||||
);
|
);
|
||||||
|
case 'view':
|
||||||
|
return !!state?.view && conditionObj.views.includes(state.view);
|
||||||
|
case 'fullscreen':
|
||||||
|
return (
|
||||||
|
state.fullscreen !== undefined && conditionObj.fullscreen === state.fullscreen
|
||||||
|
);
|
||||||
|
case 'expand':
|
||||||
|
return state.expand !== undefined && conditionObj.expand === state.expand;
|
||||||
|
case 'camera':
|
||||||
|
return !!state.camera && conditionObj.cameras.includes(state.camera);
|
||||||
case 'numeric_state':
|
case 'numeric_state':
|
||||||
return (
|
return (
|
||||||
!!state.state &&
|
!!state.state &&
|
||||||
|
|||||||
+4
-1
@@ -518,7 +518,10 @@ const imageSchema = elementsBaseSchema.extend({
|
|||||||
|
|
||||||
// https://www.home-assistant.io/dashboards/conditional/#state
|
// https://www.home-assistant.io/dashboards/conditional/#state
|
||||||
const stateConditionSchema = z.object({
|
const stateConditionSchema = z.object({
|
||||||
condition: z.literal('state'),
|
// If the condition is not specified, a state condition is assumed. This
|
||||||
|
// allows the syntax to match a picture elements conditional:
|
||||||
|
// https://www.home-assistant.io/dashboards/picture-elements/#conditional-element
|
||||||
|
condition: z.literal('state').optional(),
|
||||||
entity: z.string(),
|
entity: z.string(),
|
||||||
state: z.string().or(z.string().array()).optional(),
|
state: z.string().or(z.string().array()).optional(),
|
||||||
state_not: z.string().or(z.string().array()).optional(),
|
state_not: z.string().or(z.string().array()).optional(),
|
||||||
|
|||||||
@@ -457,7 +457,8 @@ describe('ConditionsManager', () => {
|
|||||||
expect(manager.hasHAStateConditions()).toBeFalsy();
|
expect(manager.hasHAStateConditions()).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('with HA state conditions', () => {
|
describe('with HA state conditions', () => {
|
||||||
|
it('explicitly stated', () => {
|
||||||
const api = createCardAPI();
|
const api = createCardAPI();
|
||||||
const numericConfig = createSuitableConfig([
|
const numericConfig = createSuitableConfig([
|
||||||
{
|
{
|
||||||
@@ -473,6 +474,22 @@ describe('ConditionsManager', () => {
|
|||||||
expect(manager.hasHAStateConditions()).toBeTruthy();
|
expect(manager.hasHAStateConditions()).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('implicitly assumed', () => {
|
||||||
|
const api = createCardAPI();
|
||||||
|
const numericConfig = createSuitableConfig([
|
||||||
|
{
|
||||||
|
entity: 'binary_sensor.foo',
|
||||||
|
state: 'on',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
vi.mocked(api.getConfigManager().getConfig).mockReturnValue(numericConfig);
|
||||||
|
const manager = new ConditionsManager(api);
|
||||||
|
manager.setConditionsFromConfig();
|
||||||
|
|
||||||
|
expect(manager.hasHAStateConditions()).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('with HA numeric_state conditions', () => {
|
it('with HA numeric_state conditions', () => {
|
||||||
const api = createCardAPI();
|
const api = createCardAPI();
|
||||||
const numericStateConfig = createSuitableConfig([
|
const numericStateConfig = createSuitableConfig([
|
||||||
@@ -657,6 +674,23 @@ describe('ConditionsManager', () => {
|
|||||||
});
|
});
|
||||||
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('implicit state condition', () => {
|
||||||
|
const manager = new ConditionsManager(createCardAPI());
|
||||||
|
const conditions = [
|
||||||
|
{
|
||||||
|
entity: 'binary_sensor.foo',
|
||||||
|
state: 'on',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||||
|
manager.setState({ state: { 'binary_sensor.foo': createStateEntity() } });
|
||||||
|
expect(manager.evaluateConditions(conditions)).toBeTruthy();
|
||||||
|
manager.setState({
|
||||||
|
state: { 'binary_sensor.foo': createStateEntity({ state: 'off' }) },
|
||||||
|
});
|
||||||
|
expect(manager.evaluateConditions(conditions)).toBeFalsy();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('with numeric state condition', () => {
|
describe('with numeric state condition', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user