2018-11-27 17:05:23 +01:00
|
|
|
Custom Switch
|
|
|
|
=============
|
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
.. seo::
|
|
|
|
:description: Instructions for setting up Custom C++ switches with ESPHome.
|
|
|
|
:image: language-cpp.svg
|
|
|
|
:keywords: C++, Custom
|
|
|
|
|
2023-11-01 06:47:57 +01:00
|
|
|
.. warning::
|
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
Custom components are deprecated, not recommended for new configurations and will be removed from ESPHome in a
|
|
|
|
future release. Please look at creating a real ESPHome component and "importing" it into your configuration with
|
|
|
|
:doc:`/components/external_components`.
|
|
|
|
|
|
|
|
You can find some basic documentation on creating your own components at :ref:`contributing_to_esphome`.
|
|
|
|
|
|
|
|
.. warning::
|
2023-11-01 06:47:57 +01:00
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
While we try to keep the ESPHome YAML configuration options as stable as possible, the ESPHome API is less
|
|
|
|
stable. If something in the APIs needs to be changed in order for something else to work, we will do so.
|
2023-11-01 06:47:57 +01:00
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
This component can be used to create custom switches in ESPHome using the C++ (Arduino) API.
|
2018-11-27 17:05:23 +01:00
|
|
|
|
2024-05-07 07:16:56 +02:00
|
|
|
Please first read :doc:`/components/sensor/custom` guide, the same principles apply here.
|
2018-11-27 17:05:23 +01:00
|
|
|
|
|
|
|
The example below is an example of a custom switch; this custom switch is essentially the
|
|
|
|
same as the gpio switch implementation.
|
|
|
|
|
|
|
|
.. code-block:: cpp
|
|
|
|
|
2019-02-16 23:25:23 +01:00
|
|
|
#include "esphome.h"
|
2018-11-27 17:05:23 +01:00
|
|
|
|
2019-05-12 22:44:59 +02:00
|
|
|
class MyCustomSwitch : public Component, public Switch {
|
2018-11-27 17:05:23 +01:00
|
|
|
public:
|
|
|
|
void setup() override {
|
|
|
|
// This will be called by App.setup()
|
|
|
|
pinMode(5, INPUT);
|
|
|
|
}
|
|
|
|
void write_state(bool state) override {
|
|
|
|
// This will be called every time the user requests a state change.
|
|
|
|
|
|
|
|
digitalWrite(5, state);
|
|
|
|
|
|
|
|
// Acknowledge new state by publishing it
|
|
|
|
publish_state(state);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(Store this file in your configuration directory, for example ``my_switch.h``)
|
|
|
|
|
|
|
|
And in YAML:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
# Example configuration entry
|
2019-02-16 23:25:23 +01:00
|
|
|
esphome:
|
2018-11-27 17:05:23 +01:00
|
|
|
includes:
|
|
|
|
- my_switch.h
|
|
|
|
|
|
|
|
switch:
|
|
|
|
- platform: custom
|
|
|
|
lambda: |-
|
|
|
|
auto my_custom_switch = new MyCustomSwitch();
|
|
|
|
App.register_component(my_custom_switch);
|
|
|
|
return {my_custom_switch};
|
|
|
|
|
|
|
|
switches:
|
|
|
|
name: "My Custom Switches"
|
2023-03-07 09:20:08 +01:00
|
|
|
id: my_custom_switch
|
2018-11-27 17:05:23 +01:00
|
|
|
|
|
|
|
Configuration variables:
|
|
|
|
|
|
|
|
- **lambda** (**Required**, :ref:`lambda <config-lambda>`): The lambda to run for instantiating the
|
|
|
|
switch(es).
|
|
|
|
- **switches** (**Required**, list): A list of switches to initialize. The length here
|
|
|
|
must equal the number of items in the ``return`` statement of the ``lambda``.
|
|
|
|
|
2019-02-17 12:28:17 +01:00
|
|
|
- All options from :ref:`Switch <config-switch>`.
|
2018-11-27 17:05:23 +01:00
|
|
|
|
2019-05-12 22:44:59 +02:00
|
|
|
See :apiclass:`Switch <switch_::Switch>`
|
2018-11-27 17:05:23 +01:00
|
|
|
|
|
|
|
See Also
|
|
|
|
--------
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|