feat(smarthome): add smart home features and Home Assistant integration (WebSocket protocol, discovery, factories, interfaces)
This commit is contained in:
@@ -353,6 +353,324 @@ function parseScanSources(txtRecords: Record<string, string>): TScanSource[] {
|
||||
return sources.length > 0 ? sources : ['flatbed'];
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Smart Home Factories
|
||||
// ============================================================================
|
||||
|
||||
import { SwitchFeature, type ISwitchFeatureOptions } from '../features/feature.switch.js';
|
||||
import { SensorFeature, type ISensorFeatureOptions } from '../features/feature.sensor.js';
|
||||
import { LightFeature, type ILightFeatureOptions } from '../features/feature.light.js';
|
||||
import { CoverFeature, type ICoverFeatureOptions } from '../features/feature.cover.js';
|
||||
import { LockFeature, type ILockFeatureOptions } from '../features/feature.lock.js';
|
||||
import { FanFeature, type IFanFeatureOptions } from '../features/feature.fan.js';
|
||||
import { ClimateFeature, type IClimateFeatureOptions } from '../features/feature.climate.js';
|
||||
import { CameraFeature, type ICameraFeatureOptions } from '../features/feature.camera.js';
|
||||
|
||||
import type {
|
||||
TSwitchProtocol,
|
||||
TSensorProtocol,
|
||||
TLightProtocol,
|
||||
TCoverProtocol,
|
||||
TLockProtocol,
|
||||
TFanProtocol,
|
||||
TClimateProtocol,
|
||||
TCameraProtocol,
|
||||
ISwitchProtocolClient,
|
||||
ISensorProtocolClient,
|
||||
ILightProtocolClient,
|
||||
ICoverProtocolClient,
|
||||
ILockProtocolClient,
|
||||
IFanProtocolClient,
|
||||
IClimateProtocolClient,
|
||||
ICameraProtocolClient,
|
||||
ILightCapabilities,
|
||||
ICoverCapabilities,
|
||||
IFanCapabilities,
|
||||
IClimateCapabilities,
|
||||
ICameraCapabilities,
|
||||
TSensorDeviceClass,
|
||||
TSensorStateClass,
|
||||
TCoverDeviceClass,
|
||||
} from '../interfaces/smarthome.interfaces.js';
|
||||
|
||||
// Smart Switch Factory
|
||||
export interface ISmartSwitchDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TSwitchProtocol;
|
||||
protocolClient: ISwitchProtocolClient;
|
||||
deviceClass?: 'outlet' | 'switch';
|
||||
}
|
||||
|
||||
export function createSmartSwitch(
|
||||
info: ISmartSwitchDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const switchFeature = new SwitchFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
deviceClass: info.deviceClass,
|
||||
});
|
||||
|
||||
device.addFeature(switchFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// Smart Sensor Factory
|
||||
export interface ISmartSensorDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TSensorProtocol;
|
||||
protocolClient: ISensorProtocolClient;
|
||||
deviceClass?: TSensorDeviceClass;
|
||||
stateClass?: TSensorStateClass;
|
||||
unit?: string;
|
||||
}
|
||||
|
||||
export function createSmartSensor(
|
||||
info: ISmartSensorDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const sensorFeature = new SensorFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
deviceClass: info.deviceClass,
|
||||
stateClass: info.stateClass,
|
||||
unit: info.unit,
|
||||
});
|
||||
|
||||
device.addFeature(sensorFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// Smart Light Factory
|
||||
export interface ISmartLightDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TLightProtocol;
|
||||
protocolClient: ILightProtocolClient;
|
||||
capabilities?: Partial<ILightCapabilities>;
|
||||
}
|
||||
|
||||
export function createSmartLight(
|
||||
info: ISmartLightDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const lightFeature = new LightFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
capabilities: info.capabilities,
|
||||
});
|
||||
|
||||
device.addFeature(lightFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// Smart Cover Factory
|
||||
export interface ISmartCoverDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TCoverProtocol;
|
||||
protocolClient: ICoverProtocolClient;
|
||||
deviceClass?: TCoverDeviceClass;
|
||||
capabilities?: Partial<ICoverCapabilities>;
|
||||
}
|
||||
|
||||
export function createSmartCover(
|
||||
info: ISmartCoverDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const coverFeature = new CoverFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
deviceClass: info.deviceClass,
|
||||
capabilities: info.capabilities,
|
||||
});
|
||||
|
||||
device.addFeature(coverFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// Smart Lock Factory
|
||||
export interface ISmartLockDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TLockProtocol;
|
||||
protocolClient: ILockProtocolClient;
|
||||
supportsOpen?: boolean;
|
||||
}
|
||||
|
||||
export function createSmartLock(
|
||||
info: ISmartLockDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const lockFeature = new LockFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
supportsOpen: info.supportsOpen,
|
||||
});
|
||||
|
||||
device.addFeature(lockFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// Smart Fan Factory
|
||||
export interface ISmartFanDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TFanProtocol;
|
||||
protocolClient: IFanProtocolClient;
|
||||
capabilities?: Partial<IFanCapabilities>;
|
||||
}
|
||||
|
||||
export function createSmartFan(
|
||||
info: ISmartFanDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const fanFeature = new FanFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
capabilities: info.capabilities,
|
||||
});
|
||||
|
||||
device.addFeature(fanFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// Smart Climate Factory
|
||||
export interface ISmartClimateDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TClimateProtocol;
|
||||
protocolClient: IClimateProtocolClient;
|
||||
capabilities?: Partial<IClimateCapabilities>;
|
||||
}
|
||||
|
||||
export function createSmartClimate(
|
||||
info: ISmartClimateDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const climateFeature = new ClimateFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
capabilities: info.capabilities,
|
||||
});
|
||||
|
||||
device.addFeature(climateFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// Smart Camera Factory
|
||||
export interface ISmartCameraDiscoveryInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
address: string;
|
||||
port: number;
|
||||
entityId: string;
|
||||
protocol: TCameraProtocol;
|
||||
protocolClient: ICameraProtocolClient;
|
||||
capabilities?: Partial<ICameraCapabilities>;
|
||||
}
|
||||
|
||||
export function createSmartCamera(
|
||||
info: ISmartCameraDiscoveryInfo,
|
||||
retryOptions?: IRetryOptions
|
||||
): UniversalDevice {
|
||||
const device = new UniversalDevice(info.address, info.port, {
|
||||
name: info.name,
|
||||
retryOptions,
|
||||
});
|
||||
|
||||
(device as { id: string }).id = info.id;
|
||||
|
||||
const cameraFeature = new CameraFeature(device.getDeviceReference(), info.port, {
|
||||
protocol: info.protocol,
|
||||
entityId: info.entityId,
|
||||
protocolClient: info.protocolClient,
|
||||
capabilities: info.capabilities,
|
||||
});
|
||||
|
||||
device.addFeature(cameraFeature);
|
||||
return device;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Exports
|
||||
// ============================================================================
|
||||
@@ -366,4 +684,13 @@ export {
|
||||
VolumeFeature,
|
||||
PowerFeature,
|
||||
SnmpFeature,
|
||||
// Smart home features
|
||||
SwitchFeature,
|
||||
SensorFeature,
|
||||
LightFeature,
|
||||
CoverFeature,
|
||||
LockFeature,
|
||||
FanFeature,
|
||||
ClimateFeature,
|
||||
CameraFeature,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user