mirror of
https://github.com/esphome/esphome-docs.git
synced 2024-09-28 04:17:47 +02:00
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:
parent
5c3adfb24f
commit
14af7df38a
@ -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
|
||||
-------------------------------------
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user