Skip to content

Commit

Permalink
Merge pull request #942 from matrixx567/popup_weather_forecast_fix
Browse files Browse the repository at this point in the history
Fix number of forecast entities.
  • Loading branch information
basbruss authored Aug 31, 2022
2 parents f34ac86 + e1cc4dd commit e85d8b0
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ulm_actions_card:
return popup_config.popup_variables;
}
}
return {};
]]]
hold_action:
action: >
Expand Down Expand Up @@ -134,7 +135,7 @@ ulm_actions_card:
action = 'more-info'
}
return action
return action;
]]]
navigation_path: "[[[ return variables.ulm_card_hold_action == 'navigate' ? variables.ulm_card_hold_navigate_path : '/0' ]]]"
haptic: "[[[ return variables.ulm_card_hold_haptic != null ? variables.ulm_card_hold_haptic : 'none' ]]]"
Expand Down Expand Up @@ -208,6 +209,7 @@ ulm_actions_card:
return popup_config.popup_variables;
}
}
return {};
]]]
double_tap_action:
action: >
Expand Down Expand Up @@ -238,7 +240,7 @@ ulm_actions_card:
action = 'more-info'
}
return action
return action;
]]]
navigation_path: "[[[ return variables.ulm_card_double_tap_action == 'navigate' ? variables.ulm_card_double_tap_navigate_path : '/0' ]]]"
haptic: "[[[ return variables.ulm_card_double_tap_haptic != null ? variables.ulm_card_double_tap_haptic : 'none' ]]]"
Expand Down Expand Up @@ -312,4 +314,5 @@ ulm_actions_card:
return popup_config.popup_variables;
}
}
return {};
]]]
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ ulm_actions_icon:
return popup_config.popup_variables;
}
}
return {};
]]]
hold_action:
action: >
Expand Down Expand Up @@ -242,6 +243,7 @@ ulm_actions_icon:
return popup_config.popup_variables;
}
}
return {};
]]]
double_tap_action:
action: >
Expand Down Expand Up @@ -357,4 +359,5 @@ ulm_actions_icon:
return popup_config.popup_variables;
}
}
return {};
]]]
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ ulm_actions_name:
return popup_config.popup_variables;
}
}
return {};
]]]
hold_action:
action: >
Expand Down Expand Up @@ -222,6 +223,7 @@ ulm_actions_name:
return popup_config.popup_variables;
}
}
return {};
]]]
double_tap_action:
action: >
Expand Down Expand Up @@ -333,4 +335,5 @@ ulm_actions_name:
return popup_config.popup_variables;
}
}
return {};
]]]
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
popup_weather_forecast:
variables:
ulm_weather_popup_surpress_first_forecast: false
show_icon: false
show_name: false
show_label: false
Expand Down Expand Up @@ -97,37 +99,72 @@ popup_weather_forecast:
temp: "[[[ return entity.attributes.temperature + '°' ]]]"
tempminmax: "[[[ return 'Max. ' + entity.attributes.forecast[0].temperature + '° Min. ' + entity.attributes.forecast[0].templow + '°' ]]]"
- type: "vertical-stack"
cards:
- type: "custom:button-card"
template: "popup_weather_row"
entity: "[[[ return entity.entity_id ]]]"
variables:
ulm_popup_weather_entity_idx: 0
- type: "custom:button-card"
template: "popup_weather_row"
entity: "[[[ return entity.entity_id ]]]"
variables:
ulm_popup_weather_entity_idx: 1
- type: "custom:button-card"
template: "popup_weather_row"
entity: "[[[ return entity.entity_id ]]]"
variables:
ulm_popup_weather_entity_idx: 2
- type: "custom:button-card"
template: "popup_weather_row"
entity: "[[[ return entity.entity_id ]]]"
variables:
ulm_popup_weather_entity_idx: 3
- type: "custom:button-card"
template: "popup_weather_row"
entity: "[[[ return entity.entity_id ]]]"
variables:
ulm_popup_weather_entity_idx: 4
- type: "custom:button-card"
template: "popup_weather_row"
entity: "[[[ return entity.entity_id ]]]"
variables:
ulm_popup_weather_entity_idx: 5
cards: >
[[[
let rows = [];
function generate_weather_row(index) {
var row = {
'type': 'custom:button-card',
'template': 'popup_weather_row',
'entity': entity.entity_id,
'variables': {
'ulm_popup_weather_entity_idx': index
}
};
return row;
}
let surpress_first_forecast = ('ulm_weather_popup_surpress_first_forecast' in variables) ? variables.ulm_weather_popup_surpress_first_forecast : false;
let index = 0;
for (let forecast in entity.attributes.forecast) {
if ((index == 0) && surpress_first_forecast) {
index++;
continue;
}
rows.push(generate_weather_row(index));
index++;
if (rows.length >= 6) {
break;
}
}
return rows;
]]]
# Code above create following cards
# cards:
# - type: "custom:button-card"
# template: "popup_weather_row"
# entity: "[[[ return entity.entity_id ]]]"
# variables:
# ulm_popup_weather_entity_idx: 0
# - type: "custom:button-card"
# template: "popup_weather_row"
# entity: "[[[ return entity.entity_id ]]]"
# variables:
# ulm_popup_weather_entity_idx: 1
# - type: "custom:button-card"
# template: "popup_weather_row"
# entity: "[[[ return entity.entity_id ]]]"
# variables:
# ulm_popup_weather_entity_idx: 2
# - type: "custom:button-card"
# template: "popup_weather_row"
# entity: "[[[ return entity.entity_id ]]]"
# variables:
# ulm_popup_weather_entity_idx: 3
# - type: "custom:button-card"
# template: "popup_weather_row"
# entity: "[[[ return entity.entity_id ]]]"
# variables:
# ulm_popup_weather_entity_idx: 4
# - type: "custom:button-card"
# template: "popup_weather_row"
# entity: "[[[ return entity.entity_id ]]]"
# variables:
# ulm_popup_weather_entity_idx: 5
buttons:
card:
type: "custom:button-card"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
popup_weather_radar:
template: "popup_weather_forecast"
variables:
ulm_weather_popup_surpress_first_forecast: false
styles:
grid:
- grid-template-areas: "'header' 'content2' 'buttons' 'footer'"
Expand Down
32 changes: 32 additions & 0 deletions docs/setup/custom_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,35 @@ show_name: false
show_label: false
show_units: false
```

The usage of variables within the popup have some restrictions. Default variables of the popup don't work in the same
way as for other custom button cards. The default variables -- defined in the variables section -- will always be overwritten by
the custom actions caller.

The following code won't work on custom popup cards:

```yaml
popup_weather_forecast:
...
variables:
ulm_weather_popup_surpress_first_forecast: false
...
```

Instead of using the variables section of the card the variables must be checked within a JavaScript template. The following
code shows an example how to check and define a default value of custom popup variable:

```yaml
element: >
[[[
let surpress_first_forecast = ('ulm_weather_popup_surpress_first_forecast' in variables) ? variables.ulm_weather_popup_surpress_first_forecast : false;
if (surpress_first_forecast) {
...
}
return surpress_first_forecast;
]]]
```

The code checks if the variable is available before reading from the variabl and if the variable isn't available it will set a default value.
10 changes: 9 additions & 1 deletion docs/usage/popups/popup_weather.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ To enable this popup, add the following code to your card:
```yaml
ulm_custom_popup:
template: "popup_weather_forecast"
entity: weather.xxx
popup_variables:
ulm_popup_weather_entity: weather.xxx
ulm_weather_popup_surpress_first_forecast: false
```
## Variables
| Variable | Default | Required | Notes |
|-------------------------------------------|---------|------------------|-------------------------------|
| entity | | :material-check: | |
| ulm_weather_popup_surpress_first_forecast | false | :material-close: | Suppress first forecast entry |
## Credits
- Designed by schumijo

0 comments on commit e85d8b0

Please sign in to comment.