-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
389 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
::: warning | ||
_Needs [Custom Integration](https://github.com/zachowj/hass-node-red) installed | ||
in Home Assistant for this node to function_ | ||
::: | ||
|
||
# Text | ||
|
||
Creates a text entity in Home Assistant which can be manipulated from this node. | ||
|
||
## Configuration | ||
|
||
### State <Badge text="required"/> | ||
|
||
- Type: `text` | ||
|
||
The state of the entity should be updated to | ||
|
||
## Inputs | ||
|
||
properties of `msg.payload` | ||
|
||
### state | ||
|
||
- Type: `text` | ||
|
||
The state of the text entity should be updated to | ||
|
||
## Outputs | ||
|
||
Value types: | ||
|
||
- `entity state`: entity state of the text entity | ||
- `config`: config properties of the node |
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,7 @@ | ||
{ | ||
"ha-text": { | ||
"label": { | ||
"text": "Text" | ||
} | ||
} | ||
} |
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,19 @@ | ||
export default [ | ||
{ | ||
id: 'mode', | ||
type: 'select', | ||
values: ['text', 'password'], | ||
}, | ||
{ | ||
id: 'min_length', | ||
type: 'number', | ||
}, | ||
{ | ||
id: 'max_length', | ||
type: 'number', | ||
}, | ||
{ | ||
id: 'pattern', | ||
type: 'string', | ||
}, | ||
]; |
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,92 @@ | ||
import InputOutputController, { | ||
InputProperties, | ||
} from '../../common/controllers/InputOutputController'; | ||
import InputError from '../../common/errors/InputError'; | ||
import NoConnectionError from '../../common/errors/NoConnectionError'; | ||
import BidirectionalEntityIntegration, { | ||
StateChangePayload, | ||
} from '../../common/integration/BidirectionalEntityIntegration'; | ||
import { EntityBaseNodeProperties, OutputProperty } from '../../types/nodes'; | ||
import { TextNode } from '.'; | ||
|
||
export interface TextNodeProperties extends EntityBaseNodeProperties { | ||
state: string; | ||
stateType: string; | ||
outputProperties: OutputProperty[]; | ||
} | ||
|
||
export default class TextController extends InputOutputController< | ||
TextNode, | ||
TextNodeProperties | ||
> { | ||
protected integration?: BidirectionalEntityIntegration; | ||
|
||
protected async onInput({ | ||
done, | ||
message, | ||
parsedMessage, | ||
send, | ||
}: InputProperties) { | ||
if (!this.integration?.isConnected) { | ||
throw new NoConnectionError(); | ||
} | ||
if (!this.integration?.isIntegrationLoaded) { | ||
throw new InputError( | ||
'home-assistant.error.integration_not_loaded', | ||
'home-assistant.error.error' | ||
); | ||
} | ||
|
||
const state = this.typedInputService.getValue( | ||
parsedMessage.state.value, | ||
parsedMessage.stateType.value, | ||
{ | ||
message, | ||
} | ||
); | ||
|
||
if (this.#validState(state) === false) { | ||
throw new InputError( | ||
'home-assistant.error.pattern_not_matched', | ||
'home-assistant.error.error' | ||
); | ||
} | ||
|
||
this.state?.setLastPayload({ state, attributes: {} }); | ||
await this.integration.updateHomeAssistant(state); | ||
|
||
this.status.setSuccess(state.toString()); | ||
this.setCustomOutputs(this.node.config.outputProperties, message, { | ||
config: this.node.config, | ||
entityState: state, | ||
}); | ||
|
||
send(message); | ||
done(); | ||
} | ||
|
||
public async onValueChange(payload: StateChangePayload) { | ||
if (typeof payload.state !== 'string') return; | ||
|
||
if (this.#validState(payload.state)) { | ||
this.state?.setLastPayload({ | ||
state: payload.state, | ||
attributes: {}, | ||
}); | ||
} | ||
} | ||
|
||
#validState(text: string): boolean { | ||
const haConfig = | ||
this.integration?.getEntityConfigNode().config.haConfig; | ||
const pattern = haConfig?.find((item) => item.property === 'pattern') | ||
?.value as string; | ||
|
||
if (pattern) { | ||
const regex = new RegExp(pattern); | ||
return regex.test(text); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,25 @@ | ||
<input type="hidden" id="node-input-version" /> | ||
|
||
<div class="form-row"> | ||
<label for="node-input-name"> | ||
<span data-i18n="home-assistant.label.name"></span> | ||
</label> | ||
<input type="text" id="node-input-name" placeholder="Name" /> | ||
</div> | ||
|
||
<div class="form-row"> | ||
<label for="node-input-entityConfig"> | ||
<span data-i18n="home-assistant.label.entity_config"></span> | ||
</label> | ||
<input type="text" id="node-input-entityConfig" /> | ||
</div> | ||
|
||
<div class="form-row"> | ||
<label for="node-input-text"> | ||
<span data-i18n="ha-text.label.text"></span> | ||
</label> | ||
<input type="text" id="node-input-state" style="width: 70%" /> | ||
<input type="hidden" id="node-input-stateType" /> | ||
</div> | ||
|
||
<div class="form-row"><ol id="custom-outputs"></ol></div> |
Oops, something went wrong.