Skip to content

Commit 6f800ad

Browse files
committed
[mapping] Update docs
1 parent e31b820 commit 6f800ad

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

content/components/mapping.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ description: "Mapping Component"
33
title: "Mapping Component"
44
---
55

6-
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.
6+
The `mapping` component allows you to create a map or dictionary that allows a one-to-one translation from keys to
7+
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.
78

89
```yaml
910
# Example configuration entry
@@ -43,9 +44,14 @@ You can also map to a class. This is useful when you want to map to a more compl
4344
- `color` : Maps to a predefined [Color](#config-color). The values should each be a color ID.
4445
- The name of a C++ class defined by ESPHome, e.g. `Component`. The values should each be a ID of that class.
4546

47+
4648
## Using a mapping
4749

48-
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.
50+
A mapping defined in this component can be used in lambdas in other components. The mapping can be accessed using
51+
the ``id`` function, and the value can be looked up using the ``[]`` operator as per the above example, or the ``get`` function.
52+
A map may be updated at run time using a lambda call, e.g. ``map.set("key", value)``.
53+
54+
Maps are stored in RAM, but will use PSRAM if available.
4955

5056
A more complex example follows:
5157

@@ -84,8 +90,14 @@ display:
8490
- platform: ...
8591
# update the display drawing random text in random colors
8692
lambda: |-
87-
auto color = color_map[random_uint32() % 3];
93+
auto color = color_map.get(random_uint32() % 3]); # Uses get() to index the color_map
8894
it.printf(100, 100, id(roboto20), color, id(string_map)[random_uint32() % 3].c_str(), Color(0));
95+
96+
on_...:
97+
then:
98+
- lambda: |-
99+
id(color_map).set(2, Color::random_color());
100+
89101
```
90102

91103
## See Also

0 commit comments

Comments
 (0)