Skip to content

Commit 4e617a1

Browse files
committed
Document mapping component
1 parent 31ad6c1 commit 4e617a1

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

components/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ ESPHome-specific components or components supporting ESPHome device provisioning
6262
Improv via Serial, components/improv_serial, improv.svg, dark-invert
6363
Interval, components/interval, description.svg, dark-invert
6464
JSON, components/json, json.svg, dark-invert
65+
Mapping, components/mapping, mapping.svg, dark-invert
6566
XXTEA, components/xxtea, xxtea.svg
6667
Script, components/script, description.svg, dark-invert
6768

components/mapping.rst

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Mapping Component
2+
-----------------
3+
4+
The `mapping` component allows you to create a map or dictionary that allows a one-to-one translation from keys to values. This enables e.g. mapping a string to a number or vice versa, or mapping a string such as a weather condition to an image.
5+
6+
.. code-block:: yaml
7+
8+
# Example configuration entry
9+
mapping:
10+
- id: weather_icon
11+
from: string
12+
to: image
13+
entries:
14+
clear-night: clear_night_img
15+
cloudy: cloudy_img
16+
17+
# Using the mapping in an automation
18+
text_sensor:
19+
- id: forecast_text
20+
platform: homeassistant
21+
entity_id: weather.forecast_home
22+
on_value:
23+
lvgl.image.update:
24+
id: weather_image
25+
src: !lambda return id(weather_icon)[x];
26+
27+
Configuration variables:
28+
29+
- **id** (**Required**, :ref:`config-id`): Give the mapping an ID so that you can refer
30+
to it later in :ref:`lambdas <config-lambda>`.
31+
- **from** (**Required**, string): The type of the keys in the mapping. Can be one of ``string`` or ``int``.
32+
- **to** (**Required**, string): The type of values in the map. May be one of ``string`` or ``int`` or a class specifier as discussed below.
33+
- **entries** (**Required**, dict): A list of key-value pairs that define the mapping. The keys must be of the type specified in the ``from`` field, and the values must be of the type specified in the ``to`` field.
34+
35+
Mapping to a class
36+
##################
37+
38+
You can also map to a class. This is useful when you want to map to a more complex type, such as an image or a color. There are several types of class specifiers you can use:
39+
40+
- ``image``: Maps to an image as defined in the :doc:`/components/image` component. The values should each be an image ID.
41+
- ``color``: Maps to a predefined :ref:`config-color`. The values should each be a color ID.
42+
- The name of a C++ class defined by ESPHome, e.g. ``Component``. The values should each be a ID of that class.
43+
44+
Using a mapping
45+
###############
46+
47+
A mapping defined in this component can be used in lambdas in other components. The mapping can be accessed using the ``id`` function, and the value can be looked up using the ``[]`` operator as per the above example.
48+
49+
A more complex example follows:
50+
51+
.. code-block:: yaml
52+
53+
mapping:
54+
- id: color_map
55+
from: int
56+
to: color
57+
entries:
58+
0: red
59+
1: green
60+
2: blue
61+
- id: string_map
62+
from: int
63+
to: string
64+
entries:
65+
0: red
66+
1: green
67+
2: blue
68+
69+
color:
70+
- id: red
71+
hex: FF0000
72+
- id: green
73+
hex: 00FF00
74+
- id: blue
75+
hex: 0000FF
76+
77+
font:
78+
- file: gfonts://Roboto
79+
id: roboto20
80+
size: 20
81+
bpp: 4
82+
83+
display:
84+
- platform: ...
85+
# update the display drawing random text in random colors
86+
lambda: |-
87+
auto color = color_map[random_uint32() % 3];
88+
it.printf(100, 100, id(roboto20), color, id(string_map)[random_uint32() % 3].c_str(), Color(0));
89+
90+
91+
See Also
92+
--------
93+
94+
- :doc:`index`
95+
- :doc:`/automations/actions`
96+
- :ghedit:`Edit`

images/mapping.svg

+15
Loading

0 commit comments

Comments
 (0)