Files
houseplan-card/demo/smoke_light_default_tap.mjs

63 lines
3.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { launch, checkAll, finish } from './serve.mjs';
const { page, browser } = await launch();
const res = await page.evaluate(async () => {
const out = {};
const c = window.__card;
const calls = [];
c.hass = { ...c.hass, callService: (d, s, data) => { calls.push([d, s, data.entity_id]); return Promise.resolve(); } };
await c.updateComplete;
c._setMode('view'); await c.updateComplete;
// лампа (primary light) без явного действия → клик = toggle
const lamp = c._devices.find((d) => d.primary?.startsWith('light.') && !d.tapAction);
out.hasLamp = !!lamp;
const lampState = c.hass.states[lamp.primary]?.state;
const expectedService = lampState === 'off' ? 'turn_on'
: (lampState === 'unknown' || lampState === '' ? 'toggle' : 'turn_off');
c._infoCard = null;
c._clickDevice(new MouseEvent('click'), lamp);
const lampCall = calls.at(-1);
out.lampToggles = ['light', 'homeassistant'].includes(lampCall?.[0])
&& lampCall?.[1] === expectedService && lampCall?.[2] === lamp.primary;
// The untouched select is an effective projection, not a frozen draft.
// Reproduce the field race where a dialog was opened before HA identified
// the leading entity as light: runtime already sees light-default toggle,
// and the UI must catch up without materializing a stored tap_action.
c._setMode('devices'); await c.updateComplete;
c._openMarkerDialog(lamp); await c.updateComplete;
c._markerDialog = {
...c._markerDialog,
tapAction: 'info', tapActionTouched: false,
originalHasTapAction: false, originalTapAction: undefined,
};
await c.updateComplete;
const tapSelect = c.renderRoot.querySelector('#marker-tap-action');
out.defaultLampDialogShowsEffectiveToggle = tapSelect?.value === 'toggle';
out.defaultLampActionRemainsUnmaterialized = !c._markerDialog.tapActionTouched
&& !c._markerDialog.originalHasTapAction;
tapSelect.value = 'info';
tapSelect.dispatchEvent(new Event('change'));
await c.updateComplete;
out.explicitDeviceCardStaysSelected = c.renderRoot.querySelector('#marker-tap-action')?.value === 'info'
&& c._markerDialog.tapActionTouched;
c._markerDialog = null;
c._setMode('view'); await c.updateComplete;
// устройство с не-light primary (сенсор) → клик = инфо
const sensorDev = c._devices.find((d) => d.primary?.startsWith('sensor.') && !d.tapAction);
const n = calls.length;
c._infoCard = null;
c._clickDevice(new MouseEvent('click'), sensorDev);
out.sensorInfo = calls.length === n && !!c._infoCard;
c._infoCard = null;
// явный info у лампы отключает дефолт
const cfgm = { id: lamp.id, binding: lamp.bindingKind + ':' + lamp.bindingRef, tap_action: 'info' };
c._serverCfg = { ...c._serverCfg, markers: [...(c._serverCfg.markers || []).filter((m) => m.id !== lamp.id), cfgm] };
c._regSignature = ''; c._maybeRebuildDevices(); await c.updateComplete;
const lamp2 = c._devices.find((d) => d.id === lamp.id);
const n2 = calls.length;
c._clickDevice(new MouseEvent('click'), lamp2);
out.explicitInfoWins = calls.length === n2 && !!c._infoCard;
return out;
});
checkAll(res);
await finish(browser, res);