forked from home-assistant/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cover tilt position to tile card (home-assistant#17619)
Add cover tilt to tile card
- Loading branch information
Showing
6 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
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
121 changes: 121 additions & 0 deletions
121
src/panels/lovelace/tile-features/hui-cover-tilt-position-tile-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,121 @@ | ||
import { HassEntity } from "home-assistant-js-websocket"; | ||
import { css, html, LitElement, nothing } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { computeAttributeNameDisplay } from "../../../common/entity/compute_attribute_display"; | ||
import { computeDomain } from "../../../common/entity/compute_domain"; | ||
import { supportsFeature } from "../../../common/entity/supports-feature"; | ||
import { CoverEntity, CoverEntityFeature } from "../../../data/cover"; | ||
import { UNAVAILABLE } from "../../../data/entity"; | ||
import { generateTiltSliderTrackBackgroundGradient } from "../../../dialogs/more-info/components/cover/ha-more-info-cover-tilt-position"; | ||
import { HomeAssistant } from "../../../types"; | ||
import { LovelaceTileFeature } from "../types"; | ||
import { CoverTiltPositionTileFeatureConfig } from "./types"; | ||
|
||
const GRADIENT = generateTiltSliderTrackBackgroundGradient(); | ||
|
||
export const supportsCoverTiltPositionTileFeature = (stateObj: HassEntity) => { | ||
const domain = computeDomain(stateObj.entity_id); | ||
return ( | ||
domain === "cover" && | ||
supportsFeature(stateObj, CoverEntityFeature.SET_TILT_POSITION) | ||
); | ||
}; | ||
|
||
@customElement("hui-cover-tilt-position-tile-feature") | ||
class HuiCoverTiltPositionTileFeature | ||
extends LitElement | ||
implements LovelaceTileFeature | ||
{ | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@property({ attribute: false }) public stateObj?: CoverEntity; | ||
|
||
@state() private _config?: CoverTiltPositionTileFeatureConfig; | ||
|
||
static getStubConfig(): CoverTiltPositionTileFeatureConfig { | ||
return { | ||
type: "cover-tilt-position", | ||
}; | ||
} | ||
|
||
public setConfig(config: CoverTiltPositionTileFeatureConfig): void { | ||
if (!config) { | ||
throw new Error("Invalid configuration"); | ||
} | ||
this._config = config; | ||
} | ||
|
||
protected render() { | ||
if ( | ||
!this._config || | ||
!this.hass || | ||
!this.stateObj || | ||
!supportsCoverTiltPositionTileFeature(this.stateObj) | ||
) { | ||
return nothing; | ||
} | ||
|
||
const percentage = this.stateObj.attributes.current_tilt_position ?? 0; | ||
|
||
const value = Math.max(Math.round(percentage), 0); | ||
|
||
return html` | ||
<div class="container"> | ||
<ha-control-slider | ||
.value=${value} | ||
min="0" | ||
max="100" | ||
mode="cursor" | ||
inverted | ||
@value-changed=${this._valueChanged} | ||
.ariaLabel=${computeAttributeNameDisplay( | ||
this.hass.localize, | ||
this.stateObj, | ||
this.hass.entities, | ||
"current_tilt_position" | ||
)} | ||
.disabled=${this.stateObj!.state === UNAVAILABLE} | ||
> | ||
<div slot="background" class="gradient"></div | ||
></ha-control-slider> | ||
</div> | ||
`; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent) { | ||
const value = (ev.detail as any).value; | ||
if (isNaN(value)) return; | ||
|
||
this.hass!.callService("cover", "set_cover_tilt_position", { | ||
entity_id: this.stateObj!.entity_id, | ||
tilt_position: value, | ||
}); | ||
} | ||
|
||
static get styles() { | ||
return css` | ||
ha-control-slider { | ||
/* Force inactive state to be colored for the slider */ | ||
--control-slider-color: var(--tile-color); | ||
--control-slider-background: var(--tile-color); | ||
--control-slider-background-opacity: 0.2; | ||
--control-slider-thickness: 40px; | ||
--control-slider-border-radius: 10px; | ||
} | ||
.container { | ||
padding: 0 12px 12px 12px; | ||
width: auto; | ||
} | ||
.gradient { | ||
background: -webkit-linear-gradient(left, ${GRADIENT}); | ||
opacity: 0.6; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-cover-tilt-position-tile-feature": HuiCoverTiltPositionTileFeature; | ||
} | ||
} |
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