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
42 changes: 40 additions & 2 deletions src/components/ha-switch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { customElement, CSSResult, css, query, property } from "lit-element";
import {
customElement,
CSSResult,
css,
query,
html,
property,
} from "lit-element";
import "@material/mwc-switch";
import { style } from "@material/mwc-switch/mwc-switch-css";
// tslint:disable-next-line
import { Switch } from "@material/mwc-switch";
import { Constructor } from "../types";
import { forwardHaptic } from "../data/haptics";
import { ripple } from "@material/mwc-ripple/ripple-directive";
// tslint:disable-next-line
const MwcSwitch = customElements.get("mwc-switch") as Constructor<Switch>;

Expand All @@ -26,14 +34,38 @@ export class HaSwitch extends MwcSwitch {
"slotted",
Boolean(this._slot.assignedNodes().length)
);
this._slot.addEventListener("click", () => (this.checked = !this.checked));
this.addEventListener("change", () => {
if (this.haptic) {
forwardHaptic("light");
}
});
}

protected render() {
return html`
<div class="mdc-switch">
<div class="mdc-switch__track"></div>
<div
class="mdc-switch__thumb-underlay"
.ripple="${ripple({
interactionNode: this,
})}"
>
<div class="mdc-switch__thumb">
<input
type="checkbox"
id="basic-switch"
class="mdc-switch__native-control"
role="switch"
@change="${this._haChangeHandler}"
/>
</div>
</div>
</div>
<label for="basic-switch"><slot></slot></label>
`;
}

protected static get styles(): CSSResult[] {
return [
style,
Expand Down Expand Up @@ -65,6 +97,12 @@ export class HaSwitch extends MwcSwitch {
`,
];
}

private _haChangeHandler(e: Event) {
this.mdcFoundation.handleChange(e);
// catch "click" event and sync properties
this.checked = this.formElement.checked;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't do this. This will lead to nasty bugs.

When a parent renders this element, it will diff against itself to know if it needs to change the property.

So let's say this happens:

  • Parent renders checked = true
  • User changes it, sets checked=false
  • Parent renders checked = true -> this will not update <ha-switch> because it already rendered true last time, even though the property current value is false

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually a copy/paste from mwc-switch, because it was a private function

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be weird of the parent not to update it's value btw when the switch is changed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, there is no value-changed event, just the normal change event of the native checkbox, so it would be impossible for the parent to see what the state is when we would not update the prop? ev.target.checked would no longer work.

I could rewrite it and fire the value-changed event and not do this, but that would differ us a lot from mwc-switch and means a lot of change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok let's keep it.

}
}

declare global {
Expand Down
14 changes: 13 additions & 1 deletion src/components/map/ha-location-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import {
} from "../../common/dom/setup-leaflet-map";
import { fireEvent } from "../../common/dom/fire_event";
import { nextRender } from "../../common/util/render-status";
import { defaultRadiusColor } from "../../data/zone";

@customElement("ha-location-editor")
class LocationEditor extends LitElement {
@property() public location?: [number, number];
@property() public radius?: number;
@property() public radiusColor?: string;
@property() public icon?: string;
public fitZoom = 16;
private _iconEl?: DivIcon;
Expand Down Expand Up @@ -83,6 +85,9 @@ class LocationEditor extends LitElement {
if (changedProps.has("radius")) {
this._updateRadius();
}
if (changedProps.has("radiusColor")) {
this._updateRadiusColor();
}
if (changedProps.has("icon")) {
this._updateIcon();
}
Expand Down Expand Up @@ -213,7 +218,7 @@ class LocationEditor extends LitElement {
this._leafletMap!.addLayer(this._locationMarker);
} else {
this._locationMarker = this.Leaflet!.circle(this.location, {
color: "#FF9800",
color: this.radiusColor || defaultRadiusColor,
radius: this.radius,
});
this._leafletMap!.addLayer(this._locationMarker);
Expand All @@ -228,6 +233,13 @@ class LocationEditor extends LitElement {
(this._locationMarker as Circle).setRadius(this.radius);
}

private _updateRadiusColor(): void {
if (!this._locationMarker || !this.radius) {
return;
}
(this._locationMarker as Circle).setStyle({ color: this.radiusColor });
}

static get styles(): CSSResult {
return css`
:host {
Expand Down
3 changes: 2 additions & 1 deletion src/components/map/ha-locations-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
LeafletModuleType,
} from "../../common/dom/setup-leaflet-map";
import { fireEvent } from "../../common/dom/fire_event";
import { defaultRadiusColor } from "../../data/zone";

declare global {
// for fire event
Expand Down Expand Up @@ -202,7 +203,7 @@ export class HaLocationsEditor extends LitElement {
const circle = this.Leaflet!.circle(
[location.latitude, location.longitude],
{
color: location.radius_color ? location.radius_color : "#FF9800",
color: location.radius_color || defaultRadiusColor,
radius: location.radius,
}
);
Expand Down
4 changes: 4 additions & 0 deletions src/data/zone.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { HomeAssistant } from "../types";

export const defaultRadiusColor = "#FF9800";
export const homeRadiusColor: string = "#03a9f4";
export const passiveRadiusColor: string = "#9b9b9b";

export interface Zone {
id: string;
name: string;
Expand Down
9 changes: 8 additions & 1 deletion src/panels/config/zone/dialog-zone-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import "../../../components/ha-dialog";

import { ZoneDetailDialogParams } from "./show-dialog-zone-detail";
import { HomeAssistant } from "../../../types";
import { ZoneMutableParams } from "../../../data/zone";
import {
ZoneMutableParams,
passiveRadiusColor,
defaultRadiusColor,
} from "../../../data/zone";
import { addDistanceToCoord } from "../../../common/location/add_distance_to_coord";

class DialogZoneDetail extends LitElement {
Expand Down Expand Up @@ -127,6 +131,9 @@ class DialogZoneDetail extends LitElement {
class="flex"
.location=${this._locationValue}
.radius=${this._radius}
.radiusColor=${this._passive
? passiveRadiusColor
: defaultRadiusColor}
.icon=${this._icon}
@change=${this._locationChanged}
></ha-location-editor>
Expand Down
11 changes: 7 additions & 4 deletions src/panels/config/zone/ha-config-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import {
updateZone,
deleteZone,
ZoneMutableParams,
homeRadiusColor,
passiveRadiusColor,
defaultRadiusColor,
} from "../../../data/zone";
// tslint:disable-next-line
import {
Expand Down Expand Up @@ -68,17 +71,17 @@ export class HaConfigZone extends SubscribeMixin(LitElement) {
radius: state.attributes.radius,
radius_color:
state.entity_id === "zone.home"
? "#03a9f4"
? homeRadiusColor
: state.attributes.passive
? "#9b9b9b"
: "#FF9800",
? passiveRadiusColor
: defaultRadiusColor,
editable: false,
};
});
const storageLocations: MarkerLocation[] = storageItems.map((zone) => {
return {
...zone,
radius_color: zone.passive ? "#9b9b9b" : "#FF9800",
radius_color: zone.passive ? passiveRadiusColor : defaultRadiusColor,
editable: true,
};
});
Expand Down
3 changes: 2 additions & 1 deletion src/panels/map/ha-panel-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { computeStateDomain } from "../../common/entity/compute_state_domain";
import { computeStateName } from "../../common/entity/compute_state_name";
import LocalizeMixin from "../../mixins/localize-mixin";
import { setupLeafletMap } from "../../common/dom/setup-leaflet-map";
import { defaultRadiusColor } from "../../data/zone";

/*
* @appliesMixin LocalizeMixin
Expand Down Expand Up @@ -175,7 +176,7 @@ class HaPanelMap extends LocalizeMixin(PolymerElement) {
[entity.attributes.latitude, entity.attributes.longitude],
{
interactive: false,
color: "#FF9800",
color: defaultRadiusColor,
radius: entity.attributes.radius,
}
).addTo(map)
Expand Down