-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add target humidity feature (#18913)
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/panels/lovelace/card-features/hui-target-humidity-card-feature.ts
This file contains 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,127 @@ | ||
import { HassEntity } from "home-assistant-js-websocket"; | ||
import { css, html, LitElement, nothing, PropertyValues } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { computeDomain } from "../../../common/entity/compute_domain"; | ||
import "../../../components/ha-control-slider"; | ||
import { UNAVAILABLE } from "../../../data/entity"; | ||
import { HumidifierEntity } from "../../../data/humidifier"; | ||
import { HomeAssistant } from "../../../types"; | ||
import { LovelaceCardFeature } from "../types"; | ||
import { TargetHumidityCardFeatureConfig } from "./types"; | ||
|
||
export const supportsTargetHumidityCardFeature = (stateObj: HassEntity) => { | ||
const domain = computeDomain(stateObj.entity_id); | ||
return domain === "humidifier"; | ||
}; | ||
|
||
@customElement("hui-target-humidity-card-feature") | ||
class HuiTargetHumidityCardFeature | ||
extends LitElement | ||
implements LovelaceCardFeature | ||
{ | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@property({ attribute: false }) public stateObj?: HumidifierEntity; | ||
|
||
@state() private _config?: TargetHumidityCardFeatureConfig; | ||
|
||
@state() private _targetHumidity?: number; | ||
|
||
static getStubConfig(): TargetHumidityCardFeatureConfig { | ||
return { | ||
type: "target-humidity", | ||
}; | ||
} | ||
|
||
public setConfig(config: TargetHumidityCardFeatureConfig): void { | ||
if (!config) { | ||
throw new Error("Invalid configuration"); | ||
} | ||
this._config = config; | ||
} | ||
|
||
protected willUpdate(changedProp: PropertyValues): void { | ||
super.willUpdate(changedProp); | ||
if (changedProp.has("stateObj")) { | ||
this._targetHumidity = this.stateObj!.attributes.humidity; | ||
} | ||
} | ||
|
||
private get _step() { | ||
return 1; | ||
} | ||
|
||
private get _min() { | ||
return this.stateObj!.attributes.min_humidity ?? 0; | ||
} | ||
|
||
private get _max() { | ||
return this.stateObj!.attributes.max_humidity ?? 100; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent) { | ||
const value = (ev.detail as any).value; | ||
if (isNaN(value)) return; | ||
this._targetHumidity = value; | ||
this._callService(); | ||
} | ||
|
||
private _callService() { | ||
this.hass!.callService("humidifier", "set_humidity", { | ||
entity_id: this.stateObj!.entity_id, | ||
humidity: this._targetHumidity, | ||
}); | ||
} | ||
|
||
protected render() { | ||
if ( | ||
!this._config || | ||
!this.hass || | ||
!this.stateObj || | ||
!supportsTargetHumidityCardFeature(this.stateObj) | ||
) { | ||
return nothing; | ||
} | ||
|
||
return html` | ||
<div class="container"> | ||
<ha-control-slider | ||
.value=${this.stateObj.attributes.humidity} | ||
.min=${this._min} | ||
.max=${this._max} | ||
.step=${this._step} | ||
.disabled=${this.stateObj!.state === UNAVAILABLE} | ||
@value-changed=${this._valueChanged} | ||
.label=${this.hass.formatEntityAttributeName( | ||
this.stateObj, | ||
"humidity" | ||
)} | ||
unit="%" | ||
.locale=${this.hass.locale} | ||
></ha-control-slider> | ||
</div> | ||
`; | ||
} | ||
|
||
static get styles() { | ||
return css` | ||
ha-control-slider { | ||
--control-slider-color: var(--feature-color); | ||
--control-slider-background: var(--feature-color); | ||
--control-slider-background-opacity: 0.2; | ||
--control-slider-thickness: 40px; | ||
--control-slider-border-radius: 10px; | ||
} | ||
.container { | ||
padding: 0 12px 12px 12px; | ||
width: auto; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-target-humidity-card-feature": HuiTargetHumidityCardFeature; | ||
} | ||
} |
This file contains 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 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 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 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