-
Notifications
You must be signed in to change notification settings - Fork 5
feat: support for displaying current weather attributes #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
abc5128
feat: support for displaying current weather attributes
troinine f3893a6
feat: support for displaying current weather attributes
troinine eae19f0
chore: updated test applications
troinine 5b47093
test: added tests for current weather attributes
troinine 7e8edbc
feat: added editor support for current attributes
troinine 0a15fad
feat: handle ozone unit
troinine 27ee1aa
docs: updated README.md to cover current weather configurations
troinine dd8b114
test: fix tests
troinine 5603062
fix: review fix
troinine f89bd26
fix: review fix
troinine 03b1824
fix: fixed current weather layout
troinine 88a725c
fix: review fix
troinine 1d82567
fix: review fix
troinine f12ea00
fix: review fix
troinine 1fa1c98
fix: review fixes
troinine 4c5524c
fix: review fixes
troinine 07a3d3b
fix: review fix
troinine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { html, LitElement, nothing, TemplateResult } from "lit"; | ||
| import { customElement, property } from "lit/decorators.js"; | ||
| import { | ||
| CurrentWeatherAttributes, | ||
| ExtendedHomeAssistant, | ||
| WeatherForecastCardConfig, | ||
| } from "../types"; | ||
| import { getWeatherUnit, getWind, WeatherEntity } from "../data/weather"; | ||
| import { capitalize } from "lodash-es"; | ||
| import memoizeOne from "memoize-one"; | ||
|
|
||
| const ATTRIBUTE_ICON_MAP: { [key in CurrentWeatherAttributes]: string } = { | ||
| humidity: "mdi:cloud-percent-outline", | ||
| pressure: "mdi:gauge", | ||
| wind_speed: "mdi:weather-windy-variant", | ||
| wind_gust_speed: "mdi:weather-windy", | ||
| visibility: "mdi:eye", | ||
| ozone: "mdi:molecule", | ||
| uv_index: "mdi:weather-sunny-alert", | ||
| dew_point: "mdi:water-thermometer-outline", | ||
| apparent_temperature: "mdi:thermometer", | ||
| cloud_coverage: "mdi:cloud-outline", | ||
| }; | ||
|
|
||
| @customElement("wfc-current-weather-attributes") | ||
| export class WfcCurrentWeatherAttributes extends LitElement { | ||
| @property({ attribute: false }) hass!: ExtendedHomeAssistant; | ||
| @property({ attribute: false }) weatherEntity!: WeatherEntity; | ||
| @property({ attribute: false }) | ||
| weatherAttributes: CurrentWeatherAttributes[] = []; | ||
| @property({ attribute: false }) config!: WeatherForecastCardConfig; | ||
|
|
||
| protected createRenderRoot() { | ||
| return this; | ||
| } | ||
|
|
||
| protected render(): TemplateResult | typeof nothing { | ||
| if ( | ||
| !this.hass || | ||
| !this.weatherEntity || | ||
| this.weatherAttributes.length === 0 | ||
| ) { | ||
| return nothing; | ||
| } | ||
|
|
||
| const attributeTemplates = this.weatherAttributes | ||
| .map((attr) => this._renderAttribute(attr)) | ||
| .filter((template) => template !== nothing); | ||
|
|
||
| if (attributeTemplates.length === 0) { | ||
| return nothing; | ||
| } | ||
|
|
||
| return html` | ||
| <div class="wfc-current-attributes">${attributeTemplates}</div> | ||
| `; | ||
| } | ||
|
|
||
| private _renderAttribute( | ||
| attribute: CurrentWeatherAttributes | ||
| ): TemplateResult | typeof nothing { | ||
| const value = this.computeAttributeValue(attribute); | ||
|
|
||
| if (!value) { | ||
| return nothing; | ||
| } | ||
|
|
||
| return html` | ||
| <div class="wfc-current-attribute"> | ||
| <ha-attribute-icon | ||
| class="wfc-current-attribute-icon" | ||
| .hass=${this.hass} | ||
| .stateObj=${this.weatherEntity} | ||
| .attribute=${attribute} | ||
| .icon=${ATTRIBUTE_ICON_MAP[attribute]} | ||
| ></ha-attribute-icon> | ||
| <span class="wfc-current-attribute-name"> | ||
| ${this.localize(attribute)} | ||
| </span> | ||
| <span class="wfc-current-attribute-value">${value}</span> | ||
| </div> | ||
| `; | ||
| } | ||
|
|
||
| private computeAttributeValue = ( | ||
| attribute: CurrentWeatherAttributes | ||
| ): string | undefined => { | ||
| const stateObj = this.weatherEntity; | ||
|
|
||
| if (stateObj.attributes[attribute] === undefined) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (attribute === "wind_speed") { | ||
| return getWind(this.hass, stateObj); | ||
| } | ||
|
|
||
| // hass.formatEntityAttributeValue does not support wind_gust_speed yet | ||
| if ( | ||
| attribute === "wind_gust_speed" && | ||
| stateObj.attributes.wind_gust_speed !== undefined | ||
| ) { | ||
| const unit = getWeatherUnit(this.hass, stateObj, "wind_gust_speed"); | ||
|
|
||
| return `${stateObj.attributes.wind_gust_speed} ${unit}`; | ||
| } | ||
|
|
||
| // hass.formatEntityAttributeValue does not support ozone yet | ||
| if (attribute === "ozone" && stateObj.attributes.ozone !== undefined) { | ||
| const unit = getWeatherUnit(this.hass, stateObj, "ozone"); | ||
|
|
||
| return `${stateObj.attributes.ozone} ${unit}`; | ||
| } | ||
|
|
||
| return this.hass.formatEntityAttributeValue(this.weatherEntity, attribute); | ||
| }; | ||
|
|
||
| private localize = (attribute: string): string => { | ||
| return ( | ||
| this.hass.formatEntityAttributeName(this.weatherEntity, attribute) || | ||
| this.hass.localize(getLocalizationKey(attribute)) || | ||
| capitalize(attribute).replace(/_/g, " ") | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| const getLocalizationKey = memoizeOne((attribute: string): string => { | ||
| switch (attribute) { | ||
| case "pressure": | ||
| return "ui.card.weather.attributes.air_pressure"; | ||
| default: | ||
| return `ui.card.weather.attributes.${attribute}`; | ||
| } | ||
| }); | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| "wfc-current-weather-attributes": WfcCurrentWeatherAttributes; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.