Add Color RGB Selector (#12039)

This commit is contained in:
Zack Barett 2022-03-15 17:34:02 -05:00 committed by GitHub
parent db78b046a2
commit 5c53bc4225
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 2 deletions

View File

@ -173,10 +173,11 @@ const SCHEMAS: {
name: "Location with radius", name: "Location with radius",
selector: { location: { radius: true, icon: "mdi:home" } }, selector: { location: { radius: true, icon: "mdi:home" } },
}, },
"color-temp": { color_temp: {
name: "Color Temperature", name: "Color Temperature",
selector: { color_temp: {} }, selector: { color_temp: {} },
}, },
color_rgb: { name: "Color", selector: { color_rgb: {} } },
}, },
}, },
{ {

View File

@ -0,0 +1,58 @@
import { css, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators";
import type { HomeAssistant } from "../../types";
import type { ColorRGBSelector } from "../../data/selector";
import { fireEvent } from "../../common/dom/fire_event";
import { hex2rgb, rgb2hex } from "../../common/color/convert-color";
import "../ha-textfield";
@customElement("ha-selector-color_rgb")
export class HaColorRGBSelector extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@property({ attribute: false }) public selector!: ColorRGBSelector;
@property() public value?: string;
@property() public label?: string;
@property({ type: Boolean, reflect: true }) public disabled = false;
protected render() {
return html`
<ha-textfield
type="color"
.value=${this.value ? rgb2hex(this.value as any) : ""}
.label=${this.label || ""}
@change=${this._valueChanged}
></ha-textfield>
`;
}
private _valueChanged(ev: CustomEvent) {
const value = (ev.target as any).value;
fireEvent(this, "value-changed", {
value: hex2rgb(value),
});
}
static styles = css`
:host {
display: flex;
justify-content: flex-end;
align-items: center;
}
ha-textfield {
--text-field-padding: 8px;
min-width: 75px;
flex-grow: 1;
margin: 0 4px;
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"ha-selector-color_rgb": HaColorRGBSelector;
}
}

View File

@ -8,6 +8,7 @@ import "./ha-selector-addon";
import "./ha-selector-area"; import "./ha-selector-area";
import "./ha-selector-attribute"; import "./ha-selector-attribute";
import "./ha-selector-boolean"; import "./ha-selector-boolean";
import "./ha-selector-color-rgb";
import "./ha-selector-device"; import "./ha-selector-device";
import "./ha-selector-duration"; import "./ha-selector-duration";
import "./ha-selector-entity"; import "./ha-selector-entity";

View File

@ -17,7 +17,8 @@ export type Selector =
| MediaSelector | MediaSelector
| ThemeSelector | ThemeSelector
| LocationSelector | LocationSelector
| ColorTempSelector; | ColorTempSelector
| ColorRGBSelector;
export interface EntitySelector { export interface EntitySelector {
entity: { entity: {
@ -34,6 +35,11 @@ export interface AttributeSelector {
}; };
} }
export interface ColorRGBSelector {
// eslint-disable-next-line @typescript-eslint/ban-types
color_rgb: {};
}
export interface DeviceSelector { export interface DeviceSelector {
device: { device: {
integration?: string; integration?: string;