EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
Public Member Functions | Static Public Member Functions | List of all members
HASwitch Class Reference

#include <haSwitch.h>

Inheritance diagram for HASwitch:
HAEntity

Public Member Functions

 HASwitch ()
 Switch constructor. More...
 
void setPayloadOn (const char *payload)
 The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_on. More...
 
void setPayloadOff (const char *payload)
 The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_off. More...
 
void setPayloadOn (int payload)
 The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_on. More...
 
void setPayloadOff (int payload)
 The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_off. More...
 
void setStateOn (const char *payload)
 The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on https://www.home-assistant.io/integrations/switch.mqtt/#state_on. More...
 
void setStateOff (const char *payload)
 The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off https://www.home-assistant.io/integrations/switch.mqtt/#state_off. More...
 
void setStateOn (int payload)
 The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on https://www.home-assistant.io/integrations/switch.mqtt/#state_on. More...
 
void setStateOff (int payload)
 The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off https://www.home-assistant.io/integrations/switch.mqtt/#state_off. More...
 
void setValueField (const char *payload)
 Defines a json key to extract device’s state from the state_topic. To determine the switches’s state result of this template will be compared to state_on and state_off https://www.home-assistant.io/integrations/switch.mqtt/#value_template. More...
 
- Public Member Functions inherited from HAEntity
size_t getAnounceMessage (int bufferlen, uint8_t *buffer)
 Gets entity anounce message to be sent over EnigmaIOT message. More...
 
void setNameSufix (const char *payload)
 Sets name suffix. Used for multi entity nodes. More...
 
void allowSendAttributes ()
 Enables registering entity attributes as a json object. More...
 
size_t measureMessage ()
 Gets needed buffer size for discovery message. More...
 

Static Public Member Functions

static size_t getDiscoveryJson (char *buffer, size_t buflen, const char *nodeName, const char *networkName, DynamicJsonDocument *inputJSON)
 Allows Gateway to get Home Assistant discovery message using Switch template. More...
 
- Static Public Member Functions inherited from HAEntity
static String deviceTypeStr (haDeviceType_t entityType)
 Gets entity type string from haDeviceType_t value https://www.home-assistant.io/docs/mqtt/discovery/. More...
 
static String getDiscoveryTopic (const char *hassPrefix, const char *nodeName, haDeviceType_t entityType, const char *nameSuffix=NULL)
 Allows Gateway to get discovery message MQTT topic. More...
 

Additional Inherited Members

- Protected Member Functions inherited from HAEntity
 HAEntity ()
 Default constructor. Needed for inheritance. More...
 
- Protected Attributes inherited from HAEntity
size_t capacity
 JSON object memory reservation length. More...
 
haDeviceType_t deviceType = UNDEFINED
 HomeAssistant entity device type. More...
 
DynamicJsonDocument * entityConfig
 JSON object to be sent to gateway. More...
 

Detailed Description

The mqtt switch platform lets you control your MQTT enabled switches. In an ideal scenario, the MQTT device will have a state_topic to publish state changes. If these messages are published with a RETAIN flag, the MQTT switch will receive an instant state update after subscription, and will start with the correct state. Otherwise, the initial state of the switch will be false / off.

When a state_topic is not available, the switch will work in optimistic mode. In this mode, the switch will immediately change state after every command. Otherwise, the switch will wait for state confirmation from the device (message from state_topic).

Optimistic mode can be forced, even if the state_topic is available. Try to enable it, if experiencing incorrect switch operation.

https://www.home-assistant.io/integrations/switch.mqtt/

An example of switch discovery message may be like this:

Topic: homeassistant/switch/lights_switch/config

Payload:

{ "name":"light_switch", // string (optional) The name of the device "unique_id":"light_switch", // string (optional) An ID that uniquely identifies this switch device. If two switches have the same unique ID, // Home Assistant will raise an exception "command_topic":"EnigmaIOT/light/set/data", // string (optional) The MQTT topic to publish commands to change the switch state "payload_off":"{\"cmd":"swi","swi":0}", // string (optional, default: OFF) The payload that represents off state. If specified, will be used for both // comparing to the value in the state_topic (see value_template and state_off for details) and sending as off command to the command_topic "payload_on":"{\"cmd":"swi","swi":1}", // string (optional, default: ON) The payload that represents on state. If specified, will be used for both comparing to the value in the // state_topic (see value_template and state_on for details) and sending as on command to the command_topic "state_topic":"EnigmaIOT/light/data", // string (optional) The MQTT topic subscribed to receive state updates "state_off":0, // string (optional) The payload that represents the off state. Used when value that represents off state in the state_topic is different // from value that should be sent to the command_topic to turn the device off. Default: payload_off if defined, else OFF "state_on":1, // string (optional) The payload that represents the on state. Used when value that represents on state in the state_topic is different // from value that should be sent to the command_topic to turn the device on. Default: payload_on if defined, else ON "value_template":"{{value_json.swi}}" // string (optional) Defines a template to extract device’s state from the state_topic. To determine the switches’s state result of this // template will be compared to state_on and state_off "json_attributes_template":"{{value_json | tojson}}", // template (optional) Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. // Usage example can be found in MQTT sensor documentation "json_attributes_topic":"EnigmaIOT/light/data", // string (optional) The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. // Usage example can be found in MQTT sensor documentation }

Template message for switch is this

Topic: homeassistant/switch/<node_name>_<name_suffix>/config

Payload

{
"name":"<node_name>_<name_suffix>",
"unique_id":"<node_name>_<name_suffix>",
"command_topic":"<network_name>/<node_name>/set/data"
"payload_off":"<cmd_payload_on>",
"payload_on":"<cmd_payload_off>"
"state_topic":"<network_name>/<node_name>/data",
"state_off":"<state_off>",
"state_on":"<state_on>",
"value_template":"{{value_json.<value_field>}}"
"json_attributes_template":"{{value_json | tojson}}",
"json_attributes_topic":"<network_name>/<node_name>/data",
}

Message to gateway is like following

{
"nmsfx":<name_suffix>,
"pl_on":<cmd_payload_on>,
"pl_off":<cmd_payload_off>,
"stat_on":<state_on>,
"stat_off":<state_off>,
"val":<value_field>
}

Definition at line 94 of file haSwitch.h.

Constructor & Destructor Documentation

◆ HASwitch()

HASwitch::HASwitch ( )
inline

Switch constructor.

Definition at line 100 of file haSwitch.h.

Member Function Documentation

◆ getDiscoveryJson()

size_t HASwitch::getDiscoveryJson ( char *  buffer,
size_t  buflen,
const char *  nodeName,
const char *  networkName,
DynamicJsonDocument *  inputJSON 
)
static

Allows Gateway to get Home Assistant discovery message using Switch template.

Parameters
bufferBuffer to hold message string
buflenBuffer size
nodeNameOriginating node name
networkNameEnigmaIOT network name
inputJSONJSON object sent by node with needed data to fill template in
Returns
Discovery message payload

msgPack || !len

Definition at line 85 of file haSwitch.cpp.

◆ setPayloadOff() [1/2]

void HASwitch::setPayloadOff ( const char *  payload)

The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_off.

Parameters
payloadON state string

Definition at line 20 of file haSwitch.cpp.

◆ setPayloadOff() [2/2]

void HASwitch::setPayloadOff ( int  payload)

The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_off.

Parameters
payloadON state string

Definition at line 30 of file haSwitch.cpp.

◆ setPayloadOn() [1/2]

void HASwitch::setPayloadOn ( const char *  payload)

The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_on.

Parameters
payloadON state string

Definition at line 14 of file haSwitch.cpp.

◆ setPayloadOn() [2/2]

void HASwitch::setPayloadOn ( int  payload)

The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic https://www.home-assistant.io/integrations/switch.mqtt/#payload_on.

Parameters
payloadON state integer

Definition at line 26 of file haSwitch.cpp.

◆ setStateOff() [1/2]

void HASwitch::setStateOff ( const char *  payload)

The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off https://www.home-assistant.io/integrations/switch.mqtt/#state_off.

Parameters
payloadON state integer

Definition at line 40 of file haSwitch.cpp.

◆ setStateOff() [2/2]

void HASwitch::setStateOff ( int  payload)

The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off https://www.home-assistant.io/integrations/switch.mqtt/#state_off.

Parameters
payloadON state integer

Definition at line 50 of file haSwitch.cpp.

◆ setStateOn() [1/2]

void HASwitch::setStateOn ( const char *  payload)

The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on https://www.home-assistant.io/integrations/switch.mqtt/#state_on.

Parameters
payloadON state integer

Definition at line 34 of file haSwitch.cpp.

◆ setStateOn() [2/2]

void HASwitch::setStateOn ( int  payload)

The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on https://www.home-assistant.io/integrations/switch.mqtt/#state_on.

Parameters
payloadON state integer

Definition at line 46 of file haSwitch.cpp.

◆ setValueField()

void HASwitch::setValueField ( const char *  payload)

Defines a json key to extract device’s state from the state_topic. To determine the switches’s state result of this template will be compared to state_on and state_off https://www.home-assistant.io/integrations/switch.mqtt/#value_template.

Parameters
payloadON state value

Definition at line 54 of file haSwitch.cpp.


The documentation for this class was generated from the following files: