From 9ee1af9e775d01b788ae715c6b115d0b7678fd5e Mon Sep 17 00:00:00 2001 From: Raymond Julin Date: Wed, 31 Mar 2021 22:51:37 +0200 Subject: [PATCH] feat: Support custom domain/service calls and passing extra data. Fixes #211 --- README.md | 4 ++++ src/config/service.ts | 11 +++++++++++ src/main.ts | 9 ++++++++- src/types.ts | 9 +++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/config/service.ts diff --git a/README.md b/README.md index 31e261a..e0684ba 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,10 @@ resources: - `entity` _string_: The thermostat entity id **required** - `header` _false|Header object_: See section about header config - `setpoints` _false|Setpoints object_: See section about header config +- `service` _object_: Must specify both domain+service if overriding + - `domain` _string_: Override the service call domain + - `service` _string_: Override the service call name + - `data` _object_: Send extra data with the service call - `unit` _string|bool_: Override the unit to display. Set to false to hide unit - `decimals` _number_: Specify number of decimals to use: 1 or 0 - `fallback` _string_: Specify a text to display if a valid set point can't be determined. Defaults to `N/A` diff --git a/src/config/service.ts b/src/config/service.ts new file mode 100644 index 0000000..df7eee5 --- /dev/null +++ b/src/config/service.ts @@ -0,0 +1,11 @@ +import { Service } from '../types' + +export default function parseServie(config: false | Service): Service { + if (!config) { + return { + domain: 'climate', + service: 'set_temperature', + } + } + return config +} diff --git a/src/main.ts b/src/main.ts index 6fcb2c8..63b6692 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,6 +15,7 @@ import renderModeType from './components/modeType' import parseHeader, { HeaderData, MODE_ICONS } from './config/header' import parseSetpoints from './config/setpoints' +import parseService from './config/service' import { CardConfig, @@ -31,6 +32,7 @@ import { EntityValue, HVAC_MODES, MODES, + Service, } from './types' const DEBOUNCE_TIMEOUT = 1000 @@ -107,6 +109,8 @@ export default class SimpleThermostat extends LitElement { @property() header: false | HeaderData @property() + service: Service + @property() modes: Array = [] @property() _hass: HASS = {} @@ -134,8 +138,10 @@ export default class SimpleThermostat extends LitElement { _debouncedSetTemperature = debounce( (values: object) => { - this._hass.callService('climate', 'set_temperature', { + const { domain, service, data = {} } = this.service + this._hass.callService(domain, service, { entity_id: this.config.entity, + ...data, ...values, }) }, @@ -170,6 +176,7 @@ export default class SimpleThermostat extends LitElement { } this.header = parseHeader(this.config.header, entity, hass) + this.service = parseService(this.config?.service ?? false) const attributes = entity.attributes diff --git a/src/types.ts b/src/types.ts index 296893e..e009b5e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -64,6 +64,14 @@ export interface Setpoint { export type Setpoints = Record +export interface Service { + domain: string + service: string + data?: { + [key: string]: string + } +} + export interface CardConfig { entity?: string header: false | HeaderConfig @@ -75,6 +83,7 @@ export interface CardConfig { step_layout?: 'row' | 'column' unit?: boolean | string fallback?: string + service?: Service hide?: { setpoint?: boolean temperature?: boolean