Skip to content

Commit

Permalink
fix: use api to turn light groups on/off
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Jun 7, 2019
1 parent 50a4e9e commit 4de244b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions api/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,23 @@ export class Location {
return this.sendCommandToSecurityPanel('security-panel.silence-siren')
}

setLightGroup(groupId: string, on: boolean, durationSeconds = 60) {
console.log('SETTING LIGHT GROUP', groupId, on, durationSeconds)
this.restClient.request<any>(
'POST',
`https://api.ring.com/groups/v1/locations/${
this.locationId
}/groups/${groupId}/devices`,
{
lights_on: {
duration_seconds: durationSeconds,
enabled: on
}
},
true
)
}

getNextMessageOfType(type: MessageType, src: string) {
return this.onMessage
.pipe(
Expand Down
11 changes: 7 additions & 4 deletions api/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,30 @@ export class RingRestClient {
async request<T = void>(
method: 'GET' | 'POST',
url: string,
data?: any
data?: any,
json = false
): Promise<T> {
const token = await this.authTokenPromise
const headers = {
'content-type': 'application/x-www-form-urlencoded',
'content-type': json
? 'application/json'
: 'application/x-www-form-urlencoded',
authorization: `Bearer ${token}`
}

try {
return await requestWithRetry<T>({
method,
url,
data: querystring.stringify(data),
data: json ? data : querystring.stringify(data),
headers
})
} catch (e) {
const response = e.response || {}

if (response.status === 401) {
this.authTokenPromise = this.getAuthToken()
return this.request(method, url, data)
return this.request(method, url, data, json)
}

if (
Expand Down
1 change: 1 addition & 0 deletions api/ring-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface RingDeviceData {
co?: { alarmStatus?: 'active' }
smoke?: { alarmStatus?: 'active' }
motionStatus?: 'clear' | 'faulted'
groupId?: string

// switch
on?: boolean
Expand Down
1 change: 1 addition & 0 deletions homebridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ This works well if you automatically arm/disarm on leave/arrive (see setup instr
when you turn on a light via the Ring app. To force a duration when the light is turned on from HomeKit,
set this option to a specific number of seconds.
If this option is not set, the lights will use the duration from the previous time the light was turned on in the Ring app.
For light groups, this will default to 60 seconds.
The maximum value is `32767`, which is ~9.1 hours.

`hideLightGroups`: Ring smart lighting allows you to create lighting groups within the Ring app.
Expand Down
9 changes: 9 additions & 0 deletions homebridge/beam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { RingAlarmPlatformConfig } from './config'
import { BaseAccessory } from './base-accessory'

export class Beam extends BaseAccessory {
isLightGroup =
this.device.data.deviceType === RingDeviceType.BeamsLightGroupSwitch
groupId = this.device.data.groupId

constructor(
public readonly device: RingDevice,
public readonly accessory: HAP.Accessory,
Expand Down Expand Up @@ -53,6 +57,11 @@ export class Beam extends BaseAccessory {
const duration = beamDurationSeconds
? Math.min(beamDurationSeconds, 32767)
: undefined

if (this.isLightGroup && this.groupId) {
return this.device.location.setLightGroup(this.groupId, on, duration)
}

const data = on ? { lightMode: 'on', duration } : { lightMode: 'default' }

return this.device.sendCommand('light-mode.set', data)
Expand Down

0 comments on commit 4de244b

Please sign in to comment.