Add get_setup_priority() to Custom sensor docs (#819)

Co-authored-by: Maarten Kleijwegt <9350991+MaartenKleijwegt@users.noreply.github.com>
This commit is contained in:
Maarten 2020-12-21 00:56:35 +01:00 committed by GitHub
parent 5c3adfb24f
commit 14af7df38a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -76,7 +76,7 @@ Let's now also take a closer look at this line, which you might not be too used
.. code-block:: cpp
class MyCustomSensor : public Component, public Sensor {
class MyCustomSensor : public Component, public Sensor {
What this line is essentially saying is that we're defining our own class that's called ``MyCustomSensor``
which is also a subclass of :apiclass:`Component` and :apiclass:`Sensor <sensor::Sensor>`.
@ -132,6 +132,57 @@ Every time ``update`` is called we will now **publish** a new value to the front
The rest of ESPHome will then take care of processing this value and ultimately publishing it
to the outside world (for example using MQTT).
One last thing. Some sensors, such as the BMP180 were are going to explain later, require some other component before they can be used. Remember how we talked about the ``setup()`` method? Well just like when writing in the Arduino IDE, components need to be set up in the right order. For that ESPHome introduces another method in the :apiclass:`Component` class.
.. code-block:: cpp
float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; }
Where HARDWARE can be any of:
.. code-block:: cpp
/// For communication buses like i2c/spi
extern const float BUS;
/// For components that represent GPIO pins like PCF8573
extern const float IO;
/// For components that deal with hardware and are very important like GPIO switch
extern const float HARDWARE;
/// For components that import data from directly connected sensors like DHT.
extern const float DATA;
/// Alias for DATA (here for compatability reasons)
extern const float HARDWARE_LATE;
/// For components that use data from sensors like displays
extern const float PROCESSOR;
extern const float WIFI;
/// For components that should be initialized after WiFi is connected.
extern const float AFTER_WIFI;
/// For components that should be initialized after a data connection (API/MQTT) is connected.
extern const float AFTER_CONNECTION;
/// For components that should be initialized at the very end of the setup process.
extern const float LATE;
Now don't let the wording confuse you. The ``get_setup_priority()`` method is an override. Instead of fetching the setup priority setup for us, it instead fetches the setup priority for esphome, while being defined by us. The BMP180 would for instance need to be setup with a priority of IO or lower. A serial streaming (TCP) server would require a working WIFI setup and therefore get AFTER_WIFI.
This finalizes our example as:
.. code-block:: cpp
class MyCustomSensor : public PollingComponent, public Sensor {
public:
// constructor
MyCustomSensor() : PollingComponent(15000) {}
float get_setup_priority() const override { return esphome::setup_priority::XXXX; }
void setup() override {
// This will be called by App.setup()
}
void update() override {
// This will be called every "update_interval" milliseconds.
}
};
Step 2: Registering the custom sensor
-------------------------------------