Skip to content

Commit

Permalink
feat: Support custom domain/service calls and passing extra data. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nervetattoo committed Mar 31, 2021
1 parent c9da47f commit 0a93120
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
11 changes: 11 additions & 0 deletions src/config/service.ts
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,6 +32,7 @@ import {
EntityValue,
HVAC_MODES,
MODES,
Service,
} from './types'

const DEBOUNCE_TIMEOUT = 1000
Expand Down Expand Up @@ -107,6 +109,8 @@ export default class SimpleThermostat extends LitElement {
@property()
header: false | HeaderData
@property()
service: Service
@property()
modes: Array<ControlMode> = []
@property()
_hass: HASS = {}
Expand Down Expand Up @@ -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,
})
},
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export interface Setpoint {

export type Setpoints = Record<string, Setpoint>

export interface Service {
domain: string
service: string
data?: {
[key: string]: string
}
}

export interface CardConfig {
entity?: string
header: false | HeaderConfig
Expand All @@ -75,6 +83,7 @@ export interface CardConfig {
step_layout?: 'row' | 'column'
unit?: boolean | string
fallback?: string
service?: Service
hide?: {
setpoint?: boolean
temperature?: boolean
Expand Down

0 comments on commit 0a93120

Please sign in to comment.