Rename several view variables for clarity.
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
import { Timer } from '../utils/timer';
|
||||
import { CardAutoRefreshAPI } from './types';
|
||||
|
||||
export class AutoUpdateManager {
|
||||
protected _timer = new Timer();
|
||||
protected _api: CardAutoRefreshAPI;
|
||||
|
||||
constructor(api: CardAutoRefreshAPI) {
|
||||
this._api = api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the update timer to trigger an update refresh every
|
||||
* `view.update_seconds`.
|
||||
*/
|
||||
public startDefaultViewTimer(): void {
|
||||
this._timer.stop();
|
||||
const updateSeconds = this._api.getConfigManager().getConfig()?.view.update_seconds;
|
||||
if (updateSeconds) {
|
||||
this._timer.start(updateSeconds, () => {
|
||||
if (this._isAutomatedUpdateAllowed()) {
|
||||
this._api.getViewManager().setViewDefault();
|
||||
} else {
|
||||
// Not allowed to update this time around, but try again at the next
|
||||
// interval.
|
||||
this.startDefaultViewTimer();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected _isAutomatedUpdateAllowed(): boolean {
|
||||
const triggers = this._api.getTriggersManager();
|
||||
const config = this._api.getConfigManager().getConfig();
|
||||
const interactionManager = this._api.getInteractionManager();
|
||||
|
||||
return (
|
||||
!triggers.isTriggered() &&
|
||||
(config?.view.update_force || !interactionManager.hasInteraction())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@ export class CardElementManager {
|
||||
this._api.getMediaLoadedInfoManager().initialize();
|
||||
this._api.getMicrophoneManager().initialize();
|
||||
this._api.getKeyboardStateManager().initialize();
|
||||
this._api.getDefaultManager().initialize();
|
||||
|
||||
// Whether or not the card is in panel mode on the dashboard.
|
||||
setOrRemoveAttribute(this._element, isCardInPanel(this._element), 'panel');
|
||||
@@ -123,6 +124,7 @@ export class CardElementManager {
|
||||
this._api.getFullscreenManager().disconnect();
|
||||
this._api.getKeyboardStateManager().uninitialize();
|
||||
this._api.getActionsManager().uninitialize();
|
||||
this._api.getDefaultManager().uninitialize();
|
||||
|
||||
// Uninitialize cameras to cause them to reinitialize on
|
||||
// reconnection, to ensure the state subscription/unsubscription works
|
||||
|
||||
@@ -142,6 +142,18 @@ export class ConfigManager {
|
||||
.uninitialize(InitializationAspect.MICROPHONE_CONNECT);
|
||||
}
|
||||
|
||||
if (
|
||||
previousConfig &&
|
||||
!isEqual(
|
||||
previousConfig?.view.default_reset,
|
||||
this._overriddenConfig?.view.default_reset,
|
||||
)
|
||||
) {
|
||||
this._api
|
||||
.getInitializationManager()
|
||||
.uninitialize(InitializationAspect.DEFAULT_RESET);
|
||||
}
|
||||
|
||||
this._api.getCardElementManager().update();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import { EntityRegistryManager } from '../utils/ha/entity-registry';
|
||||
import { EntityCache } from '../utils/ha/entity-registry/cache';
|
||||
import { ResolvedMediaCache } from '../utils/ha/resolved-media';
|
||||
import { ActionsManager } from './actions/actions-manager';
|
||||
import { AutoUpdateManager } from './auto-update-manager';
|
||||
import { DefaultManager } from './default-manager';
|
||||
import { AutomationsManager } from './automations-manager';
|
||||
import { CameraURLManager } from './camera-url-manager';
|
||||
import {
|
||||
@@ -32,12 +32,12 @@ import { StyleManager } from './style-manager';
|
||||
import { TriggersManager } from './triggers-manager';
|
||||
import {
|
||||
CardActionsManagerAPI,
|
||||
CardAutoRefreshAPI,
|
||||
CardAutomationsAPI,
|
||||
CardCameraAPI,
|
||||
CardCameraURLAPI,
|
||||
CardConditionAPI,
|
||||
CardConfigAPI,
|
||||
CardDefaultManagerAPI,
|
||||
CardDownloadAPI,
|
||||
CardElementAPI,
|
||||
CardExpandAPI,
|
||||
@@ -62,11 +62,11 @@ export class CardController
|
||||
implements
|
||||
CardActionsManagerAPI,
|
||||
CardAutomationsAPI,
|
||||
CardAutoRefreshAPI,
|
||||
CardCameraAPI,
|
||||
CardCameraURLAPI,
|
||||
CardConditionAPI,
|
||||
CardConfigAPI,
|
||||
CardDefaultManagerAPI,
|
||||
CardDownloadAPI,
|
||||
CardElementAPI,
|
||||
CardExpandAPI,
|
||||
@@ -92,12 +92,12 @@ export class CardController
|
||||
|
||||
protected _actionsManager = new ActionsManager(this);
|
||||
protected _automationsManager = new AutomationsManager(this);
|
||||
protected _autoUpdateManager = new AutoUpdateManager(this);
|
||||
protected _cameraManager = new CameraManager(this);
|
||||
protected _cameraURLManager = new CameraURLManager(this);
|
||||
protected _cardElementManager: CardElementManager;
|
||||
protected _conditionsManager: ConditionsManager;
|
||||
protected _configManager = new ConfigManager(this);
|
||||
protected _defaultManager = new DefaultManager(this);
|
||||
protected _downloadManager = new DownloadManager(this);
|
||||
protected _expandManager = new ExpandManager(this);
|
||||
protected _fullscreenManager = new FullscreenManager(this);
|
||||
@@ -143,10 +143,6 @@ export class CardController
|
||||
return this._automationsManager;
|
||||
}
|
||||
|
||||
public getAutoUpdateManager(): AutoUpdateManager {
|
||||
return this._autoUpdateManager;
|
||||
}
|
||||
|
||||
public getCameraManager(): CameraManager {
|
||||
return this._cameraManager;
|
||||
}
|
||||
@@ -171,6 +167,11 @@ export class CardController
|
||||
public getConfigManager(): ConfigManager {
|
||||
return this._configManager;
|
||||
}
|
||||
|
||||
public getDefaultManager(): DefaultManager {
|
||||
return this._defaultManager;
|
||||
}
|
||||
|
||||
public getDownloadManager(): DownloadManager {
|
||||
return this._downloadManager;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import PQueue from 'p-queue';
|
||||
import { DestroyCallback, subscribeToTrigger } from '../utils/ha';
|
||||
import { isActionAllowedBasedOnInteractionState } from '../utils/interaction-mode';
|
||||
import { Timer } from '../utils/timer';
|
||||
import { CardDefaultManagerAPI } from './types';
|
||||
|
||||
/**
|
||||
* Manages automated resetting to the default view.
|
||||
*/
|
||||
export class DefaultManager {
|
||||
protected _timer = new Timer();
|
||||
protected _api: CardDefaultManagerAPI;
|
||||
protected _unsubscribeCallback: DestroyCallback | null = null;
|
||||
protected _initializationLimit = new PQueue({ concurrency: 1 });
|
||||
|
||||
constructor(api: CardDefaultManagerAPI) {
|
||||
this._api = api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the default manager. Requires both hass and configuration to be
|
||||
* effective (so cannot be called from just the configuration manager, as hass
|
||||
* will not be available yet)
|
||||
*/
|
||||
public async initialize(): Promise<boolean> {
|
||||
const result = await this._initializationLimit.add(() => this._reconfigure());
|
||||
this._startTimer();
|
||||
return !!result;
|
||||
}
|
||||
|
||||
public uninitialize(): void {
|
||||
this._timer.stop();
|
||||
this._unsubscribeCallback?.();
|
||||
this._unsubscribeCallback = null;
|
||||
}
|
||||
|
||||
protected async _reconfigure(): Promise<boolean> {
|
||||
const hass = this._api.getHASSManager().getHASS();
|
||||
const config = this._api.getConfigManager().getConfig()?.view.default_reset;
|
||||
if (!hass || !config) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this._unsubscribeCallback) {
|
||||
await this._unsubscribeCallback();
|
||||
}
|
||||
|
||||
this._unsubscribeCallback = await subscribeToTrigger(
|
||||
hass,
|
||||
() => this._setToDefaultIfAllowed(),
|
||||
{
|
||||
entityID: config.entities,
|
||||
platform: 'state',
|
||||
stateOnly: true,
|
||||
},
|
||||
);
|
||||
|
||||
// If the timer is running, restart it with the newly configured timer.
|
||||
if (this._timer.isRunning()) {
|
||||
this._timer.stop();
|
||||
this._startTimer();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected _setToDefaultIfAllowed(): void {
|
||||
if (this._isAutomatedUpdateAllowed()) {
|
||||
this._api.getViewManager().setViewDefault();
|
||||
}
|
||||
}
|
||||
|
||||
protected _isAutomatedUpdateAllowed(): boolean {
|
||||
const interactionMode = this._api.getConfigManager().getConfig()?.view
|
||||
.default_reset.interaction_mode;
|
||||
return (
|
||||
!!interactionMode &&
|
||||
isActionAllowedBasedOnInteractionState(
|
||||
interactionMode,
|
||||
this._api.getInteractionManager().hasInteraction(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected _startTimer(): void {
|
||||
const timerSeconds = this._api.getConfigManager().getConfig()?.view
|
||||
.default_reset.every_seconds;
|
||||
if (timerSeconds) {
|
||||
this._timer.startRepeated(timerSeconds, () => this._setToDefaultIfAllowed());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,23 +37,6 @@ export class HASSManager {
|
||||
this._hass = hass;
|
||||
|
||||
if (
|
||||
// Home Assistant pumps a lot of updates through. Re-rendering the card is
|
||||
// necessary at times (e.g. to update the 'clip' view as new clips
|
||||
// arrive), but also is a jarring experience for the user (e.g. if they
|
||||
// are browsing the mini-gallery). Do not allow re-rendering from a Home
|
||||
// Assistant update if there's been recent interaction (e.g. clicks on the
|
||||
// card) or if there is media active playing.
|
||||
this._isAutomatedViewUpdateAllowed() &&
|
||||
isHassDifferent(
|
||||
this._hass,
|
||||
oldHass,
|
||||
this._api.getConfigManager().getConfig()?.view.update_entities ?? [],
|
||||
)
|
||||
) {
|
||||
// If entities being monitored have changed then reset the view to the
|
||||
// default.
|
||||
this._api.getViewManager().setViewDefault();
|
||||
} else if (
|
||||
isHassDifferent(this._hass, oldHass, [
|
||||
...(this._api.getConfigManager().getConfig()?.view.render_entities ?? []),
|
||||
|
||||
@@ -75,11 +58,4 @@ export class HASSManager {
|
||||
// Dark mode may depend on HASS.
|
||||
this._api.getStyleManager().setLightOrDarkMode();
|
||||
}
|
||||
|
||||
protected _isAutomatedViewUpdateAllowed(): boolean {
|
||||
return (
|
||||
this._api.getConfigManager().getConfig()?.view.update_force ||
|
||||
!this._api.getInteractionManager().hasInteraction()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ export enum InitializationAspect {
|
||||
MEDIA_PLAYERS = 'media-players',
|
||||
CAMERAS = 'cameras',
|
||||
MICROPHONE_CONNECT = 'microphone-connect',
|
||||
DEFAULT_RESET = 'default-reset',
|
||||
}
|
||||
|
||||
export class InitializationManager {
|
||||
@@ -125,6 +126,7 @@ export class InitializationManager {
|
||||
|
||||
if (
|
||||
this._initializer.isInitializedMultiple([
|
||||
InitializationAspect.DEFAULT_RESET,
|
||||
...(config.menu.buttons.media_player.enabled
|
||||
? [InitializationAspect.MEDIA_PLAYERS]
|
||||
: []),
|
||||
@@ -135,6 +137,8 @@ export class InitializationManager {
|
||||
|
||||
if (
|
||||
!(await this._initializer.initializeMultipleIfNecessary({
|
||||
[InitializationAspect.DEFAULT_RESET]: async () =>
|
||||
await this._api.getDefaultManager().initialize(),
|
||||
...(config.menu.buttons.media_player.enabled && {
|
||||
[InitializationAspect.MEDIA_PLAYERS]: async () =>
|
||||
await this._api.getMediaPlayerManager().initialize(),
|
||||
|
||||
@@ -41,7 +41,10 @@ export class InteractionManager {
|
||||
this._api.getConditionsManager().setState({ interaction: false });
|
||||
|
||||
if (!this._api.getTriggersManager().isTriggered()) {
|
||||
if (this._api.getConfigManager().getConfig()?.view.reset_after_interaction) {
|
||||
if (
|
||||
this._api.getConfigManager().getConfig()?.view.default_reset
|
||||
.after_interaction
|
||||
) {
|
||||
this._api.getViewManager().setViewDefault();
|
||||
}
|
||||
this._api.getStyleManager().setLightOrDarkMode();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { CardKeyboardStateAPI, KeysState } from './types';
|
||||
import isEqual from 'lodash/isEqual';
|
||||
import isEqual from 'lodash-es/isEqual';
|
||||
|
||||
export class KeyboardStateManager {
|
||||
protected _api: CardKeyboardStateAPI;
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { ConditionsManager } from './conditions-manager';
|
||||
import type { EntityRegistryManager } from '../utils/ha/entity-registry';
|
||||
import type { ResolvedMediaCache } from '../utils/ha/resolved-media';
|
||||
import type { ActionsManager } from './actions/actions-manager';
|
||||
import type { AutoUpdateManager } from './auto-update-manager';
|
||||
import type { DefaultManager } from './default-manager';
|
||||
import type { AutomationsManager } from './automations-manager';
|
||||
import type { CameraURLManager } from './camera-url-manager';
|
||||
import type { CardElementManager } from './card-element-manager';
|
||||
@@ -60,13 +60,6 @@ export interface CardAutomationsAPI {
|
||||
getMessageManager(): MessageManager;
|
||||
}
|
||||
|
||||
export interface CardAutoRefreshAPI {
|
||||
getConfigManager(): ConfigManager;
|
||||
getInteractionManager(): InteractionManager;
|
||||
getTriggersManager(): TriggersManager;
|
||||
getViewManager(): ViewManager;
|
||||
}
|
||||
|
||||
export interface CardCameraAPI {
|
||||
getActionsManager(): ActionsManager;
|
||||
getConfigManager(): ConfigManager;
|
||||
@@ -92,6 +85,7 @@ export interface CardConfigAPI {
|
||||
getCardElementManager(): CardElementManager;
|
||||
getConditionsManager(): ConditionsManager;
|
||||
getConfigManager(): ConfigManager;
|
||||
getDefaultManager(): DefaultManager;
|
||||
getInitializationManager(): InitializationManager;
|
||||
getMediaLoadedInfoManager(): MediaLoadedInfoManager;
|
||||
getMessageManager(): MessageManager;
|
||||
@@ -104,6 +98,14 @@ export interface CardConfigLoaderAPI {
|
||||
getAutomationsManager(): AutomationsManager;
|
||||
}
|
||||
|
||||
export interface CardDefaultManagerAPI {
|
||||
getConfigManager(): ConfigManager;
|
||||
getHASSManager(): HASSManager;
|
||||
getInteractionManager(): InteractionManager;
|
||||
getTriggersManager(): TriggersManager;
|
||||
getViewManager(): ViewManager;
|
||||
}
|
||||
|
||||
export interface CardDownloadAPI {
|
||||
getCameraManager(): CameraManager;
|
||||
getHASSManager(): HASSManager;
|
||||
@@ -115,6 +117,7 @@ export interface CardDownloadAPI {
|
||||
export interface CardElementAPI {
|
||||
getActionsManager(): ActionsManager;
|
||||
getCameraManager(): CameraManager;
|
||||
getDefaultManager(): DefaultManager;
|
||||
getExpandManager(): ExpandManager;
|
||||
getFullscreenManager(): FullscreenManager;
|
||||
getInitializationManager(): InitializationManager;
|
||||
@@ -143,6 +146,7 @@ export interface CardHASSAPI {
|
||||
getCardElementManager(): CardElementManager;
|
||||
getConditionsManager(): ConditionsManager;
|
||||
getConfigManager(): ConfigManager;
|
||||
getDefaultManager(): DefaultManager;
|
||||
getInteractionManager(): InteractionManager;
|
||||
getMediaPlayerManager(): MediaPlayerManager;
|
||||
getMessageManager(): MessageManager;
|
||||
@@ -155,6 +159,7 @@ export interface CardInitializerAPI {
|
||||
getCameraManager(): CameraManager;
|
||||
getCardElementManager(): CardElementManager;
|
||||
getConfigManager(): ConfigManager;
|
||||
getDefaultManager(): DefaultManager;
|
||||
getEntityRegistryManager(): EntityRegistryManager;
|
||||
getHASSManager(): HASSManager;
|
||||
getMediaPlayerManager(): MediaPlayerManager;
|
||||
@@ -233,7 +238,6 @@ export interface CardTriggersAPI {
|
||||
}
|
||||
|
||||
export interface CardViewAPI {
|
||||
getAutoUpdateManager(): AutoUpdateManager;
|
||||
getCameraManager(): CameraManager;
|
||||
getCardElementManager(): CardElementManager;
|
||||
getConditionsManager(): ConditionsManager;
|
||||
|
||||
@@ -55,7 +55,7 @@ export class ViewManager {
|
||||
let forceCameraID: string | null = params?.cameraID ?? null;
|
||||
const viewName = config.view.default;
|
||||
|
||||
if (!forceCameraID && this._view?.camera && config.view.update_cycle_camera) {
|
||||
if (!forceCameraID && this._view?.camera && config.view.default_cycle_camera) {
|
||||
const cameraIDs = [
|
||||
...getCameraIDsForViewName(this._api.getCameraManager(), viewName),
|
||||
];
|
||||
@@ -69,10 +69,6 @@ export class ViewManager {
|
||||
viewName: viewName,
|
||||
...(forceCameraID && { cameraID: forceCameraID }),
|
||||
});
|
||||
|
||||
// Restart the refresh timer, so the default view is refreshed at a fixed
|
||||
// interval from now (if so configured).
|
||||
this._api.getAutoUpdateManager().startDefaultViewTimer();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user