Skip to content

Commit

Permalink
Fix doorbell event support
Browse files Browse the repository at this point in the history
  • Loading branch information
JELoohuis committed Sep 23, 2024
1 parent bf9cfa0 commit b36509e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/camera/TuyaCameraConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const CAMERA_ALARM_CAPABILITIES = {
decibel_switch: 'alarm_sound',
cry_detection_switch: 'alarm_crying_child',
pet_detection: 'alarm_pet',
doorbell_active: 'hidden.doorbell',
} as const;

// Map from an event to an alarm capability
Expand Down
15 changes: 11 additions & 4 deletions lib/camera/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,17 @@ abstract class TuyaOAuth2DeviceWithCamera extends TuyaTimeOutAlarmDevice {
}

// Event messages
if (
(statusKey === 'initiative_message' && changed.includes('initiative_message')) ||
(statusKey === 'alarm_message' && changed.includes('alarm_message'))
) {
if (statusKey === 'event_message' && changed.includes('event_message')) {
const event_message = value as { etype: string };
if (event_message.etype === 'ac_doorbell') {
if (!this.hasCapability('hidden.doorbell')) {
await this.addCapability('hidden.doorbell').catch(this.error);
} else {
await this.homey.flow.getDeviceTriggerCard(this.DOORBELL_TRIGGER_FLOW).trigger(this).catch(this.error);
}
}
}

if (statusKey === 'initiative_message' && changed.includes('initiative_message')) {
// Event messages are base64 encoded JSON
const encoded = status[statusKey] as string;
Expand Down
5 changes: 4 additions & 1 deletion lib/camera/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class TuyaOAuth2DriverWithCamera extends TuyaOAuth2Driver {
}

// More complicated capabilities
if (constIncludes(COMPLEX_CAMERA_CAPABILITIES, capability)) {
if (
constIncludes(COMPLEX_CAMERA_CAPABILITIES, capability) ||
constIncludes(SIMPLE_CAMERA_CAPABILITIES.setting, capability)
) {
props.store.tuya_capabilities.push(capability);
}
}
Expand Down
16 changes: 12 additions & 4 deletions lib/webhooks/TuyaWebhookParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ type IotCoreStatusEvent = {
properties?: Array<TuyaIotCoreStatusUpdate<unknown>>;
};
};
type EventEvent = {
event: 'event';
data: {
etype: string;
edata: string;
};
};

type TuyaWebhookData = OnlineEvent | OfflineEvent | StatusEvent | IotCoreStatusEvent;
export type TuyaWebhookData = OnlineEvent | OfflineEvent | StatusEvent | IotCoreStatusEvent | EventEvent;

export default class TuyaWebhookParser {
private readonly logContext;
Expand All @@ -39,14 +46,15 @@ export default class TuyaWebhookParser {
break;
case 'status':
statusUpdate = convertStatusArrayToStatusObject(message.data.deviceStatus);

break;
case 'iot_core_status':
statusUpdate = convertStatusArrayToStatusObject(message.data.properties);

break;
case 'event':
statusUpdate = { event_message: message.data };
break;
default:
throw new Error(`Unknown Webhook Event: ${message}`);
throw new Error(`Unknown Webhook Event: ${JSON.stringify(message)}`);
}

const changedStatusCodes = Object.keys(statusUpdate);
Expand Down
3 changes: 2 additions & 1 deletion types/TuyaTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type TuyaOAuth2Device from '../lib/TuyaOAuth2Device';
import { TuyaWebhookData } from '../lib/webhooks/TuyaWebhookParser';

export type TuyaStatus = Record<string, unknown>;
export type TuyaStatusSource = 'sync' | 'online' | 'offline' | 'status' | 'iot_core_status';
export type TuyaStatusSource = 'sync' | TuyaWebhookData['event'];

// Legacy status update
export type TuyaStatusUpdate<T> = {
Expand Down

0 comments on commit b36509e

Please sign in to comment.