Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gallery/src/pages/components/ha-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const SCHEMAS: {
select: "Select",
icon: "Icon",
media: "Media",
location: "Location",
},
schema: [
{ name: "addon", selector: { addon: {} } },
Expand Down Expand Up @@ -75,6 +76,10 @@ const SCHEMAS: {
media: {},
},
},
{
name: "location",
selector: { location: { radius: true, icon: "mdi:home" } },
},
],
},
{
Expand Down
5 changes: 5 additions & 0 deletions gallery/src/pages/components/ha-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ const SCHEMAS: {
},
icon: { name: "Icon", selector: { icon: {} } },
media: { name: "Media", selector: { media: {} } },
location: { name: "Location", selector: { location: {} } },
location_radius: {
name: "Location with radius",
selector: { location: { radius: true, icon: "mdi:home" } },
},
},
},
];
Expand Down
4 changes: 3 additions & 1 deletion src/components/ha-form/ha-form-constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export class HaFormConstant extends LitElement implements HaFormElement {
@property() public label!: string;

protected render(): TemplateResult {
return html`<span class="label">${this.label}</span>: ${this.schema.value}`;
return html`<span class="label">${this.label}</span>${this.schema.value
? `: ${this.schema.value}`
: ""}`;
}

static get styles(): CSSResultGroup {
Expand Down
4 changes: 3 additions & 1 deletion src/components/ha-form/ha-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { HomeAssistant } from "../../types";
const getValue = (obj, item) =>
obj ? (!item.name ? obj : obj[item.name]) : null;

const getError = (obj, item) => (obj && item.name ? obj[item.name] : null);

let selectorImported = false;

@customElement("ha-form")
Expand Down Expand Up @@ -84,7 +86,7 @@ export class HaForm extends LitElement implements HaFormElement {
`
: ""}
${this.schema.map((item) => {
const error = getValue(this.error, item);
const error = getError(this.error, item);

return html`
${error
Expand Down
2 changes: 1 addition & 1 deletion src/components/ha-form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface HaFormSelector extends HaFormBaseSchema {

export interface HaFormConstantSchema extends HaFormBaseSchema {
type: "constant";
value: string;
value?: string;
}

export interface HaFormIntegerSchema extends HaFormBaseSchema {
Expand Down
80 changes: 80 additions & 0 deletions src/components/ha-selector/ha-selector-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { html, LitElement } from "lit";
import { customElement, property } from "lit/decorators";
import memoizeOne from "memoize-one";
import { fireEvent } from "../../common/dom/fire_event";
import type {
LocationSelector,
LocationSelectorValue,
} from "../../data/selector";
import "../../panels/lovelace/components/hui-theme-select-editor";
import type { HomeAssistant } from "../../types";
import type { MarkerLocation } from "../map/ha-locations-editor";
import "../map/ha-locations-editor";

@customElement("ha-selector-location")
export class HaLocationSelector extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public selector!: LocationSelector;

@property() public value?: LocationSelectorValue;

@property() public label?: string;

@property({ type: Boolean, reflect: true }) public disabled = false;

protected render() {
return html`
<ha-locations-editor
class="flex"
.hass=${this.hass}
.locations=${this._location(this.selector, this.value)}
@location-updated=${this._locationChanged}
@radius-updated=${this._radiusChanged}
></ha-locations-editor>
`;
}

private _location = memoizeOne(
(
selector: LocationSelector,
value?: LocationSelectorValue
): MarkerLocation[] => {
const computedStyles = getComputedStyle(this);
const zoneRadiusColor = selector.location.radius
? computedStyles.getPropertyValue("--zone-radius-color") ||
computedStyles.getPropertyValue("--accent-color")
: undefined;
return [
{
id: "location",
latitude: value?.latitude || this.hass.config.latitude,
longitude: value?.longitude || this.hass.config.longitude,
radius: selector.location.radius ? value?.radius || 1000 : undefined,
radius_color: zoneRadiusColor,
icon: selector.location.icon,
location_editable: true,
radius_editable: true,
},
];
}
);

private _locationChanged(ev: CustomEvent) {
const [latitude, longitude] = ev.detail.location;
fireEvent(this, "value-changed", {
value: { ...this.value, latitude, longitude },
});
}

private _radiusChanged(ev: CustomEvent) {
const radius = ev.detail.radius;
fireEvent(this, "value-changed", { value: { ...this.value, radius } });
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-selector-location": HaLocationSelector;
}
}
1 change: 1 addition & 0 deletions src/components/ha-selector/ha-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import "./ha-selector-time";
import "./ha-selector-icon";
import "./ha-selector-media";
import "./ha-selector-theme";
import "./ha-selector-location";

@customElement("ha-selector")
export class HaSelector extends LitElement {
Expand Down
13 changes: 12 additions & 1 deletion src/data/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type Selector =
| SelectSelector
| IconSelector
| MediaSelector
| ThemeSelector;
| ThemeSelector
| LocationSelector;

export interface EntitySelector {
entity: {
Expand Down Expand Up @@ -164,6 +165,16 @@ export interface MediaSelector {
media: {};
}

export interface LocationSelector {
location: { radius?: boolean; icon?: string };
}

export interface LocationSelectorValue {
latitude: number;
longitude: number;
radius?: number;
}

export interface MediaSelectorValue {
entity_id?: string;
media_content_id?: string;
Expand Down
6 changes: 3 additions & 3 deletions src/data/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export interface Zone {
}

export interface ZoneMutableParams {
name: string;
icon?: string;
latitude: number;
longitude: number;
name: string;
passive: boolean;
radius: number;
passive?: boolean;
radius?: number;
}

export const fetchZones = (hass: HomeAssistant) =>
Expand Down
Loading