Skip to content

Commit

Permalink
Adds forced refresh after time runs out.
Browse files Browse the repository at this point in the history
  • Loading branch information
luixal committed Apr 8, 2024
1 parent d0f258f commit 300301f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ This card is quite simple so there are only a few options:
| image | string | | **REQUIRED** The path to the image in media source format. i.e: media-source://media_source/local/my_image.jpg |
| entity_id | string | | The entity you want to toggle when card is clicked |
| apply_grayscale | boolean | | If `true` applies a grayscale on the image when entity is `off` |
| forced_refresh_interval | integer | | Number of seconds to force an image refresh |
| tap_action | string | `toggle` | Action to perform on click. One of `none`, `toggle` or `call-service`. See actions below |

## Actions
Expand Down Expand Up @@ -170,6 +171,23 @@ type: custom:media-source-image-card
image: media-source://media_source/local/banana.png
entity_id: input_boolean.boolean_test
apply_grayscale: true
tap_action:
action: call-service
service: input_boolean.turn_on
target:
entity_id: input_boolean.boolean_test
```

### Random image using just a template
This will refresh the image every 10 seconds, resolving the template again and showing a random image in a `X.png` format, where `X` is a number.

```yaml
type: custom:media-source-image-card
image: |
media-source://media_source/local/{{ '%02d' % range(1, 20) | random }}.png
entity_id: input_boolean.boolean_test
apply_grayscale: true
forced_refresh_interval: 10
tap_action:
action: call-service
service: input_boolean.turn_on
Expand Down
6 changes: 5 additions & 1 deletion media-source-image-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class MediaSourceImageCard extends HTMLElement {
}
// when a related entity changes, refresh content:
if (this.watchEntities(this.config.image, hass)) this.renderContent();
// if forced_refresh_interval is set, register timeout to re-render content:
if (this.config.forced_refresh_interval && !this.forced_refresh_interval) {
this.forced_refresh_interval = setInterval(() => {console.log('timeout!!!!!!'); this.renderContent() }, this.config.forced_refresh_interval * 1000);
}
// apply grayscale according to entity state:
if (this.config.entity_id) {
const newState = hass.states[this.config.entity_id].state;
Expand Down Expand Up @@ -204,7 +208,7 @@ window.customCards.push({
});

console.info(
`%c MEDIA SOURCE IMAGE CARD %c Version 0.2.5 `,
`%c MEDIA SOURCE IMAGE CARD %c Version 0.3.0 `,
'color: orange; font-weight: bold; background: black',
'color: white; font-weight: bold; background: dimgray',
);

0 comments on commit 300301f

Please sign in to comment.