mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-23 09:16:38 +00:00
Convert toggle functions to TypeScript (#2082)
* Convert toggle functions to TypeScript * Update hui-picture-glance-card.ts * Update hui-picture-glance-card.ts
This commit is contained in:
parent
a82561355c
commit
3497cb892e
@ -10,12 +10,12 @@ import { styleMap } from "lit-html/directives/styleMap";
|
||||
|
||||
import "../../../components/ha-card";
|
||||
|
||||
import toggleEntity from "../common/entity/toggle-entity";
|
||||
import isValidEntityId from "../../../common/entity/valid_entity_id";
|
||||
import stateIcon from "../../../common/entity/state_icon";
|
||||
import computeStateDomain from "../../../common/entity/compute_state_domain";
|
||||
import computeStateName from "../../../common/entity/compute_state_name";
|
||||
import applyThemesOnElement from "../../../common/dom/apply_themes_on_element";
|
||||
import { toggleEntity } from "../common/entity/toggle-entity";
|
||||
import { HomeAssistant, LightEntity } from "../../../types";
|
||||
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
||||
import { LovelaceCard, LovelaceConfig } from "../types";
|
||||
@ -199,7 +199,7 @@ class HuiEntityButtonCard extends hassLocalizeLitMixin(LitElement)
|
||||
const action = hold ? config.hold_action : config.tap_action || "more-info";
|
||||
switch (action) {
|
||||
case "toggle":
|
||||
toggleEntity(this.hass, entityId);
|
||||
toggleEntity(this.hass!, entityId);
|
||||
break;
|
||||
case "call-service":
|
||||
if (!config.service) {
|
||||
|
@ -13,12 +13,12 @@ import { HomeAssistant } from "../../../types";
|
||||
import { LovelaceCard, LovelaceConfig, LovelaceCardEditor } from "../types";
|
||||
import { longPress } from "../common/directives/long-press-directive";
|
||||
import { EntityConfig } from "../entity-rows/types";
|
||||
import { toggleEntity } from "../common/entity/toggle-entity";
|
||||
|
||||
import computeStateDisplay from "../../../common/entity/compute_state_display";
|
||||
import computeStateName from "../../../common/entity/compute_state_name";
|
||||
import processConfigEntities from "../common/process-config-entities";
|
||||
import applyThemesOnElement from "../../../common/dom/apply_themes_on_element";
|
||||
import toggleEntity from "../common/entity/toggle-entity";
|
||||
|
||||
import "../../../components/entity/state-badge";
|
||||
import "../../../components/ha-card";
|
||||
@ -245,7 +245,7 @@ export class HuiGlanceCard extends hassLocalizeLitMixin(LitElement)
|
||||
const action = hold ? config.hold_action : config.tap_action || "more-info";
|
||||
switch (action) {
|
||||
case "toggle":
|
||||
toggleEntity(this.hass, entityId);
|
||||
toggleEntity(this.hass!, entityId);
|
||||
break;
|
||||
case "call-service":
|
||||
const [domain, service] = config.service!.split(".", 2);
|
||||
|
@ -7,10 +7,10 @@ import "../components/hui-image";
|
||||
import computeDomain from "../../../common/entity/compute_domain";
|
||||
import computeStateDisplay from "../../../common/entity/compute_state_display";
|
||||
import computeStateName from "../../../common/entity/compute_state_name";
|
||||
import toggleEntity from "../common/entity/toggle-entity";
|
||||
|
||||
import EventsMixin from "../../../mixins/events-mixin";
|
||||
import LocalizeMixin from "../../../mixins/localize-mixin";
|
||||
import { toggleEntity } from "../common/entity/toggle-entity";
|
||||
import { longPressBind } from "../common/directives/long-press-directive";
|
||||
|
||||
const UNAVAILABLE = "Unavailable";
|
||||
|
@ -9,13 +9,13 @@ import { LovelaceCard, LovelaceConfig } from "../types";
|
||||
import { EntityConfig } from "../entity-rows/types";
|
||||
import { navigate } from "../../../common/navigate";
|
||||
import { HomeAssistant } from "../../../types";
|
||||
import { toggleEntity } from "../common/entity/toggle-entity";
|
||||
|
||||
import computeStateDisplay from "../../../common/entity/compute_state_display";
|
||||
import computeStateName from "../../../common/entity/compute_state_name";
|
||||
import processConfigEntities from "../common/process-config-entities";
|
||||
import computeDomain from "../../../common/entity/compute_domain";
|
||||
import stateIcon from "../../../common/entity/state_icon";
|
||||
import toggleEntity from "../common/entity/toggle-entity";
|
||||
|
||||
import "../../../components/ha-card";
|
||||
import "../../../components/ha-icon";
|
||||
@ -172,7 +172,7 @@ class HuiPictureGlanceCard extends hassLocalizeLitMixin(LitElement)
|
||||
}
|
||||
|
||||
private _callService(ev: MouseEvent): void {
|
||||
toggleEntity(this.hass, (ev.target as any).entity);
|
||||
toggleEntity(this.hass!, (ev.target as any).entity);
|
||||
}
|
||||
|
||||
private _handleImageClick(): void {
|
||||
|
@ -1,7 +0,0 @@
|
||||
import { STATES_OFF } from "../../../../common/const";
|
||||
import turnOnOffEntity from "./turn-on-off-entity";
|
||||
|
||||
export default function toggleEntity(hass, entityId) {
|
||||
const turnOn = STATES_OFF.includes(hass.states[entityId].state);
|
||||
turnOnOffEntity(hass, entityId, turnOn);
|
||||
}
|
10
src/panels/lovelace/common/entity/toggle-entity.ts
Normal file
10
src/panels/lovelace/common/entity/toggle-entity.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { STATES_OFF } from "../../../../common/const";
|
||||
import { turnOnOffEntity } from "./turn-on-off-entity";
|
||||
import { HomeAssistant } from "../../../../types";
|
||||
export const toggleEntity = (
|
||||
hass: HomeAssistant,
|
||||
entityId: string
|
||||
): Promise<void> => {
|
||||
const turnOn = STATES_OFF.includes(hass.states[entityId].state);
|
||||
return turnOnOffEntity(hass, entityId, turnOn);
|
||||
};
|
@ -1,7 +1,12 @@
|
||||
import { STATES_OFF } from "../../../../common/const";
|
||||
import computeDomain from "../../../../common/entity/compute_domain";
|
||||
import { STATES_OFF } from "../../../../common/const";
|
||||
import { HomeAssistant } from "../../../../types";
|
||||
|
||||
export default function turnOnOffEntities(hass, entityIds, turnOn = true) {
|
||||
export const turnOnOffEntities = (
|
||||
hass: HomeAssistant,
|
||||
entityIds: string[],
|
||||
turnOn = true
|
||||
): void => {
|
||||
const domainsToCall = {};
|
||||
entityIds.forEach((entityId) => {
|
||||
if (STATES_OFF.includes(hass.states[entityId].state) === turnOn) {
|
||||
@ -10,7 +15,9 @@ export default function turnOnOffEntities(hass, entityIds, turnOn = true) {
|
||||
? stateDomain
|
||||
: "homeassistant";
|
||||
|
||||
if (!(serviceDomain in domainsToCall)) domainsToCall[serviceDomain] = [];
|
||||
if (!(serviceDomain in domainsToCall)) {
|
||||
domainsToCall[serviceDomain] = [];
|
||||
}
|
||||
domainsToCall[serviceDomain].push(entityId);
|
||||
}
|
||||
});
|
||||
@ -31,4 +38,4 @@ export default function turnOnOffEntities(hass, entityIds, turnOn = true) {
|
||||
const entities = domainsToCall[domain];
|
||||
hass.callService(domain, service, { entity_id: entities });
|
||||
});
|
||||
}
|
||||
};
|
@ -1,6 +1,11 @@
|
||||
import computeDomain from "../../../../common/entity/compute_domain";
|
||||
import { HomeAssistant } from "../../../../types";
|
||||
|
||||
export default function turnOnOffEntity(hass, entityId, turnOn = true) {
|
||||
export const turnOnOffEntity = (
|
||||
hass: HomeAssistant,
|
||||
entityId: string,
|
||||
turnOn = true
|
||||
): Promise<void> => {
|
||||
const stateDomain = computeDomain(entityId);
|
||||
const serviceDomain = stateDomain === "group" ? "homeassistant" : stateDomain;
|
||||
|
||||
@ -16,5 +21,5 @@ export default function turnOnOffEntity(hass, entityId, turnOn = true) {
|
||||
service = turnOn ? "turn_on" : "turn_off";
|
||||
}
|
||||
|
||||
hass.callService(serviceDomain, service, { entity_id: entityId });
|
||||
}
|
||||
return hass.callService(serviceDomain, service, { entity_id: entityId });
|
||||
};
|
@ -2,7 +2,7 @@ import { HomeAssistant } from "../../../types";
|
||||
import { LovelaceElementConfig } from "../elements/types";
|
||||
import { fireEvent } from "../../../common/dom/fire_event";
|
||||
import { navigate } from "../../../common/navigate";
|
||||
import toggleEntity from "../../../../src/panels/lovelace/common/entity/toggle-entity";
|
||||
import { toggleEntity } from "../../../../src/panels/lovelace/common/entity/toggle-entity";
|
||||
|
||||
export const handleClick = (
|
||||
node: HTMLElement,
|
||||
@ -28,7 +28,7 @@ export const handleClick = (
|
||||
navigate(node, config.navigation_path ? config.navigation_path : "");
|
||||
break;
|
||||
case "toggle":
|
||||
toggleEntity(hass, config.entity);
|
||||
toggleEntity(hass, config.entity!);
|
||||
break;
|
||||
case "call-service": {
|
||||
if (config.service) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user