mirror of
https://github.com/home-assistant/frontend.git
synced 2026-11-23 21:28:49 +00:00
Compare commits
1
Commits
dev
...
cover-tilt-angles
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2bec6001a |
No files matched your search
+28
-1
@@ -124,8 +124,32 @@ export function isFullyClosed(stateObj: CoverEntity) {
|
||||
return stateObj.state === "closed";
|
||||
}
|
||||
|
||||
export function coverOpenTiltPosition(stateObj: CoverEntity): number {
|
||||
return stateObj.attributes.open_tilt_position ?? 100;
|
||||
}
|
||||
|
||||
export function coverTiltAngle(stateObj: CoverEntity, tilt: number): number {
|
||||
const start = stateObj.attributes.tilt_start_angle;
|
||||
const end = stateObj.attributes.tilt_end_angle;
|
||||
if (start != null && end != null) {
|
||||
return start + ((end - start) * tilt) / 100;
|
||||
}
|
||||
const open = coverOpenTiltPosition(stateObj);
|
||||
if (tilt <= open) {
|
||||
return open === 0 ? 0 : (-90 * (open - tilt)) / open;
|
||||
}
|
||||
return (90 * (tilt - open)) / (100 - open);
|
||||
}
|
||||
|
||||
export function coverTiltOpenness(stateObj: CoverEntity, tilt: number) {
|
||||
return 100 * (1 - Math.min(1, Math.abs(coverTiltAngle(stateObj, tilt)) / 90));
|
||||
}
|
||||
|
||||
export function isFullyOpenTilt(stateObj: CoverEntity) {
|
||||
return stateObj.attributes.current_tilt_position === 100;
|
||||
return (
|
||||
stateObj.attributes.current_tilt_position ===
|
||||
coverOpenTiltPosition(stateObj)
|
||||
);
|
||||
}
|
||||
|
||||
export function isFullyClosedTilt(stateObj: CoverEntity) {
|
||||
@@ -199,6 +223,9 @@ export function canStopTilt(stateObj: CoverEntity): boolean {
|
||||
interface CoverEntityAttributes extends HassEntityAttributeBase {
|
||||
current_position?: number;
|
||||
current_tilt_position?: number;
|
||||
open_tilt_position?: number;
|
||||
tilt_start_angle?: number;
|
||||
tilt_end_angle?: number;
|
||||
supported_speeds?: string[];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { HassEntity } from "home-assistant-js-websocket";
|
||||
import { css, html, LitElement, nothing, unsafeCSS } from "lit";
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import { styleMap } from "lit/directives/style-map";
|
||||
import { consume } from "../../../common/decorators/consume";
|
||||
@@ -16,7 +16,10 @@ import { stateColorCss } from "../../../common/entity/state_color";
|
||||
import type { LocalizeFunc } from "../../../common/translations/localize";
|
||||
import "../../../components/ha-control-slider";
|
||||
import type { CoverEntity } from "../../../data/cover";
|
||||
import { coverSupportsTiltPosition } from "../../../data/cover";
|
||||
import {
|
||||
coverSupportsTiltPosition,
|
||||
coverTiltOpenness,
|
||||
} from "../../../data/cover";
|
||||
import {
|
||||
apiContext,
|
||||
entitiesContext,
|
||||
@@ -37,34 +40,29 @@ import type {
|
||||
LovelaceCardFeatureContext,
|
||||
} from "./types";
|
||||
|
||||
function generateTiltSliderTrackBackgroundGradient() {
|
||||
const count = 24;
|
||||
const minStrokeWidth = 0.2;
|
||||
const gradient: [number, string][] = [];
|
||||
const SLAT_COUNT = 24;
|
||||
const MIN_SLAT_WIDTH = 0.2;
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
const stopOffset1 = i / count;
|
||||
const stopOffset2 =
|
||||
stopOffset1 +
|
||||
(i / count ** 2) * (1 - minStrokeWidth) +
|
||||
minStrokeWidth / count;
|
||||
const computeTiltGradient = (stateObj: CoverEntity) => {
|
||||
const stops: string[] = [];
|
||||
|
||||
if (i !== 0) {
|
||||
gradient.push([stopOffset1, "transparent"]);
|
||||
}
|
||||
gradient.push([stopOffset1, "var(--control-slider-color)"]);
|
||||
gradient.push([stopOffset2, "var(--control-slider-color)"]);
|
||||
gradient.push([stopOffset2, "transparent"]);
|
||||
for (let i = 0; i < SLAT_COUNT; i++) {
|
||||
const tilt = ((i + 0.5) / SLAT_COUNT) * 100;
|
||||
const closedness = 1 - coverTiltOpenness(stateObj, tilt) / 100;
|
||||
const width = MIN_SLAT_WIDTH + (1 - MIN_SLAT_WIDTH) * closedness;
|
||||
const slatStart = ((i + (1 - width) / 2) / SLAT_COUNT) * 100;
|
||||
const slatEnd = ((i + (1 + width) / 2) / SLAT_COUNT) * 100;
|
||||
|
||||
stops.push(
|
||||
`transparent ${slatStart}%`,
|
||||
`var(--control-slider-color) ${slatStart}%`,
|
||||
`var(--control-slider-color) ${slatEnd}%`,
|
||||
`transparent ${slatEnd}%`
|
||||
);
|
||||
}
|
||||
|
||||
return unsafeCSS(
|
||||
gradient
|
||||
.map(([stop, color]) => `${color} ${(stop as number) * 100}%`)
|
||||
.join(", ")
|
||||
);
|
||||
}
|
||||
|
||||
const GRADIENT = generateTiltSliderTrackBackgroundGradient();
|
||||
return stops.join(", ");
|
||||
};
|
||||
|
||||
const supportsCoverTiltPositionCardFeatureFromState = (
|
||||
stateObj: HassEntity
|
||||
@@ -159,6 +157,10 @@ class HuiCoverTiltPositionCardFeature
|
||||
"--state-cover-inactive-color": openColor,
|
||||
};
|
||||
|
||||
const gradientStyle = {
|
||||
"--tilt-gradient": computeTiltGradient(this._stateObj),
|
||||
};
|
||||
|
||||
return html`
|
||||
<ha-control-slider
|
||||
style=${styleMap(style)}
|
||||
@@ -178,7 +180,11 @@ class HuiCoverTiltPositionCardFeature
|
||||
.unit=${DOMAIN_ATTRIBUTES_UNITS.cover.current_tilt_position}
|
||||
.locale=${this._locale}
|
||||
>
|
||||
<div slot="background" class="gradient"></div
|
||||
<div
|
||||
slot="background"
|
||||
class="gradient"
|
||||
style=${styleMap(gradientStyle)}
|
||||
></div
|
||||
></ha-control-slider>
|
||||
`;
|
||||
}
|
||||
@@ -198,9 +204,12 @@ class HuiCoverTiltPositionCardFeature
|
||||
cardFeatureStyles,
|
||||
css`
|
||||
.gradient {
|
||||
background: linear-gradient(to right, ${GRADIENT});
|
||||
background: linear-gradient(to left, var(--tilt-gradient));
|
||||
opacity: 0.6;
|
||||
}
|
||||
.gradient:dir(rtl) {
|
||||
background: linear-gradient(to right, var(--tilt-gradient));
|
||||
}
|
||||
`,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
coverSupportsPosition,
|
||||
coverSupportsSetValue,
|
||||
coverSupportsTiltPosition,
|
||||
coverTiltOpenness,
|
||||
isMoving,
|
||||
isOpening,
|
||||
} from "../../data/cover";
|
||||
@@ -573,12 +574,12 @@ export class HaStateControlCover extends LitElement {
|
||||
return this._formatValue(value);
|
||||
}
|
||||
|
||||
private _drawnTilt(): number {
|
||||
private _drawnTiltOpenness(): number {
|
||||
if (this.mode === "tilt" && this._drag) {
|
||||
return this._drag.value;
|
||||
return coverTiltOpenness(this.stateObj, this._drag.value);
|
||||
}
|
||||
return coverHasTiltControls(this.stateObj)
|
||||
? this._coverSprings.tilt.value
|
||||
? coverTiltOpenness(this.stateObj, this._coverSprings.tilt.value)
|
||||
: 0;
|
||||
}
|
||||
|
||||
@@ -689,7 +690,7 @@ export class HaStateControlCover extends LitElement {
|
||||
const withTarget = changing && (unknown || Math.abs(handle - cover) > 1);
|
||||
const parts = shape.render({
|
||||
position: this._coverSprings.position.value,
|
||||
tilt: this._drawnTilt(),
|
||||
tiltOpenness: this._drawnTiltOpenness(),
|
||||
wave: this._wave,
|
||||
});
|
||||
const flip = isHorizontal(shape) && this._isRtl;
|
||||
|
||||
@@ -22,7 +22,7 @@ export interface CoverWave {
|
||||
|
||||
export interface CoverShapeInput {
|
||||
position: number;
|
||||
tilt: number;
|
||||
tiltOpenness: number;
|
||||
wave: CoverWave;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ const PITCH = 12;
|
||||
|
||||
export const slatsShape: CoverShape = {
|
||||
...tiltGeometry,
|
||||
render: ({ tilt }) => {
|
||||
render: ({ tiltOpenness }) => {
|
||||
const count = Math.floor(FRAME.height / PITCH);
|
||||
const thickness = Math.max(1.2, (PITCH - 1) * (1 - tilt / 100));
|
||||
const thickness = Math.max(1.2, (PITCH - 1) * (1 - tiltOpenness / 100));
|
||||
return {
|
||||
cover: Array.from({ length: count }, (_, index) =>
|
||||
slat(FRAME.top + index * PITCH + (PITCH - thickness) / 2, thickness)
|
||||
|
||||
@@ -11,9 +11,9 @@ const CORDS = [0.22, 0.78];
|
||||
|
||||
export const venetianShape: CoverShape = {
|
||||
...rollingGeometry,
|
||||
render: ({ position, tilt }) => {
|
||||
render: ({ position, tiltOpenness }) => {
|
||||
const { left, top, width, height } = FRAME;
|
||||
const thickness = 9 - (7 * clampPercent(tilt)) / 100;
|
||||
const thickness = 9 - (7 * clampPercent(tiltOpenness)) / 100;
|
||||
const count = Math.ceil(height / PITCH) + 1;
|
||||
const first = top + thickness / 2;
|
||||
const pitch = (top + height - thickness / 2 - first) / (count - 1);
|
||||
|
||||
Reference in new issue
Block a user