The `initialized` state (used in conditions/triggers) was written once and never cleared, so it meant "has this card ever been initialized" while everything reading it took it as "is this card usable now". Home Assistant takes a card off the page and puts it back whenever its dashboard tab is left and returned to, so the card initialized again while the state claimed it was initialized throughout: `trigger: initialized` fired once per card rather than once per startup, and automations were dropped in between. The card lifecycle is now an explicit state machine (`SessionManager`), the only writer of `initialized`, which separates a card that is starting up from one initializing part of itself again while it runs. A new `ever` parameter (conditions/triggers) selects the old latched behaviour. `remote_control` uses that parameter to keep its two camera priorities correct under repeated starts. With `camera_priority: entity` the card now re-reads the entity every time it starts, so a camera selected while the card was away is picked up on return. With `camera_priority: card` the card writes the entity on its first start only, unchanged, since repeating that write would overwrite a camera the user had selected. Closes: #2642 BREAKING CHANGE: `condition: initialized` is now `false` whenever the card is not usable, and `trigger: initialized` fires each time the card starts up rather than only the first time. Set `ever: true` on either to keep the previous behaviour.
70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import type { MediaLoadedInfoEventDetail } from '../../src/types';
|
|
import { MountedCard } from '../browser/mounted-card';
|
|
import {
|
|
createStillCameraHASS,
|
|
createStillImageCardConfig,
|
|
isMediaLoadedInfoEventDetail,
|
|
STILL_CAMERA_ENTITY,
|
|
} from '../browser/test-utils';
|
|
|
|
const UNRELATED_ENTITY = 'input_boolean.unrelated';
|
|
|
|
// The size of the static image fixture the player loads. Everything downstream
|
|
// sizes the card from what the player measured, so these are read from the
|
|
// media rather than declared anywhere in the configuration.
|
|
const FIXTURE_WIDTH = 320;
|
|
const FIXTURE_HEIGHT = 180;
|
|
|
|
interface RenderedElement extends Element {
|
|
updateComplete: Promise<boolean>;
|
|
}
|
|
|
|
const mount = async (): Promise<MountedCard> => {
|
|
const hass = createStillCameraHASS({ entities: { [UNRELATED_ENTITY]: 'off' } });
|
|
return await MountedCard.create(createStillImageCardConfig(), hass);
|
|
};
|
|
|
|
const getMediaLoadedInfos = (card: MountedCard): MediaLoadedInfoEventDetail[] =>
|
|
card.events
|
|
.getEntries('advanced-camera-card:media:loaded')
|
|
.map((entry) => entry.detail)
|
|
.filter(isMediaLoadedInfoEventDetail);
|
|
|
|
describe('AdvancedCameraCardImageUpdatingPlayer', () => {
|
|
it('should announce the media load once even when the card re-renders', async () => {
|
|
const card = await mount();
|
|
|
|
const player = await card.waitForSelector<RenderedElement>(
|
|
'advanced-camera-card-image-updating-player',
|
|
);
|
|
await card.events.waitForFirst('advanced-camera-card:media:loaded');
|
|
|
|
// Something changes that the media knows nothing about. Announcing the
|
|
// media load here would mean the player had quietly reloaded, which is how
|
|
// a stream that churns looks from the outside.
|
|
card.setEntityState(UNRELATED_ENTITY, 'on');
|
|
|
|
// The card renders, and then the player does: each level only asks the next
|
|
// to render once it has rendered itself, so a second announcement would
|
|
// arrive by the end of this.
|
|
await card.updateComplete;
|
|
await player.updateComplete;
|
|
|
|
const loads = getMediaLoadedInfos(card);
|
|
expect(loads).toHaveLength(1);
|
|
expect(loads[0].info.targetID).toBe(STILL_CAMERA_ENTITY);
|
|
});
|
|
|
|
it('should announce the size of the media itself', async () => {
|
|
const card = await mount();
|
|
|
|
await card.events.waitForFirst('advanced-camera-card:media:loaded');
|
|
|
|
const [load] = getMediaLoadedInfos(card);
|
|
expect(load.info.width).toBe(FIXTURE_WIDTH);
|
|
expect(load.info.height).toBe(FIXTURE_HEIGHT);
|
|
});
|
|
});
|