1- # ESPHome Component Architecture
1+ # Component Architecture
22
3+ All components within ESPHome have a specific structure. This structure exists because:
34
5+ - It allows the Python parts of ESPHome to:
6+ - Easily determine which parts of the C++ codebase are required to complete a build.
7+ - Understand how to interact with the component/platform so it can be configured correctly.
8+ - It makes understanding and maintaining the codebase easier.
49
5- ## Directory Structure
10+ ## Directory structure
611
712```
813esphome
@@ -13,8 +18,8 @@ esphome
1318│ │ ├─ example_component.cpp
1419```
1520
16- This is the most basic component directory structure where the component would be used at the
17- top-level of the YAML configuration.
21+ This is the most basic component directory structure where the component would be used at the top-level of the YAML
22+ configuration.
1823
1924``` yaml
2025example_component :
@@ -27,9 +32,9 @@ example_component:
2732
2833At the heart of every ESPHome component is the ` CONFIG_SCHEMA` and the `to_code` method.
2934
30- The `CONFIG_SCHEMA` is based on and extends [Voluptuous](https://github.com/alecthomas/voluptuous), which is a
31- data validation library. This allows the YAML to be parsed and converted to a Python object and performs
32- strong validation against the data types to ensure they match.
35+ The `CONFIG_SCHEMA` is based on and extends [Voluptuous](https://github.com/alecthomas/voluptuous), which is a data
36+ validation library. This allows the YAML to be parsed and converted to a Python object and performs strong validation
37+ against the data types to ensure they match.
3338
3439` ` ` python
3540import esphome.config_validation as cv
@@ -73,26 +78,25 @@ import esphome.config_validation as cv
7378import esphome.codegen as cg
7479` ` `
7580
76- ` config_validation` is a module that contains all the common validation functions that are used to validate the configuration.
77- Components may contain their own validations as well and this is very extensible.
78- ` codegen` is a module that contains all the code generation functions that are used to generate the C++ code that is placed
79- into `main.cpp`.
81+ ` config_validation` is a module that contains all the common validation method that are used to validate the
82+ configuration. Components may contain their own validations as well and this is very extensible. `codegen` is a module
83+ that contains all the code generation method that are used to generate the C++ code that is placed into `main.cpp`.
8084
8185
8286` ` ` python
8387example_component_ns = cg.esphome_ns.namespace("example_component")
8488` ` `
8589
86- This is the c ++ namespace inside the `esphome` namespace. It is required here so that the codegen knows the exact
90+ This is the C ++ namespace inside the `esphome` namespace. It is required here so that the codegen knows the exact
8791namespace of the class that is being created. The namespace **must** match the name of the component.
8892
8993
9094` ` ` python
9195ExampleComponent = example_component_ns.class_("ExampleComponent", cg.Component)
9296` ` `
9397
94- This is the class that is being created. The first argument is the name of the class, the second and subsequent
95- arguments are the base classes that this class inherits from.
98+ This is the class that is being created. The first argument is the name of the class and any subsequent arguments are
99+ the base classes that this class inherits from.
96100
97101# ### Configuration validation
98102
@@ -113,10 +117,10 @@ specify their own `id` in their configuration in the event that they wish to ref
113117
114118# ### Code generation
115119
116- The `to_code` method is called after the entire configuration has been validated. This function is given the
117- parsed `config` object for this instance of this component. This method is responsible for generating the
118- C++ code that is placed into `main.cpp` translates the user configuration into the C++ instance method calls,
119- setting the variables on the object .
120+ The `to_code` method is called after the entire configuration has been validated. It is given the parsed `config`
121+ object for this instance of this component and uses it to determine exactly what C++ code is placed into the generated
122+ ` main.cpp` file. It translates the user configuration into the C++ instance method calls, setting variables on the
123+ object as required/specified .
120124
121125` ` ` python
122126var = cg.new_Pvariable(config[CONF_ID])
@@ -131,12 +135,12 @@ constructor of the class.
131135await cg.register_component(var, config)
132136` ` `
133137
134- This line generates `App.register_component(var)` in C++ which registers the component so that its `setup`, `loop` and/or `update`
135- functions are called correctly.
138+ This line generates `App.register_component(var)` in C++ which registers the component so that its `setup`, `loop`
139+ and/or `update` methods are called correctly.
136140
137141Assuming the user has `foo : true` in their YAML configuration, this line:
138142
139- ` ` ` python
143+ ` ` ` python
140144cg.add(var.set_foo(config[CONF_FOO]))
141145` ` `
142146
@@ -158,24 +162,26 @@ If the config value is not set, then we do not call the setter function.
158162
159163# ## Further information
160164
161- - ` AUTO_LOAD` - A list of components that will be automatically loaded if they are not already specified in the configuration.
162- This can be a method that can be run with access to the `CORE` information like the target platform.
163- - ` CODEOWNERS` - A list of GitHub usernames that are responsible for this component. `script/build_codeowners.py` will
165+ - `AUTO_LOAD` : A list of components that will be automatically loaded if they are not already specified in the
166+ configuration. This can be a method that can be run with access to the `CORE` information like the target platform.
167+ - `CONFLICTS_WITH` : A list of components which conflict with this component. If the user has one of them in their
168+ config, a validation error will be generated.
169+ - `CODEOWNERS` : A list of GitHub usernames that are responsible for this component. `script/build_codeowners.py` will
164170 update the `CODEOWNERS` file.
165- - ` DEPENDENCIES` - A list of components that this component depends on. If these components are not present in the configuration,
166- validation will fail and the user will be shown an error.
167- - ` MULTI_CONF` - If set to `True`, the user can use this component multiple times in their configuration. If set to a
171+ - `DEPENDENCIES` : A list of components that this component depends on. If these components are not present in the
172+ configuration, validation will fail and the user will be shown an error.
173+ - `MULTI_CONF` : If set to `True`, the user can use this component multiple times in their configuration. If set to a
168174 number, the user can use this component that number of times.
169- - ` MULTI_CONF_NO_DEFAULT` - This is a special flag that allows the component to be auto-loaded without an instance of the configuration.
170- An example of this is the `uart` component. This component can be auto-loaded so that all of the uart headers will be available but
171- potentially there is no native uart instance, but one provided by another component such an an external i2c UART expander.
172-
175+ - `MULTI_CONF_NO_DEFAULT` : This is a special flag that allows the component to be auto-loaded without an instance of
176+ the configuration. An example of this is the `uart` component. This component can be auto-loaded so that all of the
177+ UART headers will be available but potentially there is no native UART instance, but one provided by another
178+ component such an an external i2c UART expander.
173179
174180# ## Final validation
175181
176- ESPHome has a mechanism to run a final validation step after all of the configuration is initially deemed to be individually valid.
177- This final validation gives an instance of a component the ability to check the configuration of any other components and potentially fail
178- the validation stage if an important dependent configuration does not match.
182+ ESPHome has a mechanism to run a final validation step after all of the configuration is initially deemed to be
183+ individually valid. This final validation gives an instance of a component the ability to check the configuration of
184+ any other components and potentially fail the validation stage if an important dependent configuration does not match.
179185
180- For example many components that rely on `uart` can use the `FINAL_VALIDATE_SCHEMA` to ensure that the `tx_pin` and/or `rx_pin` are
181- configured.
186+ For example many components that rely on `uart` can use the `FINAL_VALIDATE_SCHEMA` to ensure that the `tx_pin` and/or
187+ ` rx_pin ` are configured.
0 commit comments