Skip to content

Commit 7036cef

Browse files
bramkragtenballoob
andauthored
Allow to change the location of home zone in zone editor (#4849)
* Allow to change the location of home zone in zone editor * Update src/translations/en.json Co-Authored-By: Paulus Schoutsen <balloob@gmail.com> * Comment + mobile to general config * Remove dupe import Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
1 parent fb7fbf2 commit 7036cef

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

src/components/map/ha-locations-editor.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export interface MarkerLocation {
4141
id: string;
4242
icon?: string;
4343
radius_color?: string;
44-
editable?: boolean;
44+
location_editable?: boolean;
45+
radius_editable?: boolean;
4546
}
4647

4748
@customElement("ha-locations-editor")
@@ -208,7 +209,7 @@ export class HaLocationsEditor extends LitElement {
208209
}
209210
);
210211
circle.addTo(this._leafletMap!);
211-
if (location.editable) {
212+
if (location.radius_editable || location.location_editable) {
212213
// @ts-ignore
213214
circle.editing.enable();
214215
// @ts-ignore
@@ -230,19 +231,25 @@ export class HaLocationsEditor extends LitElement {
230231
// @ts-ignore
231232
(ev: MouseEvent) => this._markerClicked(ev)
232233
);
233-
resizeMarker.addEventListener(
234-
"dragend",
235-
// @ts-ignore
236-
(ev: DragEndEvent) => this._updateRadius(ev)
237-
);
234+
if (location.radius_editable) {
235+
resizeMarker.addEventListener(
236+
"dragend",
237+
// @ts-ignore
238+
(ev: DragEndEvent) => this._updateRadius(ev)
239+
);
240+
} else {
241+
resizeMarker.remove();
242+
}
238243
this._locationMarkers![location.id] = circle;
239244
} else {
240245
this._circles[location.id] = circle;
241246
}
242247
}
243-
if (!location.radius || !location.editable) {
248+
if (
249+
!location.radius ||
250+
(!location.radius_editable && !location.location_editable)
251+
) {
244252
const options: MarkerOptions = {
245-
draggable: Boolean(location.editable),
246253
title: location.name,
247254
};
248255

src/panels/config/zone/ha-config-zone.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
4747
import { subscribeEntityRegistry } from "../../../data/entity_registry";
4848
import { configSections } from "../ha-panel-config";
4949
import { navigate } from "../../../common/navigate";
50+
import { saveCoreConfig } from "../../../data/core";
51+
import { ifDefined } from "lit-html/directives/if-defined";
52+
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box";
5053

5154
@customElement("ha-config-zone")
5255
export class HaConfigZone extends SubscribeMixin(LitElement) {
@@ -57,6 +60,7 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
5760
@property() private _storageItems?: Zone[];
5861
@property() private _stateItems?: HassEntity[];
5962
@property() private _activeEntry: string = "";
63+
@property() private _canEditCore = false;
6064
@query("ha-locations-editor") private _map?: HaLocationsEditor;
6165
private _regEntities: string[] = [];
6266

@@ -76,14 +80,17 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
7680
: state.attributes.passive
7781
? passiveRadiusColor
7882
: defaultRadiusColor,
79-
editable: false,
83+
location_editable:
84+
state.entity_id === "zone.home" && this._canEditCore,
85+
radius_editable: false,
8086
};
8187
});
8288
const storageLocations: MarkerLocation[] = storageItems.map((zone) => {
8389
return {
8490
...zone,
8591
radius_color: zone.passive ? passiveRadiusColor : defaultRadiusColor,
86-
editable: true,
92+
location_editable: true,
93+
radius_editable: true,
8794
};
8895
});
8996
return storageLocations.concat(stateLocations);
@@ -166,12 +173,23 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
166173
<paper-icon-button
167174
.entityId=${state.entity_id}
168175
icon="hass:pencil"
169-
disabled
176+
@click=${this._openCoreConfig}
177+
disabled=${ifDefined(
178+
state.entity_id === "zone.home" &&
179+
this.narrow &&
180+
this._canEditCore
181+
? undefined
182+
: true
183+
)}
170184
></paper-icon-button>
171185
<paper-tooltip position="left">
172186
${state.entity_id === "zone.home"
173187
? this.hass.localize(
174-
"ui.panel.config.zone.edit_home_zone"
188+
`ui.panel.config.zone.${
189+
this.narrow
190+
? "edit_home_zone_narrow"
191+
: "edit_home_zone"
192+
}`
175193
)
176194
: this.hass.localize(
177195
"ui.panel.config.zone.configured_in_yaml"
@@ -234,6 +252,9 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
234252

235253
protected firstUpdated(changedProps: PropertyValues) {
236254
super.firstUpdated(changedProps);
255+
this._canEditCore =
256+
Boolean(this.hass.user?.is_admin) &&
257+
["storage", "default"].includes(this.hass.config.config_source);
237258
this._fetchData();
238259
if (this.route.path === "/new") {
239260
navigate(this, "/config/zone", true);
@@ -288,8 +309,15 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
288309
}
289310
}
290311

291-
private _locationUpdated(ev: CustomEvent) {
312+
private async _locationUpdated(ev: CustomEvent) {
292313
this._activeEntry = ev.detail.id;
314+
if (ev.detail.id === "zone.home" && this._canEditCore) {
315+
await saveCoreConfig(this.hass, {
316+
latitude: ev.detail.location[0],
317+
longitude: ev.detail.location[1],
318+
});
319+
return;
320+
}
293321
const entry = this._storageItems!.find((item) => item.id === ev.detail.id);
294322
if (!entry) {
295323
return;
@@ -319,7 +347,7 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
319347
this._openDialog();
320348
}
321349

322-
private _itemClicked(ev: MouseEvent) {
350+
private _itemClicked(ev: Event) {
323351
if (this.narrow) {
324352
this._openEditEntry(ev);
325353
return;
@@ -328,7 +356,7 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
328356
this._zoomZone(entry.id);
329357
}
330358

331-
private _stateItemClicked(ev: MouseEvent) {
359+
private _stateItemClicked(ev: Event) {
332360
const entityId = (ev.currentTarget! as HTMLElement).getAttribute(
333361
"data-id"
334362
)!;
@@ -339,11 +367,29 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
339367
this._map?.fitMarker(id);
340368
}
341369

342-
private _openEditEntry(ev: MouseEvent) {
370+
private _openEditEntry(ev: Event) {
343371
const entry: Zone = (ev.currentTarget! as any).entry;
344372
this._openDialog(entry);
345373
}
346374

375+
private async _openCoreConfig(ev: Event) {
376+
const entityId: string = (ev.currentTarget! as any).entityId;
377+
if (entityId !== "zone.home" || !this.narrow || !this._canEditCore) {
378+
return;
379+
}
380+
if (
381+
!(await showConfirmationDialog(this, {
382+
title: this.hass.localize("ui.panel.config.zone.go_to_core_config"),
383+
text: this.hass.localize("ui.panel.config.zone.home_zone_core_config"),
384+
confirmText: this.hass!.localize("ui.common.yes"),
385+
dismissText: this.hass!.localize("ui.common.no"),
386+
}))
387+
) {
388+
return;
389+
}
390+
navigate(this, "/config/core");
391+
}
392+
347393
private async _createEntry(values: ZoneMutableParams) {
348394
const created = await createZone(this.hass!, values);
349395
this._storageItems = this._storageItems!.concat(

src/translations/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,10 @@
14151415
"add_zone": "Add Zone",
14161416
"confirm_delete": "Are you sure you want to delete this zone?",
14171417
"configured_in_yaml": "Zones configured via configuration.yaml cannot be edited via the UI.",
1418-
"edit_home_zone": "The location of your home can be changed in the general configuration.",
1418+
"edit_home_zone": "The radius of the Home zone can't be edited from the frontend yet. Drag the marker on the map to move the home zone.",
1419+
"edit_home_zone_narrow": "The radius of the Home zone can't be edited from the frontend yet. The location can be changed from the general configuration.",
1420+
"go_to_core_config": "Go to general configuration?",
1421+
"home_zone_core_config": "The location of your home zone is editable from the general configuration page. The radius of the Home zone can't be edited from the frontend yet. Do you want to go to the general configuration?",
14191422
"detail": {
14201423
"new_zone": "New Zone",
14211424
"name": "Name",

0 commit comments

Comments
 (0)