Skip to content

Commit

Permalink
Merge pull request denysdovhan#122 from krystiancharubin/feature/live…
Browse files Browse the repository at this point in the history
…-map

fix high cpu usage on live map
  • Loading branch information
krystiancharubin committed Oct 7, 2020
2 parents 5808cfb + c415be7 commit 18353a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Here is what every option means:
| `type` | `string` | **Required** | `custom:vacuum-card` |
| `entity` | `string` | **Required** | An entity_id within the `vacuum` domain. |
| `map` | `string` | Optional | An entity_id within the `camera` domain, for streaming live vacuum map. |
| `map_refresh` | `integer` | `5` | Update interval for map camera in seconds |
| `image` | `string` | `default` | Path to image of your vacuum cleaner. Better to have `png` or `svg`. |
| `show_name` | `boolean` | `true` | Show friendly name of the vacuum. |
| `show_status` | `boolean` | `true` | Show status of the vacuum. |
Expand Down
4 changes: 2 additions & 2 deletions src/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export default css`
cursor: pointer;
overflow: hidden;
position: relative;
text-align: center;
}
.preview.not-available {
filter: grayscale(1);
}
.map {
display: block;
max-width: 100%;
max-width: 95%;
image-rendering: crisp-edges;
}
Expand Down
42 changes: 32 additions & 10 deletions src/vacuum-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,6 @@ class VacuumCard extends LitElement {
}

updated(changedProps) {
if (this.map) {
const url =
this.map.attributes.entity_picture + `&t=${new Date().getTime()}`;
const img = new Image();
img.onload = () => {
this.mapUrl = url;
};
img.src = url;
}

if (
changedProps.get('hass') &&
changedProps.get('hass').states[this.config.entity].state !==
Expand All @@ -124,6 +114,38 @@ class VacuumCard extends LitElement {
}
}

updateCameraImage() {
this.hass
.callWS({
type: 'camera_thumbnail',
entity_id: this.config.map,
})
.then((val) => {
const { content_type: contentType, content } = val;
this.mapUrl = `data:${contentType};base64, ${content}`;
this.requestUpdate();
});
}

connectedCallback() {
super.connectedCallback();
if (this.map) {
this.updateCameraImage();
this.thumbUpdater = setInterval(
() => this.updateCameraImage(),
(this.config.map_refresh || 5) * 1000
);
}
}

disconnectedCallback() {
super.disconnectedCallback();
if (this.map) {
clearInterval(this.thumbUpdater);
this.map_image = null;
}
}

handleMore() {
fireEvent(
this,
Expand Down

0 comments on commit 18353a3

Please sign in to comment.