-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(device): Change expose as to use entity config
Device node will no longer be expose to Home Assistant by default. BREAKING CHANGE: Expose as won't work until manually converted in the Node-RED UI. Device node requires minimum 2.2.1 of hass-node-red.
- Loading branch information
Showing
24 changed files
with
376 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { i18nKeyandParams } from '../../types/i18n'; | ||
import BaseError from './BaseError'; | ||
|
||
export default class NoIntegrationError extends BaseError { | ||
constructor(data?: i18nKeyandParams, statusMessage?: i18nKeyandParams) { | ||
super({ | ||
data, | ||
statusMessage, | ||
name: 'NoIntegrationError', | ||
defaultStatusMessage: 'home-assistant.error.integration_not_loaded', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import InputOutputController, { | ||
InputProperties, | ||
} from '../../common/controllers/InputOutputController'; | ||
import NoConnectionError from '../../common/errors/NoConnectionError'; | ||
import NoIntegrationError from '../../common/errors/NoIntegrationError'; | ||
import { MessageType } from '../../common/integration/Integration'; | ||
import { DeviceNode, DeviceNodeProperties } from '.'; | ||
|
||
export default class DeviceAction extends InputOutputController< | ||
DeviceNode, | ||
DeviceNodeProperties | ||
> { | ||
async onInput({ message, send, done }: InputProperties) { | ||
if (!this.homeAssistant?.isConnected) { | ||
throw new NoConnectionError(); | ||
} | ||
|
||
if (!this.homeAssistant.isIntegrationLoaded) { | ||
throw new NoIntegrationError(); | ||
} | ||
|
||
const capabilities = this.node.config.capabilities?.reduce( | ||
(acc, cap) => { | ||
acc[cap.name] = cap.value; | ||
return acc; | ||
}, | ||
{} as Record<string, unknown> | ||
); | ||
const payload = { | ||
type: MessageType.DeviceAction, | ||
action: { ...this.node.config.event, ...capabilities }, | ||
}; | ||
|
||
await this.homeAssistant.websocket.send(payload); | ||
|
||
this.setCustomOutputs(this.node.config.outputProperties, message, { | ||
config: this.node.config, | ||
data: payload, | ||
}); | ||
|
||
this.status.setSuccess( | ||
`${this.node.config.event?.domain}.${this.node.config.event?.type}` | ||
); | ||
send(message); | ||
done(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import ConfigError from '../../common/errors/ConfigError'; | ||
import BidirectionalIntegration, { | ||
IntegrationMessage, | ||
} from '../../common/integration/BidrectionalIntegration'; | ||
import { MessageType } from '../../common/integration/Integration'; | ||
import { DeviceCapabilityType, TimeUnit } from '../../const'; | ||
import { | ||
HassDeviceCapability, | ||
HassDeviceTrigger, | ||
} from '../../types/home-assistant'; | ||
import { DeviceNode } from '.'; | ||
|
||
interface DeviceIntegrationMessage extends IntegrationMessage { | ||
type: MessageType.DeviceTrigger; | ||
device_trigger: unknown; | ||
} | ||
|
||
interface TriggerData extends HassDeviceTrigger { | ||
[key: string]: unknown; | ||
} | ||
|
||
export default class DeviceIntegration extends BidirectionalIntegration<DeviceNode> { | ||
#getTriggerData() { | ||
if (!this.node.config.event) { | ||
throw new ConfigError('ha-device.error.invalid_device_config'); | ||
} | ||
|
||
const trigger: TriggerData = { ...this.node.config.event }; | ||
if (this.node.config.capabilities?.length) { | ||
this.node.config.capabilities.forEach((cap) => { | ||
trigger[cap.name] = this.#getCapabilitiesValue(cap); | ||
}); | ||
} | ||
|
||
return trigger; | ||
} | ||
|
||
#getCapabilitiesValue(cap: HassDeviceCapability) { | ||
switch (cap.type) { | ||
case DeviceCapabilityType.PositiveTimePeriod: { | ||
const unit = cap.unit || TimeUnit.Seconds; | ||
return { [unit]: cap.value }; | ||
} | ||
case DeviceCapabilityType.Float: | ||
return Number(cap.value); | ||
case DeviceCapabilityType.String: | ||
default: | ||
return cap.value; | ||
} | ||
} | ||
|
||
protected getDiscoveryPayload(): DeviceIntegrationMessage { | ||
return { | ||
type: MessageType.DeviceTrigger, | ||
device_trigger: this.#getTriggerData(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import ExposeAsMixin from '../../common/controllers/ExposeAsMixin'; | ||
import OutputController from '../../common/controllers/OutputController'; | ||
import { NodeMessage } from '../../types/nodes'; | ||
import { DeviceNode } from '.'; | ||
|
||
interface DeviceTriggerEvent { | ||
description: string; | ||
} | ||
|
||
const ExposeAsController = ExposeAsMixin(OutputController<DeviceNode>); | ||
export default class DeviceTriggerController extends ExposeAsController { | ||
public onTrigger(data: DeviceTriggerEvent) { | ||
if (!this.isEnabled) return; | ||
|
||
const message: NodeMessage = {}; | ||
|
||
this.setCustomOutputs(this.node.config.outputProperties, message, { | ||
config: this.node.config, | ||
eventData: data, | ||
triggerId: this.node.config.device, | ||
}); | ||
|
||
this.status.setSuccess(data.description); | ||
this.node.send(message); | ||
} | ||
} |
Oops, something went wrong.