Skip to content

Commit

Permalink
New conditional system for pop-up triggers + Fixes
Browse files Browse the repository at this point in the history
- [x] Fan modes button error when HVAC is in Dry Mode #987
- [x] Enhancement: Add Box-Shadow Customization to Bubble Card. Fix added for PR #1009 by @flobiwankenobi
- [x] Better color handling for lights, now if the light doesn’t supports RGB it will take the accent color instead like the default Home Assistant behavior. #692
- [x] Templating .bubble-name is now instant. #975
- [x] Fixed an issue where some entity_picture url were not displayed.
- [x] New pop-up trigger system based on the same conditional system that Home Assistant use. Not to be confused with the visibility conditions. Note: This is NOT a breaking change, if you previously added a trigger in a pop-up, it will still works, but the editor will not display it anymore and it will be visible in YAML mode only. Then if you decide to use the new system, it will be used instead of your previous trigger. This new feature is based on the code from @MrBearPresident, so thanks again to him for this great new possibility! #332
  • Loading branch information
Clooos authored Dec 12, 2024
1 parent d381295 commit 7fde037
Show file tree
Hide file tree
Showing 19 changed files with 266 additions and 199 deletions.
195 changes: 103 additions & 92 deletions dist/bubble-card.js

Large diffs are not rendered by default.

19 changes: 3 additions & 16 deletions src/cards/button/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function changeIcon(context) {
}

export function changeName(context) {
if (context.config.styles?.includes("card.querySelector('.bubble-name').innerText")) return;
const buttonType = getButtonType(context);
const name = buttonType !== 'name' ? getName(context) : context.config.name;
if (name !== context.elements.previousName) {
Expand Down Expand Up @@ -188,20 +189,6 @@ export function changeStatus(context) {
}
}

if (isEntityType(context, "light")) {
if (cardType === 'button') {
context.card.classList.add('is-light');
} else if (cardType === 'pop-up') {
context.elements.headerContainer.classList.add('is-light');
}
} else {
if (cardType === 'button') {
context.card.classList.remove('is-light');
} else if (cardType === 'pop-up') {
context.elements.headerContainer.classList.remove('is-light');
}
}

if (isStateOn(context)) {
if (cardType === 'button') {
context.card.classList.add('is-on');
Expand Down Expand Up @@ -229,8 +216,8 @@ export function changeStyle(context) {

try {
customStyle = context.config.styles
? Function('hass', 'entity', 'state', 'icon', 'subButtonIcon', 'getWeatherIcon', 'card', `return \`${context.config.styles}\`;`)
(context._hass, context.config.entity, state, context.elements.icon, context.subButtonIcon, getWeatherIcon, context.card)
? Function('hass', 'entity', 'state', 'icon', 'subButtonIcon', 'getWeatherIcon', 'card', 'name', `return \`${context.config.styles}\`;`)
(context._hass, context.config.entity, state, context.elements.icon, context.subButtonIcon, getWeatherIcon, context.card, context.card.name)
: '';
} catch (error) {
throw new Error(`Error in generating button custom templates: ${error.message}`);
Expand Down
3 changes: 2 additions & 1 deletion src/cards/button/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export function handleButton(context, container = context.content, appendTo = co
}

changeIcon(context);
changeName(context);

changeState(context);
changeSubButtonState(context, container, context.elements.buttonCard);
if (context.cardType !== 'pop-up') changeStyle(context);
changeName(context);
}
1 change: 1 addition & 0 deletions src/cards/button/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default `
height: 50px;
background-color: var(--bubble-button-main-background-color, var(--bubble-main-background-color, var(--background-color-2, var(--secondary-background-color))));
border-radius: var(--bubble-button-border-radius, var(--bubble-border-radius, 32px));
box-shadow: var(--bubble-button-box-shadow, var(--bubble-box-shadow, none));
overflow: scroll;
touch-action: pan-y;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cards/climate/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export function changeIcon(context) {
}

export function changeName(context) {
if (context.config.styles?.includes("card.querySelector('.bubble-name').innerText")) return;
const name = getName(context);

if (name !== context.previousName && context.elements.name) {
context.elements.name.innerText = name;
context.previousName = name;
Expand Down
1 change: 1 addition & 0 deletions src/cards/climate/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default `
height: 50px;
background-color: var(--bubble-climate-main-background-color, var(--bubble-main-background-color, var(--background-color-2, var(--secondary-background-color))));
border-radius: var(--bubble-climate-border-radius, var(--bubble-border-radius, 32px));
box-shadow: var(--bubble-climate-box-shadow, var(--bubble-box-shadow, none));
overflow: visible;
touch-action: pan-y;
}
Expand Down
1 change: 1 addition & 0 deletions src/cards/cover/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function changeIcon(context) {
}

export function changeName(context) {
if (context.config.styles?.includes("card.querySelector('.bubble-name').innerText")) return;
const name = getName(context);
if (name !== context.elements.previousName) {
context.elements.name.innerText = name;
Expand Down
1 change: 1 addition & 0 deletions src/cards/cover/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default `
background: var(--bubble-cover-main-background-color, var(--bubble-main-background-color, var(--background-color-2, var(--secondary-background-color))));
height: 42px;
border-radius: var(--bubble-cover-border-radius, var(--bubble-border-radius, 32px));
box-shadow: var(--bubble-cover-box-shadow, var(--bubble-button-box-shadow, var(--bubble-box-shadow, none)));
align-items: center;
justify-content: center;
cursor: pointer;
Expand Down
6 changes: 3 additions & 3 deletions src/cards/media-player/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export function changeBackground(context) {
}

export function changeName(context) {
if (context.config.styles?.includes("card.querySelector('.bubble-name').innerText")) return;
const name = getName(context);

if (name !== context.previousName) {
context.elements.name.innerText = name;
context.previousName = name;
Expand Down Expand Up @@ -168,7 +168,7 @@ export function changeVolumeIcon(context) {
context.elements.volumeButton.setAttribute("icon", newIcon);
context.elements.mediaInfoContainer.style.opacity = newOpacity;
context.elements.nameContainer.style.opacity = newOpacity;
if(context.elements.subButtonContainer)context.elements.subButtonContainer.style.opacity = newOpacity;
if (context.elements.subButtonContainer) context.elements.subButtonContainer.style.opacity = newOpacity;
context.elements.previousButton.style.opacity = newOpacity;
context.elements.nextButton.style.opacity = newOpacity;
context.elements.powerButton.style.opacity = newOpacity;
Expand Down Expand Up @@ -242,4 +242,4 @@ export function changeStyle(context) {
if (context.elements.customStyle) {
context.elements.customStyle.innerText = customStyle;
}
}
}
1 change: 1 addition & 0 deletions src/cards/media-player/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default `
background-color: var(--bubble-media-player-main-background-color, var(--bubble-main-background-color, var(--background-color-2, var(--secondary-background-color))));
touch-action: pan-y;
border-radius: inherit;
box-shadow: var(--bubble-media-player-box-shadow, var(--bubble-box-shadow, none));
}
.bubble-media-player {
Expand Down
70 changes: 50 additions & 20 deletions src/cards/pop-up/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getIcon, getIconColor, getImage, getName, getState, isEntityType, isSta
import { getBackdrop } from "./create.ts";
import { addHash, onEditorChange, removeHash } from "./helpers.ts";
import { initializesubButtonIcon } from '../../tools/global-changes.ts';
import { checkConditionsMet, validateConditionalConfig, ensureArray } from '../../tools/validate-condition.ts';

export function changeEditor(context) {
if (!context.verticalStack) return;
Expand Down Expand Up @@ -110,34 +111,63 @@ export function changeStyle(context) {
backdropCustomStyle.innerText = customStyle;
}


export function changeTriggered(context) {
let triggerEntity = context.config.trigger_entity ?? '';
const triggerConditions = context.config.trigger;

if (triggerEntity === '') return;
if (triggerConditions) {
const isInitialLoad = !context.hasPageLoaded;
context.hasPageLoaded = true;

let triggerState = context.config.trigger_state ?? '';
let triggerClose = context.config.trigger_close ?? false;
let triggerEntityState = context._hass.states[triggerEntity]?.state;
console.log(context.hashAdded)

if (!triggerEntity) return;
if (!triggerState) return;
if (context.oldTriggerEntityState === triggerEntityState) return;
//Check conditions
const triggerConditions_array = ensureArray(triggerConditions);
if (validateConditionalConfig(triggerConditions_array)){
const trigger = checkConditionsMet(triggerConditions_array,context._hass);

const isInitialLoad = !context.hasPageLoaded;
context.hasPageLoaded = true;
if (trigger === context.previousTrigger) return;

if (context.config.hash === location.hash) {
if (triggerClose && triggerState !== triggerEntityState) {
if (!isInitialLoad) {
removeHash();
}
if (context.config.hash === location.hash) {
if (!trigger && !isInitialLoad) {
removeHash();
}
} else {
if (trigger) {
addHash(context.config.hash);
}
}

context.previousTrigger = trigger;
}
} else {
if (triggerEntityState === triggerState) {
addHash(context.config.hash);
// Deprecated method
let triggerEntity = context.config.trigger_entity ?? '';

if (triggerEntity === '') return;

let triggerState = context.config.trigger_state ?? '';
let triggerClose = context.config.trigger_close ?? false;
let triggerEntityState = context._hass.states[triggerEntity]?.state;

if (!triggerEntity) return;
if (!triggerState) return;
if (context.oldTriggerEntityState === triggerEntityState) return;

const isInitialLoad = !context.hasPageLoaded;
context.hasPageLoaded = true;

if (context.config.hash === location.hash) {
if (triggerClose && triggerState !== triggerEntityState) {
if (!isInitialLoad) {
removeHash();
}
}
} else {
if (triggerEntityState === triggerState) {
addHash(context.config.hash);
}
}
}

context.oldTriggerEntityState = triggerEntityState;
context.oldTriggerEntityState = triggerEntityState;
}
}
1 change: 1 addition & 0 deletions src/cards/select/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function changeIcon(context) {
}

export function changeName(context) {
if (context.config.styles?.includes("card.querySelector('.bubble-name').innerText")) return;
const name = getName(context);
if (name !== context.elements.previousName) {
applyScrollingEffect(context, context.elements.name, name);
Expand Down
13 changes: 7 additions & 6 deletions src/cards/select/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export function getTranslatedAttribute(context, state, attribute, option) {
export function getSelectedAttribute(state, attribute) {
switch (attribute) {
case 'fan_modes':
return state.attributes.fan_mode;
return state.attributes.fan_mode || null;
case 'swing_modes':
return state.attributes.swing_mode;
return state.attributes.swing_mode || null;
case 'preset_modes':
return state.attributes.preset_mode;
return state.attributes.preset_mode || null;
case 'effect_list':
return state.attributes.effect;
return state.attributes.effect || null;
case 'source_list':
return state.attributes.source;
return state.attributes.source || null;
case 'sound_mode_list':
return state.attributes.sound_mode;
return state.attributes.sound_mode || null;
default:
return state.state;
}
Expand Down Expand Up @@ -68,6 +68,7 @@ export function getOptionIcon(context, state, attribute, option) {
icon.icon = getHvacModeIcon(option);
break;
case 'fan_modes':
if (!state.attributes.fan_modes) return null;
icon = document.createElement('ha-attribute-icon');
icon.slot = 'graphic';
icon.attribute = 'fan_mode';
Expand Down
1 change: 1 addition & 0 deletions src/cards/select/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default `
height: 50px;
background-color: var(--bubble-select-main-background-color, var(--bubble-main-background-color, var(--background-color-2, var(--secondary-background-color))));
border-radius: var(--bubble-select-border-radius, var(--bubble-border-radius, 32px));
box-shadow: var(--bubble-select-box-shadow, var(--bubble-box-shadow, none));
touch-action: pan-y;
box-sizing: border-box;
border: solid 2px transparent;
Expand Down
1 change: 1 addition & 0 deletions src/cards/separator/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function changeIcon(context) {
}
}
export function changeName(context) {
if (context.config.styles?.includes("card.querySelector('.bubble-name').innerText")) return;
const name = getName(context);
if (name !== context.elements.name.innerText) {
context.elements.name.innerText = name;
Expand Down
Loading

0 comments on commit 7fde037

Please sign in to comment.