-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Cover position tile feature #16110
Merged
piitaya
merged 5 commits into
home-assistant:dev
from
Misiu:cover-position-tile-feature
Aug 18, 2023
Merged
Cover position tile feature #16110
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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
114 changes: 114 additions & 0 deletions
114
src/panels/lovelace/tile-features/hui-cover-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,114 @@ | ||
import { HassEntity } from "home-assistant-js-websocket"; | ||
import { css, html, LitElement, nothing } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { computeDomain } from "../../../common/entity/compute_domain"; | ||
import { supportsFeature } from "../../../common/entity/supports-feature"; | ||
import { CoverEntityFeature } from "../../../data/cover"; | ||
import { HomeAssistant } from "../../../types"; | ||
import { LovelaceTileFeature } from "../types"; | ||
import { CoverPositionTileFeatureConfig } from "./types"; | ||
import { stateActive } from "../../../common/entity/state_active"; | ||
import { computeAttributeNameDisplay } from "../../../common/entity/compute_attribute_display"; | ||
import { UNAVAILABLE } from "../../../data/entity"; | ||
|
||
export const supportsCoverPositionTileFeature = (stateObj: HassEntity) => { | ||
const domain = computeDomain(stateObj.entity_id); | ||
return ( | ||
domain === "cover" && | ||
supportsFeature(stateObj, CoverEntityFeature.SET_POSITION) | ||
); | ||
}; | ||
|
||
@customElement("hui-cover-position-tile-feature") | ||
class HuiCoverPositionTileFeature | ||
extends LitElement | ||
implements LovelaceTileFeature | ||
{ | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@property({ attribute: false }) public stateObj?: HassEntity; | ||
|
||
@state() private _config?: CoverPositionTileFeatureConfig; | ||
|
||
static getStubConfig(): CoverPositionTileFeatureConfig { | ||
return { | ||
type: "cover-position", | ||
}; | ||
} | ||
|
||
public setConfig(config: CoverPositionTileFeatureConfig): void { | ||
if (!config) { | ||
throw new Error("Invalid configuration"); | ||
} | ||
this._config = config; | ||
} | ||
|
||
protected render() { | ||
if ( | ||
!this._config || | ||
!this.hass || | ||
!this.stateObj || | ||
!supportsCoverPositionTileFeature(this.stateObj) | ||
) { | ||
return nothing; | ||
} | ||
|
||
const percentage = stateActive(this.stateObj) | ||
? this.stateObj.attributes.current_position ?? 0 | ||
: 0; | ||
|
||
const value = Math.max(Math.round(percentage), 0); | ||
|
||
return html` <div class="container"> | ||
<ha-control-slider | ||
.value=${value} | ||
min="0" | ||
max="100" | ||
step="1" | ||
inverted | ||
show-handle | ||
@value-changed=${this._valueChanged} | ||
.ariaLabel=${computeAttributeNameDisplay( | ||
this.hass.localize, | ||
this.stateObj, | ||
this.hass.entities, | ||
"current_position" | ||
)} | ||
.disabled=${this.stateObj!.state === UNAVAILABLE} | ||
></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_position", { | ||
entity_id: this.stateObj!.entity_id, | ||
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; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-cover-position-tile-feature": HuiCoverPositionTileFeature; | ||
} | ||
} |
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
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.
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.
The naming convention for cover-features needs a rework if we introduce tilt-sliders.
Maybe the position feature should be called "cover-position-slider"?