Custom Component - Hasatio/esphome GitHub Wiki
First there should be a yaml file based on the device.
For this, open a notepad, copy the text in the example below and edit it according to your needs, go to the "Save as" option from the "File" tab, add ".yaml" to the "File name" section, in addition to any name, and select the "save type" section as "all files". Save to where you want to save by selecting. Or it can be created as described in the "New Yaml" section here.
substitutions:
devicename: "devkit-s"
esphome:
name: $devicename
friendly_name: $devicename
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
wifi:
ssid: wifi_ssid
password: wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: $devicename
password: $devicename
reboot_timeout: 0s
fast_connect: true
# power_save_mode: none # If bluetooth is to be used, "none" should not be set.
captive_portal:
ESPHome Homeassistant" or "ESPHome Cli" be used to upload programs on the device.
"Create/Edit Component Files
There are two ways to generate esphome custom component files:
-
Creating the custom component file
-
If the path doesn't exist, create an "esphome/components" path on Github. In "esphome/components" create the component file (mycomponent) and inside it the required file "init.py". For this, go to "esphome/components" from github and press the "create new file" button from the "add file" section at the top right and write it as "mycomponent/init.py". "mycomponent" can be anything.
-
Click "commit changes" and click "commit changes" again.
-
Enter the "mycomponent" file and create the "component.h" and "component.cpp" files here in the same way we created the previous "init.py" file. The "component" can be anything.
-
It should look like this at the end.
-
-
Using repository or clone files of existing custom component from github
- Files can be accessed ready-made from "esphome/components" by clicking "Code" on the top right and selecting "Clone", "Open with GitHub Desktop" or "Download ZIP"
- Files can be accessed ready-made from "esphome/components" by clicking "Code" on the top right and selecting "Clone", "Open with GitHub Desktop" or "Download ZIP"
Start by editing "component.h" file:
- Include "esphome.h" as the esphome library will be used
#include "esphome.h"
- For component usage, esphome and component(mycomponent) namespaces are needed in it, create them. "mycomponent" optional text can be written.
namespace esphome {
namespace mycomponent {
- Define a class with polling component definition from which base and custom functions will run
class myclass : public PollingComponent
{
public:
- Edit the function (get_setup_priority) to set the running priority in the esphome already, and set it as you wish. It can be used optionally from the variables specified in the "setup_priority" function in the library in the link, set it as the highest (PROCESSOR) degree in the code.
float get_setup_priority() const override { return esphome::setup_priority::PROCESSOR; }
- Create the functions needed and basic function inside the class
void setup() override;
void loop() override;
void update() override;
- Close the class and namespaces opened
}; //class myclass
} //namespace mycomponent
} //namespace esphome
Edit "component.cpp" file:
- Include "component.h" as the library will be used
#include "component.h"
- Write the namespaces defined in "component.h"
```c++
namespace esphome {
namespace mycomponent {
- Write the functions defined in "component.h", for now set it to empty.
void myclass::setup()
{
}
void myclass::loop()
{
}
void myclass::update()
{
}
- Close the namespaces opened
} //namespace mycomponent
} //namespace esphome
Now edit "init.py" file for the component definition:
- Import as "cg" as functions and definitions from "esphome.codegen" library will be used
import esphome.codegen as cg
- Import as "cv" as functions and definitions from "esphome.config_validation" library will be used
import esphome.config_validation as cv
- Import "CONF_ID" variable as component ID from "esphome.const" library will be used
from esphome.const import (CONF_ID)
- Using the function (esphome_ns) available in esphome, define the variable "component_ns" that represents the component name to be used in yaml and the namespace name defined in the library
component_ns = cg.esphome_ns.namespace("mycomponent")
- Using the "component_ns" created above, define the variable "Myclass" that specifies the component class (myclass) defined in the library
Myclass = component_ns.class_("myclass", cg.Component)
- Set what will be contained in the component and the structure. Since creating an empty component for now, only add the id information of the "mycomponent" defined above.
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(Myclass),
}
)
.extend(cv.COMPONENT_SCHEMA)
)
- Create a component main function
async def to_code(config):
- Using the function (new_Pvariable) in esphome, assign the component id from esphome to the variable "var"
var = cg.new_Pvariable(config[CONF_ID])
- Create custom component with "register_component" function in esphome using variable(var) created above
await cg.register_component(var, config)
The latest file content should look like this:
- component.h:
#include "esphome.h"
namespace esphome {
namespace mycomponent {
class myclass : public PollingComponent
{
public:
float get_setup_priority() const override { return esphome::setup_priority::PROCESSOR; }
void setup() override;
void loop() override;
void update() override;
}
} //namespace mycomponent
} //namespace esphome
- component.cpp:
```c++
#include "component.h"
namespace esphome {
namespace mycomponent {
void myclass::setup()
{
}
void myclass::loop()
{
}
void myclass::update()
{
}
} //namespace mycomponent
} //namespace esphome
- init.py:
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import (CONF_ID)
component_ns = cg.esphome_ns.namespace("mycomponent")
Myclass = component_ns.class_("myclass", cg.Component)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(Myclass),
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
By adding the following to the yaml file, the component will be added
external_components:
- source: github://Hasatio/esphome@main
components: [mycomponent]
refresh: always
mycomponent:
Pressure (ADS1115)
Edit "component.h" file:
#include "esphome.h"
- Include the sensor library to use
#include <Adafruit_ADS1X15.h>
- Add the required library in the esphome as we will output the log
#include "esphome/core/log.h"
Edit "component.cpp" file:
#include "component.h"
namespace esphome {
namespace mycomponent {
- It was construct with enough new names to be used. Different naming can be made upon request.(ads1, ads2 ...)
Adafruit_ADS1115 ads1; //construction of first ads1115
Adafruit_ADS1115 ads2; //construction of secon ads1115
Adafruit_ADS1115 ads3; //construction of third ads1115
Adafruit_ADS1115 ads4; //construction of fourth ads1115
- It is defined for the pins and frequency that the I2c transmission line uses
#define SDA 21 //sda pin number 21
#define SCL 22 //scl pin number 22
#define freq 800000 //frequency 800khz
- The i2c connection address used is defined
#define address1 0x48 //first ads1115 address
#define address2 0x49 //second ads1115 address
#define address3 0x4a //third ads1115 address
#define address4 0x4b //fourth ads1115 address
- Define variables to use
uint16_t adc[16];
float volts[16];
String data = "";
void myclass::setup()
{
- Start the i2c line using the "begin" function in the esphome library
Wire.begin(SDA,SCL,freq);
- Make the setting and start the line using the "begin" function in the sensor library
ads1.begin(address1,&Wire);
ads2.begin(address2,&Wire);
ads3.begin(address3,&Wire);
ads4.begin(address4,&Wire);
- Set the gain using the "setGain" function in the sensor library
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
//
// ADS1015 ADS1115
// ------- -------
// (GAIN_TWOTHIRDS) // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
// (GAIN_ONE) // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// (GAIN_TWO) // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// (GAIN_FOUR) // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// (GAIN_EIGHT) // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
// (GAIN_SIXTEEN) // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
ads1.setGain(GAIN_ONE);
ads2.setGain(GAIN_ONE);
ads3.setGain(GAIN_ONE);
ads4.setGain(GAIN_ONE);
- Set the sampling per second using the "setDataRate" function in the sensor library
// RATE_ADS1115_8SPS (0x0000) ///< 8 samples per second
// RATE_ADS1115_16SPS (0x0020) ///< 16 samples per second
// RATE_ADS1115_32SPS (0x0040) ///< 32 samples per second
// RATE_ADS1115_64SPS (0x0060) ///< 64 samples per second
// RATE_ADS1115_128SPS (0x0080) ///< 128 samples per second (default)
// RATE_ADS1115_250SPS (0x00A0) ///< 250 samples per second
// RATE_ADS1115_475SPS (0x00C0) ///< 475 samples per second
// RATE_ADS1115_860SPS (0x00E0) ///< 860 samples per second
ads1.setDataRate(RATE_ADS1115_860SPS);
ads2.setDataRate(RATE_ADS1115_860SPS);
ads3.setDataRate(RATE_ADS1115_860SPS);
ads4.setDataRate(RATE_ADS1115_860SPS);
} //setup
void myclass::loop()
{
- For each ads1115 and each channel, get the data with the "readADC_SingleEnded" function in the library in the sensor and assign them to the required variable by performing the voltage conversion using the "computeVolts" function in the library.
for(int i=0;i<4;i++) //first ads1115
{
adc[i] = ads1.readADC_SingleEnded(i%4);
volts[i] = ads1.computeVolts(adc[i]);
data = data + String(volts[i]) + ",";
}
for(int i=4;i<8;i++) //second ads1115
{
adc[i] = ads2.readADC_SingleEnded(i%4);
volts[i] = ads2.computeVolts(adc[i]);
data = data + String(volts[i]) + ",";
}
for(int i=8;i<12;i++) //third ads1115
{
adc[i] = ads3.readADC_SingleEnded(i%4);
volts[i] = ads3.computeVolts(adc[i]);
data = data + String(volts[i]) + ",";
}
for(int i=12;i<16;i++) //fourth ads1115
{
adc[i] = ads4.readADC_SingleEnded(i%4);
volts[i] = ads4.computeVolts(adc[i]);
data = data + String(volts[i]) + ",";
}
- Send the data of the variable as the log output
ESP_LOGD("ads1115", "data: %s",data); //log data of data
- Reset the "data" so that the new values are correct in the next loop
data = "";
} //loop
void myclass::update()
{
}
} //namespace mycomponent
} //namespace esphome
Edit "init.py" file:
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
- To download the library needed by the sensor, write the required library in the "add_library" function in the esphome. The latest version is installed by leaving the version section blank.
cg.add_library(
"Adafruit ADS1X15", #library name
"" #version
)
Acceleration (ADXL345)
Edit "component.h" file:
#include "esphome.h"
- Include the sensor library to use
#include <Adafruit_ADXL345_U.h>
- Add the required library in the esphome as we will output the log
#include "esphome/core/log.h"
Edit "component.cpp" file:
#include "component.h"
namespace esphome {
namespace mycomponent {
- Construct in new name. Different naming can be done upon request.
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
- It is defined for the pins and frequency that the I2c transmission line uses
#define SDA 21 //sda pin number 21
#define SCL 22 //scl pin number 22
#define freq 800000 //frequency 800khz
- Define variables to use
float x, y, z;
double adxlmultiplier;
void myclass::setup()
{
- Start the i2c line using the "begin" function in the esphome library
Wire.begin(SDA,SCL,freq);
- Make the setting and start the line using the "begin" function in the sensor library
accel.begin(ADXL345_DEFAULT_ADDRESS);
- Set the range using the "setRange" function in the sensor library
/* Set the range to whatever is appropriate for your project */
// ADXL345_RANGE_16_G ///< +/- 16g
// ADXL345_RANGE_8_G ///< +/- 8g
// ADXL345_RANGE_4_G ///< +/- 4g
// ADXL345_RANGE_2_G ///< +/- 2g (default value)
accel.setRange(ADXL345_RANGE_2_G);
- Set the data rate using the "setDataRate" function in the sensor library
// ADXL345_DATARATE_3200_HZ ///< 1600Hz Bandwidth 140�A IDD
// ADXL345_DATARATE_1600_HZ ///< 800Hz Bandwidth 90�A IDD
// ADXL345_DATARATE_800_HZ ///< 400Hz Bandwidth 140�A IDD
// ADXL345_DATARATE_400_HZ ///< 200Hz Bandwidth 140�A IDD
// ADXL345_DATARATE_200_HZ ///< 100Hz Bandwidth 140�A IDD
// ADXL345_DATARATE_100_HZ ///< 50Hz Bandwidth 140�A IDD
// ADXL345_DATARATE_50_HZ ///< 25Hz Bandwidth 90�A IDD
// ADXL345_DATARATE_25_HZ ///< 12.5Hz Bandwidth 60�A IDD
// ADXL345_DATARATE_12_5_HZ ///< 6.25Hz Bandwidth 50�A IDD
// ADXL345_DATARATE_6_25HZ ///< 3.13Hz Bandwidth 45�A IDD
// ADXL345_DATARATE_3_13_HZ ///< 1.56Hz Bandwidth 40�A IDD
// ADXL345_DATARATE_1_56_HZ ///< 0.78Hz Bandwidth 34�A IDD
// ADXL345_DATARATE_0_78_HZ ///< 0.39Hz Bandwidth 23�A IDD
// ADXL345_DATARATE_0_39_HZ ///< 0.20Hz Bandwidth 23�A IDD
// ADXL345_DATARATE_0_20_HZ ///< 0.10Hz Bandwidth 23�A IDD
// ADXL345_DATARATE_0_10_HZ ///< 0.05Hz Bandwidth 23�A IDD (default value)
accel.setDataRate(ADXL345_DATARATE_100_HZ);
} //setup
void myclass::loop()
{
- Get data and assign to variables using the functions(getX, getY, getZ) in the sensor library. "ADXL345_MG2G_MULTIPLIER" and SENSORS_GRAVITY_STANDARD are constant variables defined in the library and can be changed as desired.
adxlmultiplier = ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD; //assigning the multiplier value to the voltage variable
x = accel.getX() * adxlmultiplier; //assigning value to x variable
y = accel.getY() * adxlmultiplier; //assigning value to y variable
z = accel.getZ() * adxlmultiplier; //assigning value to z variable
- Send the data of the variable as the log output
ESP_LOGD("adxl345", "x: %f",x); //log data of x
ESP_LOGD("adxl345", "y: %f",y); //log data of y
ESP_LOGD("adxl345", "z: %f",z); //log data of z
} //loop
void myclass::update()
{
}
} //namespace mycomponent
} //namespace esphome
Edit "init.py" file:
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
- To download the library needed by the sensor, write the required library in the "add_library" function in the esphome. The latest version is installed by leaving the version section blank.
cg.add_library(
"Adafruit ADXL345", #library name
"" #version
)
Battery (MAX17048)
Edit "component.h" file:
#include "esphome.h"
- Include the sensor library to use
#include <Adafruit_MAX1704X.h>
- Add the required library in the esphome as we will output the log
#include "esphome/core/log.h"
Edit "component.cpp" file:
#include "component.h"
namespace esphome {
namespace mycomponent {
- Construct in new name. Different naming can be done upon request.
Adafruit_MAX17048 maxlipo;
- It is defined for the pins and frequency that the I2c transmission line uses
#define SDA 21 //sda pin number 21
#define SCL 22 //scl pin number 22
#define freq 800000 //frequency 800khz
- Define variables to use
float voltage, percentage;
void myclass::setup()
{
- Start the i2c line using the "begin" function in the esphome library
Wire.begin(SDA,SCL,freq);
- Make the setting and start the line using the "begin" function in the sensor library
maxlipo.begin(&Wire);
} //setup
void myclass::loop()
{
- Get data and assign to variables using the functions(cellVoltage, cellPercent) in the sensor library
voltage = maxlipo.cellVoltage(); //assigning value to voltage variable
percentage = maxlipo.cellPercent(); //assigning value to percentage variable
- Send the data of the variable as the log output
ESP_LOGD("max17048", "voltage: %f",voltage; //log data of voltage
ESP_LOGD("max17048", "percentage: %f",percentage); //log data of percentage
} //loop
void myclass::update()
{
}
} //namespace mycomponent
} //namespace esphome
Edit "init.py" file:
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
- To download the library needed by the sensor, write the required library in the "add_library" function in the esphome. The latest version is installed by leaving the version section blank.
cg.add_library(
"Adafruit MAX1704X", #library name
"" #version
)
Internal Temperature
Edit "component.h" file:
#include "esphome.h"
- Add the component library in esphome as the component to use is in esphome
#include "esphome/core/component.h"
- Add the required library in the esphome as we will output the log
#include "esphome/core/log.h"
Edit "component.cpp" file:
#include "component.h"
- Get function definition
extern "C"
{
uint8_t temprature_sens_read();
}
namespace esphome {
namespace mycomponent {
- Define variables to use
float temperature = NAN;
void myclass::loop()
{
- Assign the temperature data from the function to the variable named "raw"
uint8_t raw = temprature_sens_read();
- Convert from fahrenheit to celsius using the "raw" value and make the necessary conversion, assign it to the "temperature" variable
temperature = (raw - 32) / 1.8f;
- Send the data of the variable as the log output.
ESP_LOGD("temp", "Temperature: %.1f",temperature); //log data of temperature
} //loop
void myclass::update()
{
}
} //namespace mycomponent
} //namespace esphome
Bluetooth
Edit "component.h" file:
#include "esphome.h"
- Add bluetooth Arduino library as component will be used
#include <BluetoothSerial.h>
Edit "component.cpp" file:
#include "component.h"
namespace esphome {
namespace mycomponent {
- Set the variable containing the bluetooth device name to "ESP32"
String btname = "ESP32";
void myclass::setup()
{
- Make the setting and start the line using the "begin" function in the sensor library
SerialBT.begin(btname);
} //setup
void myclass::loop()
{
- Combine pressure, acceleration, battery and temperature datas
data = data + String(x) + "," + String(y) + "," + String(z) + "," + String(voltage) + "," + String(percentage) + "," + String(temperature);
- Send "data" via bluetooth using the "println" function in the sensor library
SerialBT.println(data);
data = "";
} //loop
void myclass::update()
{
}
} //namespace mycomponent
} //namespace esphome
Edit "init.py" file:
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
- To download the library needed by the sensor, write the required library in the "add_library" function in the esphome. The latest version is installed by leaving the version section blank.
cg.add_library(
"BluetoothSerial", #library name
"" #version
)
Output (This section is made for learning only, it is not used in the system)
Edit "component.h" file:
#include "esphome.h"
- Add the component library as the component to be used is from esphome
#include "esphome/core/component.h"
- Add the required library in the esphome as we will output the log
#include "esphome/core/log.h"
namespace esphome {
namespace mycomponent {
class myclass: public PollingComponent
{
public:
void setup() override;
void loop() override;
void update() override;
- Create the cutom function
void myoutput(float o);
}; //class myclass
} //namespace mycomponent
} //namespace esphome
Edit "component.cpp" file:
#include "component.h"
namespace esphome {
namespace mycomponent {
- Set what the function will do
void myclass::myoutput(float o)
{
myout = o;
}
void myclass::loop()
{
- Send the data of the variable as the log output.
ESP_LOGD("out", "Output: %.1f",myout); //log data of myout
} //loop
void myclass::update()
{
}
} //namespace mycomponent
} //namespace esphome
Edit "init.py" file:
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import (CONF_ID)
- Define the variable "output" in the component to be used on the esphome.
CONF_MY_OUTPUT = "output"
component_ns = cg.esphome_ns.namespace("mycomponent")
Myclass = component_ns.class_("myclass", cg.Component)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(Myclass),
- Define that it is contained in the component and contains data in the form of floats.
cv.Optional(CONF_MY_OUTPUT): cv.float_,
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
- Define the "output" in the component defined in esphome to the "myoutput" function and transfer the data from the esphome to the function.
if CONF_MY_OUTPUT in config:
cg.add(var.myoutput(config[CONF_MY_OUTPUT]))
Edit yaml file as below:
mycomponent:
id: "anything"
output: 1.0 #The number can be changed as desired.
Sensor (This section is made for learning only, it is not used in the system)
Edit "component.h" file:
#include "esphome.h"
- Add the sensor library as the sensor to be used is from esphome
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace mycomponent {
class myclass: public PollingComponent
{
public:
- Create the "mysensor" function for the sensor
void mysensor(sensor::Sensor *mysens)
{
mysens_ = mysens;
}
- Create the sensor's variable, protected inside the class.
protected:
sensor::Sensor *mysens_{nullptr};
}; //class myclass
} //namespace mycomponent
} //namespace esphome
Edit "component.cpp" file:
#include "component.h"
namespace esphome {
namespace mycomponent {
- Define a counter variable that counts total data
uint64_t counter = 0;
void myclass::loop()
{
- Increment the counter every cycle
counter += 1;
- Send counter information to sensor variable
if (this->mysens_ != nullptr) this->mysens_->publish_state(counter);
} //loop
void Myi2c::update()
{
}
} //namespace mycomponent
} //namespace esphome
Edit "init.py" file:
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import (CONF_ID)
- Autoload and open multi configuration for use sensor configuration(for sensor.py)
AUTO_LOAD = ["sensor"]
MULTI_CONF = True
- Make the definitions to be used for the sensor
CONF_MY_SENSOR = "sample"
UNIT_SENSOR = "data"
component_ns = cg.esphome_ns.namespace("mycomponent")
Myclass = component_ns.class_("myclass", cg.Component)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(Myclass),
}
)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
link. Similarly, make additions to the file.
Create a new file named "sensor.py" as create and edit "init.py" in theimport esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import (CONF_ID)
- Add the sensor library as the sensor to be used is from esphome
from esphome.components import sensor
- Get variables to use from file "init.py"
from . import component_ns, Myclass, CONF_MY_SENSOR, UNIT_SENSOR
- Require component identification required for sensor use.
DEPENDENCIES = ["mycomponent"]
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.use_id(Myclass),
- Set what is contained in the sensor structure. In addition to the id information, set the unit in the sensor's output information and the number of digits after the decimal point numerically.
cv.Optional(CONF_MY_SENSOR): sensor.sensor_schema(
unit_of_measurement=UNIT_SENSOR,
accuracy_decimals=0,
),
}
)
)
- Create the sensor function and associate it with the "mysensor" function defined in "component.h".
async def to_code(config):
parent = await cg.get_variable(config[CONF_ID])
if CONF_MY_SAMPLE in config:
sens = await sensor.new_sensor(config[CONF_MY_SENSOR])
cg.add(parent.mysensor(sens))
The latest file content should look like this:
sensor.py:
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (CONF_ID)
from . import component_ns, Myclass, CONF_MY_SENSOR, UNIT_SENSOR
DEPENDENCIES = ["mycomponent"]
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.use_id(Myclass),
cv.Optional(CONF_MY_SENSOR): sensor.sensor_schema(
unit_of_measurement=UNIT_SENSOR,
accuracy_decimals=0,
),
}
)
)
async def to_code(config):
parent = await cg.get_variable(config[CONF_ID])
if CONF_MY_SAMPLE in config:
sens = await sensor.new_sensor(config[CONF_MY_SENSOR])
cg.add(parent.mysensor(sens))
Edit yaml file as below:
sensor:
- platform: mycomponent
mysensor:
id: "any id"
name: "any name" # sensor output information will appear with this name