-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add option to compare energy graphs with previous period #12723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e71adf6
e96cf90
f31e4f9
d654d24
affdc1e
de21f67
9a9da99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,106 @@ | ||||||
| import { differenceInDays, endOfDay } from "date-fns"; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should already use that as it has: in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh. Well I just fixed it everywhere else 😅 |
||||||
| import { UnsubscribeFunc } from "home-assistant-js-websocket"; | ||||||
| import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit"; | ||||||
| import { customElement, property, state } from "lit/decorators"; | ||||||
| import { formatDate } from "../../../../common/datetime/format_date"; | ||||||
| import { EnergyData, getEnergyDataCollection } from "../../../../data/energy"; | ||||||
| import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; | ||||||
| import { HomeAssistant } from "../../../../types"; | ||||||
| import { LovelaceCard } from "../../types"; | ||||||
| import { EnergyCardBaseConfig } from "../types"; | ||||||
|
|
||||||
| @customElement("hui-energy-compare-card") | ||||||
| export class HuiEnergyCompareCard | ||||||
| extends SubscribeMixin(LitElement) | ||||||
| implements LovelaceCard | ||||||
| { | ||||||
| @property({ attribute: false }) public hass!: HomeAssistant; | ||||||
|
|
||||||
| @state() private _config?: EnergyCardBaseConfig; | ||||||
|
|
||||||
| @state() private _start?: Date; | ||||||
|
|
||||||
| @state() private _end?: Date; | ||||||
|
|
||||||
| @state() private _startCompare?: Date; | ||||||
|
|
||||||
| @state() private _endCompare?: Date; | ||||||
|
|
||||||
| public getCardSize(): Promise<number> | number { | ||||||
| return 1; | ||||||
| } | ||||||
|
|
||||||
| public setConfig(config: EnergyCardBaseConfig): void { | ||||||
| this._config = config; | ||||||
| } | ||||||
|
|
||||||
| protected hassSubscribeRequiredHostProps = ["_config"]; | ||||||
|
|
||||||
| public hassSubscribe(): UnsubscribeFunc[] { | ||||||
| return [ | ||||||
| getEnergyDataCollection(this.hass, { | ||||||
| key: this._config!.collection_key, | ||||||
| }).subscribe((data) => this._update(data)), | ||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| protected render(): TemplateResult { | ||||||
| if (!this._startCompare || !this._endCompare) { | ||||||
| return html``; | ||||||
| } | ||||||
|
|
||||||
| const dayDifference = differenceInDays( | ||||||
| this._endCompare, | ||||||
| this._startCompare | ||||||
| ); | ||||||
|
|
||||||
| return html` | ||||||
| <ha-alert dismissable @alert-dismissed-clicked=${this._stopCompare}> | ||||||
| You are comparing the period | ||||||
| <b | ||||||
| >${formatDate(this._start!, this.hass.locale)}${dayDifference > 0 | ||||||
| ? ` - | ||||||
| ${formatDate(this._end || endOfDay(new Date()), this.hass.locale)}` | ||||||
| : ""}</b | ||||||
| > | ||||||
| with period | ||||||
| <b | ||||||
| >${formatDate(this._startCompare, this.hass.locale)}${dayDifference > | ||||||
| 0 | ||||||
| ? ` - | ||||||
| ${formatDate(this._endCompare, this.hass.locale)}` | ||||||
| : ""}</b | ||||||
| > | ||||||
| </ha-alert> | ||||||
| `; | ||||||
| } | ||||||
|
|
||||||
| private _update(data: EnergyData): void { | ||||||
| this._start = data.start; | ||||||
| this._end = data.end; | ||||||
| this._startCompare = data.startCompare; | ||||||
| this._endCompare = data.endCompare; | ||||||
| } | ||||||
|
|
||||||
| private _stopCompare(): void { | ||||||
| const energyCollection = getEnergyDataCollection(this.hass, { | ||||||
| key: this._config!.collection_key, | ||||||
| }); | ||||||
| energyCollection.setCompare(false); | ||||||
| energyCollection.refresh(); | ||||||
| } | ||||||
|
|
||||||
| static get styles(): CSSResultGroup { | ||||||
| return css` | ||||||
| mwc-button { | ||||||
| width: max-content; | ||||||
| } | ||||||
| `; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| declare global { | ||||||
| interface HTMLElementTagNameMap { | ||||||
| "hui-energy-compare-card": HuiEnergyCompareCard; | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬