esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
json.hpp
Go to the documentation of this file.
1#pragma once
2
17
18#if defined(ESPHOME_API_WITH_JSON) || defined(__has_include)
19#if defined(ESPHOME_API_WITH_JSON) || __has_include(<nlohmann/json.hpp>)
20#define ESPHOME_API_JSON_AVAILABLE 1
21#endif
22#endif
23
24#ifdef ESPHOME_API_JSON_AVAILABLE
25
34
35#include <nlohmann/json.hpp>
36
37namespace esphome::api {
38
39// ---------------------------------------------------------------------------
40// Enums — serialized as their lower_snake_case names (first entry is the
41// fallback used for unknown values on read).
42// ---------------------------------------------------------------------------
43
44// The enum (de)serializers below expand to a static C-style lookup array inside
45// each function; that is nlohmann's macro, not our style choice.
46// NOLINTBEGIN(cert-err58-cpp,modernize-avoid-c-arrays,modernize-type-traits)
47NLOHMANN_JSON_SERIALIZE_ENUM(EntityCategory,
48 {
49 {EntityCategory::None, "none"},
50 {EntityCategory::Config, "config"},
51 {EntityCategory::Diagnostic, "diagnostic"},
52 })
53
54NLOHMANN_JSON_SERIALIZE_ENUM(ColorMode,
55 {
56 {ColorMode::Unknown, "unknown"},
57 {ColorMode::OnOff, "on_off"},
58 {ColorMode::LegacyBrightness, "legacy_brightness"},
59 {ColorMode::Brightness, "brightness"},
60 {ColorMode::White, "white"},
61 {ColorMode::ColorTemperature, "color_temperature"},
62 {ColorMode::ColdWarmWhite, "cold_warm_white"},
63 {ColorMode::Rgb, "rgb"},
64 {ColorMode::RgbWhite, "rgb_white"},
65 {ColorMode::RgbColorTemperature, "rgb_color_temperature"},
66 {ColorMode::RgbColdWarmWhite, "rgb_cold_warm_white"},
67 })
68
69NLOHMANN_JSON_SERIALIZE_ENUM(CoverOperation,
70 {
71 {CoverOperation::Idle, "idle"},
72 {CoverOperation::IsOpening, "is_opening"},
73 {CoverOperation::IsClosing, "is_closing"},
74 })
75
76NLOHMANN_JSON_SERIALIZE_ENUM(ValveOperation,
77 {
78 {ValveOperation::Idle, "idle"},
79 {ValveOperation::IsOpening, "is_opening"},
80 {ValveOperation::IsClosing, "is_closing"},
81 })
82
83NLOHMANN_JSON_SERIALIZE_ENUM(FanDirection,
84 {
85 {FanDirection::Forward, "forward"},
86 {FanDirection::Reverse, "reverse"},
87 })
88
89NLOHMANN_JSON_SERIALIZE_ENUM(SensorStateClass,
90 {
91 {SensorStateClass::None, "none"},
92 {SensorStateClass::Measurement, "measurement"},
93 {SensorStateClass::TotalIncreasing, "total_increasing"},
94 {SensorStateClass::Total, "total"},
95 {SensorStateClass::MeasurementAngle, "measurement_angle"},
96 })
97
98NLOHMANN_JSON_SERIALIZE_ENUM(LogLevel,
99 {
100 {LogLevel::None, "none"},
101 {LogLevel::Error, "error"},
102 {LogLevel::Warn, "warn"},
103 {LogLevel::Info, "info"},
104 {LogLevel::Config, "config"},
105 {LogLevel::Debug, "debug"},
106 {LogLevel::Verbose, "verbose"},
107 {LogLevel::VeryVerbose, "very_verbose"},
108 })
109
110NLOHMANN_JSON_SERIALIZE_ENUM(TemperatureUnit,
111 {
112 {TemperatureUnit::Celsius, "celsius"},
113 {TemperatureUnit::Fahrenheit, "fahrenheit"},
114 {TemperatureUnit::Kelvin, "kelvin"},
115 })
116
117NLOHMANN_JSON_SERIALIZE_ENUM(ClimateMode,
118 {
119 {ClimateMode::Off, "off"},
120 {ClimateMode::HeatCool, "heat_cool"},
121 {ClimateMode::Cool, "cool"},
122 {ClimateMode::Heat, "heat"},
123 {ClimateMode::FanOnly, "fan_only"},
124 {ClimateMode::Dry, "dry"},
125 {ClimateMode::Auto, "auto"},
126 })
127
128NLOHMANN_JSON_SERIALIZE_ENUM(ClimateFanMode,
129 {
130 {ClimateFanMode::On, "on"},
131 {ClimateFanMode::Off, "off"},
132 {ClimateFanMode::Auto, "auto"},
133 {ClimateFanMode::Low, "low"},
134 {ClimateFanMode::Medium, "medium"},
135 {ClimateFanMode::High, "high"},
136 {ClimateFanMode::Middle, "middle"},
137 {ClimateFanMode::Focus, "focus"},
138 {ClimateFanMode::Diffuse, "diffuse"},
139 {ClimateFanMode::Quiet, "quiet"},
140 })
141
142NLOHMANN_JSON_SERIALIZE_ENUM(ClimateSwingMode,
143 {
144 {ClimateSwingMode::Off, "off"},
145 {ClimateSwingMode::Both, "both"},
146 {ClimateSwingMode::Vertical, "vertical"},
147 {ClimateSwingMode::Horizontal, "horizontal"},
148 })
149
150NLOHMANN_JSON_SERIALIZE_ENUM(ClimateAction,
151 {
152 {ClimateAction::Off, "off"},
153 {ClimateAction::Cooling, "cooling"},
154 {ClimateAction::Heating, "heating"},
155 {ClimateAction::Idle, "idle"},
156 {ClimateAction::Drying, "drying"},
157 {ClimateAction::Fan, "fan"},
158 {ClimateAction::Defrosting, "defrosting"},
159 })
160
161NLOHMANN_JSON_SERIALIZE_ENUM(ClimatePreset,
162 {
163 {ClimatePreset::None, "none"},
164 {ClimatePreset::Home, "home"},
165 {ClimatePreset::Away, "away"},
166 {ClimatePreset::Boost, "boost"},
167 {ClimatePreset::Comfort, "comfort"},
168 {ClimatePreset::Eco, "eco"},
169 {ClimatePreset::Sleep, "sleep"},
170 {ClimatePreset::Activity, "activity"},
171 })
172
173NLOHMANN_JSON_SERIALIZE_ENUM(NumberMode,
174 {
175 {NumberMode::Auto, "auto"},
176 {NumberMode::Box, "box"},
177 {NumberMode::Slider, "slider"},
178 })
179
180NLOHMANN_JSON_SERIALIZE_ENUM(TextMode,
181 {
182 {TextMode::Text, "text"},
183 {TextMode::Password, "password"},
184 })
185
186NLOHMANN_JSON_SERIALIZE_ENUM(LockState,
187 {
188 {LockState::None, "none"},
189 {LockState::Locked, "locked"},
190 {LockState::Unlocked, "unlocked"},
191 {LockState::Jammed, "jammed"},
192 {LockState::Locking, "locking"},
193 {LockState::Unlocking, "unlocking"},
194 {LockState::Opening, "opening"},
195 {LockState::Open, "open"},
196 })
197
198NLOHMANN_JSON_SERIALIZE_ENUM(MediaPlayerState,
199 {
200 {MediaPlayerState::None, "none"},
201 {MediaPlayerState::Idle, "idle"},
202 {MediaPlayerState::Playing, "playing"},
203 {MediaPlayerState::Paused, "paused"},
204 {MediaPlayerState::Announcing, "announcing"},
205 {MediaPlayerState::Off, "off"},
206 {MediaPlayerState::On, "on"},
207 })
208
209NLOHMANN_JSON_SERIALIZE_ENUM(MediaPlayerFormatPurpose,
210 {
213 })
214
215NLOHMANN_JSON_SERIALIZE_ENUM(AlarmControlPanelState,
216 {
218 {AlarmControlPanelState::ArmedHome, "armed_home"},
219 {AlarmControlPanelState::ArmedAway, "armed_away"},
220 {AlarmControlPanelState::ArmedNight, "armed_night"},
221 {AlarmControlPanelState::ArmedVacation, "armed_vacation"},
222 {AlarmControlPanelState::ArmedCustomBypass, "armed_custom_bypass"},
227 })
228
229NLOHMANN_JSON_SERIALIZE_ENUM(WaterHeaterMode,
230 {
231 {WaterHeaterMode::Off, "off"},
232 {WaterHeaterMode::Eco, "eco"},
233 {WaterHeaterMode::Electric, "electric"},
234 {WaterHeaterMode::Performance, "performance"},
235 {WaterHeaterMode::HighDemand, "high_demand"},
236 {WaterHeaterMode::HeatPump, "heat_pump"},
237 {WaterHeaterMode::Gas, "gas"},
238 })
239
240NLOHMANN_JSON_SERIALIZE_ENUM(LockCommand,
241 {
242 {LockCommand::Unlock, "unlock"},
243 {LockCommand::Lock, "lock"},
244 {LockCommand::Open, "open"},
245 })
246
247NLOHMANN_JSON_SERIALIZE_ENUM(SerialProxyPortType,
248 {
252 })
253
254NLOHMANN_JSON_SERIALIZE_ENUM(SerialProxyParity,
255 {
256 {SerialProxyParity::None, "none"},
257 {SerialProxyParity::Even, "even"},
258 {SerialProxyParity::Odd, "odd"},
259 })
260
261NLOHMANN_JSON_SERIALIZE_ENUM(SerialProxyRequestType,
262 {
266 })
267
268NLOHMANN_JSON_SERIALIZE_ENUM(SerialProxyStatus,
269 {
270 {SerialProxyStatus::Ok, "ok"},
271 {SerialProxyStatus::AssumedSuccess, "assumed_success"},
272 {SerialProxyStatus::Error, "error"},
273 {SerialProxyStatus::Timeout, "timeout"},
274 {SerialProxyStatus::NotSupported, "not_supported"},
275 })
276
277NLOHMANN_JSON_SERIALIZE_ENUM(BluetoothScannerState,
278 {
285 })
286
287NLOHMANN_JSON_SERIALIZE_ENUM(BluetoothScannerMode,
288 {
291 })
292// NOLINTEND(cert-err58-cpp,modernize-avoid-c-arrays,modernize-type-traits)
293
294// ---------------------------------------------------------------------------
295// Shared entity metadata (base of every <X>Info).
296// ---------------------------------------------------------------------------
297
298inline void entity_info_to_json(nlohmann::json& j, const EntityInfo& v) {
299 j["key"] = v.key;
300 j["object_id"] = v.object_id;
301 j["name"] = v.name;
302 j["icon"] = v.icon;
303 j["disabled_by_default"] = v.disabled_by_default;
304 j["entity_category"] = v.entity_category;
305 j["device_id"] = v.device_id;
306}
307
308inline void entity_info_from_json(const nlohmann::json& j, EntityInfo& v) {
309 v.key = j.value("key", 0U);
310 v.object_id = j.value("object_id", std::string{});
311 v.name = j.value("name", std::string{});
312 v.icon = j.value("icon", std::string{});
313 v.disabled_by_default = j.value("disabled_by_default", false);
314 v.entity_category = j.value("entity_category", EntityCategory::None);
315 v.device_id = j.value("device_id", 0U);
316}
317
318// ---------------------------------------------------------------------------
319// Entity info structs.
320// ---------------------------------------------------------------------------
321
322inline void to_json(nlohmann::json& j, const BinarySensorInfo& v) {
323 entity_info_to_json(j, v);
324 j["device_class"] = v.device_class;
325 j["is_status_binary_sensor"] = v.is_status_binary_sensor;
326}
327inline void from_json(const nlohmann::json& j, BinarySensorInfo& v) {
328 entity_info_from_json(j, v);
329 v.device_class = j.value("device_class", std::string{});
330 v.is_status_binary_sensor = j.value("is_status_binary_sensor", false);
331}
332
333inline void to_json(nlohmann::json& j, const SensorInfo& v) {
334 entity_info_to_json(j, v);
335 j["unit_of_measurement"] = v.unit_of_measurement;
336 j["accuracy_decimals"] = v.accuracy_decimals;
337 j["force_update"] = v.force_update;
338 j["device_class"] = v.device_class;
339 j["state_class"] = v.state_class;
340}
341inline void from_json(const nlohmann::json& j, SensorInfo& v) {
342 entity_info_from_json(j, v);
343 v.unit_of_measurement = j.value("unit_of_measurement", std::string{});
344 v.accuracy_decimals = j.value("accuracy_decimals", 0);
345 v.force_update = j.value("force_update", false);
346 v.device_class = j.value("device_class", std::string{});
347 v.state_class = j.value("state_class", SensorStateClass::None);
348}
349
350inline void to_json(nlohmann::json& j, const TextSensorInfo& v) {
351 entity_info_to_json(j, v);
352 j["device_class"] = v.device_class;
353}
354inline void from_json(const nlohmann::json& j, TextSensorInfo& v) {
355 entity_info_from_json(j, v);
356 v.device_class = j.value("device_class", std::string{});
357}
358
359inline void to_json(nlohmann::json& j, const SwitchInfo& v) {
360 entity_info_to_json(j, v);
361 j["assumed_state"] = v.assumed_state;
362 j["device_class"] = v.device_class;
363}
364inline void from_json(const nlohmann::json& j, SwitchInfo& v) {
365 entity_info_from_json(j, v);
366 v.assumed_state = j.value("assumed_state", false);
367 v.device_class = j.value("device_class", std::string{});
368}
369
370inline void to_json(nlohmann::json& j, const LightInfo& v) {
371 entity_info_to_json(j, v);
372 j["supported_color_modes"] = v.supported_color_modes;
373 j["min_mireds"] = v.min_mireds;
374 j["max_mireds"] = v.max_mireds;
375 j["effects"] = v.effects;
376}
377inline void from_json(const nlohmann::json& j, LightInfo& v) {
378 entity_info_from_json(j, v);
379 v.supported_color_modes = j.value("supported_color_modes", std::vector<ColorMode>{});
380 v.min_mireds = j.value("min_mireds", 0.0F);
381 v.max_mireds = j.value("max_mireds", 0.0F);
382 v.effects = j.value("effects", std::vector<std::string>{});
383}
384
385inline void to_json(nlohmann::json& j, const CoverInfo& v) {
386 entity_info_to_json(j, v);
387 j["assumed_state"] = v.assumed_state;
388 j["supports_position"] = v.supports_position;
389 j["supports_tilt"] = v.supports_tilt;
390 j["device_class"] = v.device_class;
391 j["supports_stop"] = v.supports_stop;
392}
393inline void from_json(const nlohmann::json& j, CoverInfo& v) {
394 entity_info_from_json(j, v);
395 v.assumed_state = j.value("assumed_state", false);
396 v.supports_position = j.value("supports_position", false);
397 v.supports_tilt = j.value("supports_tilt", false);
398 v.device_class = j.value("device_class", std::string{});
399 v.supports_stop = j.value("supports_stop", false);
400}
401
402inline void to_json(nlohmann::json& j, const FanInfo& v) {
403 entity_info_to_json(j, v);
404 j["supports_oscillation"] = v.supports_oscillation;
405 j["supports_speed"] = v.supports_speed;
406 j["supports_direction"] = v.supports_direction;
407 j["supported_speed_count"] = v.supported_speed_count;
408 j["supported_preset_modes"] = v.supported_preset_modes;
409}
410inline void from_json(const nlohmann::json& j, FanInfo& v) {
411 entity_info_from_json(j, v);
412 v.supports_oscillation = j.value("supports_oscillation", false);
413 v.supports_speed = j.value("supports_speed", false);
414 v.supports_direction = j.value("supports_direction", false);
415 v.supported_speed_count = j.value("supported_speed_count", 0);
416 v.supported_preset_modes = j.value("supported_preset_modes", std::vector<std::string>{});
417}
418
419inline void to_json(nlohmann::json& j, const ClimateInfo& v) {
420 entity_info_to_json(j, v);
421 j["supported_modes"] = v.supported_modes;
422 j["visual_min_temperature"] = v.visual_min_temperature;
423 j["visual_max_temperature"] = v.visual_max_temperature;
424 j["visual_target_temperature_step"] = v.visual_target_temperature_step;
425 j["supported_fan_modes"] = v.supported_fan_modes;
426 j["supported_swing_modes"] = v.supported_swing_modes;
427 j["supported_custom_fan_modes"] = v.supported_custom_fan_modes;
428 j["supported_presets"] = v.supported_presets;
429 j["supported_custom_presets"] = v.supported_custom_presets;
430 j["visual_current_temperature_step"] = v.visual_current_temperature_step;
431 j["visual_min_humidity"] = v.visual_min_humidity;
432 j["visual_max_humidity"] = v.visual_max_humidity;
433 j["feature_flags"] = v.feature_flags;
434 j["temperature_unit"] = v.temperature_unit;
435}
436inline void from_json(const nlohmann::json& j, ClimateInfo& v) {
437 entity_info_from_json(j, v);
438 v.supported_modes = j.value("supported_modes", std::vector<ClimateMode>{});
439 v.visual_min_temperature = j.value("visual_min_temperature", 0.0F);
440 v.visual_max_temperature = j.value("visual_max_temperature", 0.0F);
441 v.visual_target_temperature_step = j.value("visual_target_temperature_step", 0.0F);
442 v.supported_fan_modes = j.value("supported_fan_modes", std::vector<ClimateFanMode>{});
443 v.supported_swing_modes = j.value("supported_swing_modes", std::vector<ClimateSwingMode>{});
444 v.supported_custom_fan_modes =
445 j.value("supported_custom_fan_modes", std::vector<std::string>{});
446 v.supported_presets = j.value("supported_presets", std::vector<ClimatePreset>{});
447 v.supported_custom_presets = j.value("supported_custom_presets", std::vector<std::string>{});
448 v.visual_current_temperature_step = j.value("visual_current_temperature_step", 0.0F);
449 v.visual_min_humidity = j.value("visual_min_humidity", 0.0F);
450 v.visual_max_humidity = j.value("visual_max_humidity", 0.0F);
451 v.feature_flags = j.value("feature_flags", 0U);
452 v.temperature_unit = j.value("temperature_unit", TemperatureUnit::Celsius);
453}
454
455inline void to_json(nlohmann::json& j, const NumberInfo& v) {
456 entity_info_to_json(j, v);
457 j["min_value"] = v.min_value;
458 j["max_value"] = v.max_value;
459 j["step"] = v.step;
460 j["unit_of_measurement"] = v.unit_of_measurement;
461 j["mode"] = v.mode;
462 j["device_class"] = v.device_class;
463}
464inline void from_json(const nlohmann::json& j, NumberInfo& v) {
465 entity_info_from_json(j, v);
466 v.min_value = j.value("min_value", 0.0F);
467 v.max_value = j.value("max_value", 0.0F);
468 v.step = j.value("step", 0.0F);
469 v.unit_of_measurement = j.value("unit_of_measurement", std::string{});
470 v.mode = j.value("mode", NumberMode::Auto);
471 v.device_class = j.value("device_class", std::string{});
472}
473
474inline void to_json(nlohmann::json& j, const SelectInfo& v) {
475 entity_info_to_json(j, v);
476 j["options"] = v.options;
477}
478inline void from_json(const nlohmann::json& j, SelectInfo& v) {
479 entity_info_from_json(j, v);
480 v.options = j.value("options", std::vector<std::string>{});
481}
482
483inline void to_json(nlohmann::json& j, const TextInfo& v) {
484 entity_info_to_json(j, v);
485 j["min_length"] = v.min_length;
486 j["max_length"] = v.max_length;
487 j["pattern"] = v.pattern;
488 j["mode"] = v.mode;
489}
490inline void from_json(const nlohmann::json& j, TextInfo& v) {
491 entity_info_from_json(j, v);
492 v.min_length = j.value("min_length", 0U);
493 v.max_length = j.value("max_length", 0U);
494 v.pattern = j.value("pattern", std::string{});
495 v.mode = j.value("mode", TextMode::Text);
496}
497
498inline void to_json(nlohmann::json& j, const ButtonInfo& v) {
499 entity_info_to_json(j, v);
500 j["device_class"] = v.device_class;
501}
502inline void from_json(const nlohmann::json& j, ButtonInfo& v) {
503 entity_info_from_json(j, v);
504 v.device_class = j.value("device_class", std::string{});
505}
506
507inline void to_json(nlohmann::json& j, const LockInfo& v) {
508 entity_info_to_json(j, v);
509 j["assumed_state"] = v.assumed_state;
510 j["supports_open"] = v.supports_open;
511 j["requires_code"] = v.requires_code;
512 j["code_format"] = v.code_format;
513}
514inline void from_json(const nlohmann::json& j, LockInfo& v) {
515 entity_info_from_json(j, v);
516 v.assumed_state = j.value("assumed_state", false);
517 v.supports_open = j.value("supports_open", false);
518 v.requires_code = j.value("requires_code", false);
519 v.code_format = j.value("code_format", std::string{});
520}
521
522inline void to_json(nlohmann::json& j, const MediaPlayerSupportedFormat& v) {
523 j = nlohmann::json{{"format", v.format},
524 {"sample_rate", v.sample_rate},
525 {"num_channels", v.num_channels},
526 {"purpose", v.purpose},
527 {"sample_bytes", v.sample_bytes}};
528}
529inline void from_json(const nlohmann::json& j, MediaPlayerSupportedFormat& v) {
530 v.format = j.value("format", std::string{});
531 v.sample_rate = j.value("sample_rate", 0U);
532 v.num_channels = j.value("num_channels", 0U);
533 v.purpose = j.value("purpose", MediaPlayerFormatPurpose::Default);
534 v.sample_bytes = j.value("sample_bytes", 0U);
535}
536
537inline void to_json(nlohmann::json& j, const MediaPlayerInfo& v) {
538 entity_info_to_json(j, v);
539 j["supports_pause"] = v.supports_pause;
540 j["supported_formats"] = v.supported_formats;
541}
542inline void from_json(const nlohmann::json& j, MediaPlayerInfo& v) {
543 entity_info_from_json(j, v);
544 v.supports_pause = j.value("supports_pause", false);
545 v.supported_formats = j.value("supported_formats", std::vector<MediaPlayerSupportedFormat>{});
546}
547
548inline void to_json(nlohmann::json& j, const CameraInfo& v) {
549 entity_info_to_json(j, v);
550}
551inline void from_json(const nlohmann::json& j, CameraInfo& v) {
552 entity_info_from_json(j, v);
553}
554
555inline void to_json(nlohmann::json& j, const SirenInfo& v) {
556 entity_info_to_json(j, v);
557 j["tones"] = v.tones;
558 j["supports_duration"] = v.supports_duration;
559 j["supports_volume"] = v.supports_volume;
560}
561inline void from_json(const nlohmann::json& j, SirenInfo& v) {
562 entity_info_from_json(j, v);
563 v.tones = j.value("tones", std::vector<std::string>{});
564 v.supports_duration = j.value("supports_duration", false);
565 v.supports_volume = j.value("supports_volume", false);
566}
567
568inline void to_json(nlohmann::json& j, const AlarmControlPanelInfo& v) {
569 entity_info_to_json(j, v);
570 j["supported_features"] = v.supported_features;
571 j["requires_code"] = v.requires_code;
572 j["requires_code_to_arm"] = v.requires_code_to_arm;
573}
574inline void from_json(const nlohmann::json& j, AlarmControlPanelInfo& v) {
575 entity_info_from_json(j, v);
576 v.supported_features = j.value("supported_features", 0U);
577 v.requires_code = j.value("requires_code", false);
578 v.requires_code_to_arm = j.value("requires_code_to_arm", false);
579}
580
581inline void to_json(nlohmann::json& j, const DateInfo& v) {
582 entity_info_to_json(j, v);
583}
584inline void from_json(const nlohmann::json& j, DateInfo& v) {
585 entity_info_from_json(j, v);
586}
587
588inline void to_json(nlohmann::json& j, const DateTimeInfo& v) {
589 entity_info_to_json(j, v);
590}
591inline void from_json(const nlohmann::json& j, DateTimeInfo& v) {
592 entity_info_from_json(j, v);
593}
594
595inline void to_json(nlohmann::json& j, const TimeInfo& v) {
596 entity_info_to_json(j, v);
597}
598inline void from_json(const nlohmann::json& j, TimeInfo& v) {
599 entity_info_from_json(j, v);
600}
601
602inline void to_json(nlohmann::json& j, const ValveInfo& v) {
603 entity_info_to_json(j, v);
604 j["device_class"] = v.device_class;
605 j["assumed_state"] = v.assumed_state;
606 j["supports_position"] = v.supports_position;
607 j["supports_stop"] = v.supports_stop;
608}
609inline void from_json(const nlohmann::json& j, ValveInfo& v) {
610 entity_info_from_json(j, v);
611 v.device_class = j.value("device_class", std::string{});
612 v.assumed_state = j.value("assumed_state", false);
613 v.supports_position = j.value("supports_position", false);
614 v.supports_stop = j.value("supports_stop", false);
615}
616
617inline void to_json(nlohmann::json& j, const EventInfo& v) {
618 entity_info_to_json(j, v);
619 j["device_class"] = v.device_class;
620 j["event_types"] = v.event_types;
621}
622inline void from_json(const nlohmann::json& j, EventInfo& v) {
623 entity_info_from_json(j, v);
624 v.device_class = j.value("device_class", std::string{});
625 v.event_types = j.value("event_types", std::vector<std::string>{});
626}
627
628inline void to_json(nlohmann::json& j, const UpdateInfo& v) {
629 entity_info_to_json(j, v);
630 j["device_class"] = v.device_class;
631}
632inline void from_json(const nlohmann::json& j, UpdateInfo& v) {
633 entity_info_from_json(j, v);
634 v.device_class = j.value("device_class", std::string{});
635}
636
637inline void to_json(nlohmann::json& j, const WaterHeaterInfo& v) {
638 entity_info_to_json(j, v);
639 j["min_temperature"] = v.min_temperature;
640 j["max_temperature"] = v.max_temperature;
641 j["target_temperature_step"] = v.target_temperature_step;
642 j["supported_modes"] = v.supported_modes;
643 j["supported_features"] = v.supported_features;
644 j["temperature_unit"] = v.temperature_unit;
645}
646inline void from_json(const nlohmann::json& j, WaterHeaterInfo& v) {
647 entity_info_from_json(j, v);
648 v.min_temperature = j.value("min_temperature", 0.0F);
649 v.max_temperature = j.value("max_temperature", 0.0F);
650 v.target_temperature_step = j.value("target_temperature_step", 0.0F);
651 v.supported_modes = j.value("supported_modes", std::vector<WaterHeaterMode>{});
652 v.supported_features = j.value("supported_features", 0U);
653 v.temperature_unit = j.value("temperature_unit", TemperatureUnit::Celsius);
654}
655
656inline void to_json(nlohmann::json& j, const InfraredInfo& v) {
657 entity_info_to_json(j, v);
658 j["capabilities"] = v.capabilities;
659 j["receiver_frequency"] = v.receiver_frequency;
660}
661inline void from_json(const nlohmann::json& j, InfraredInfo& v) {
662 entity_info_from_json(j, v);
663 v.capabilities = j.value("capabilities", 0U);
664 v.receiver_frequency = j.value("receiver_frequency", 0U);
665}
666
667inline void to_json(nlohmann::json& j, const RadioFrequencyInfo& v) {
668 entity_info_to_json(j, v);
669 j["capabilities"] = v.capabilities;
670 j["frequency_min"] = v.frequency_min;
671 j["frequency_max"] = v.frequency_max;
672 j["supported_modulations"] = v.supported_modulations;
673}
674inline void from_json(const nlohmann::json& j, RadioFrequencyInfo& v) {
675 entity_info_from_json(j, v);
676 v.capabilities = j.value("capabilities", 0U);
677 v.frequency_min = j.value("frequency_min", 0U);
678 v.frequency_max = j.value("frequency_max", 0U);
679 v.supported_modulations = j.value("supported_modulations", 0U);
680}
681
682// ---------------------------------------------------------------------------
683// Entity state structs (no std::optional fields → macro-friendly).
684// ---------------------------------------------------------------------------
685
686NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BinarySensorState, key, state, missing_state)
687NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SensorState, key, state, missing_state)
688NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(TextSensorState, key, state, missing_state)
689NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SwitchState, key, state)
690NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(LightState,
691 key,
692 state,
693 brightness,
694 color_mode,
695 color_brightness,
696 red,
697 green,
698 blue,
699 white,
700 color_temperature,
701 cold_white,
702 warm_white,
703 effect)
704NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(CoverState, key, position, tilt, current_operation)
705NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
706 FanState, key, state, oscillating, direction, speed_level, preset_mode)
707NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ClimateState,
708 key,
709 mode,
710 current_temperature,
711 target_temperature,
712 target_temperature_low,
713 target_temperature_high,
714 action,
715 fan_mode,
716 swing_mode,
717 custom_fan_mode,
718 preset,
719 custom_preset,
720 current_humidity,
721 target_humidity)
722NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(NumberState, key, state, missing_state)
723NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SelectState, key, state, missing_state)
724NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(TextState, key, state, missing_state)
725NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(LockEntityState, key, state)
726NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(MediaPlayerStatus, key, state, volume, muted)
727NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SirenState, key, state)
728NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(AlarmControlPanelStatus, key, state)
729NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(DateState, key, missing_state, year, month, day)
730NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(DateTimeState, key, missing_state, epoch_seconds)
731NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(TimeState, key, missing_state, hour, minute, second)
732NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ValveState, key, position, current_operation)
733NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(EventState, key, event_type)
734NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(UpdateState,
735 key,
736 missing_state,
737 in_progress,
738 has_progress,
739 progress,
740 current_version,
741 latest_version,
742 title,
743 release_summary,
744 release_url)
745NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(WaterHeaterState,
746 key,
747 current_temperature,
748 target_temperature,
749 mode,
750 state,
751 target_temperature_low,
752 target_temperature_high)
753
754// ---------------------------------------------------------------------------
755// Device info + discovery.
756// ---------------------------------------------------------------------------
757
758NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SerialProxyPortInfo, name, port_type)
759NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(DeviceInfo,
760 name,
761 friendly_name,
762 mac_address,
763 bluetooth_mac_address,
764 esphome_version,
765 compilation_time,
766 model,
767 manufacturer,
768 project_name,
769 project_version,
770 suggested_area,
771 has_deep_sleep,
772 webserver_port,
773 api_encryption_supported,
774 bluetooth_proxy_feature_flags,
775 voice_assistant_feature_flags,
776 zwave_proxy_feature_flags,
777 zwave_home_id,
778 serial_proxies)
779NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(DiscoveredDevice,
780 name,
781 hostname,
782 address,
783 port,
784 mac,
785 version,
786 friendly_name,
787 platform,
788 board,
789 network,
790 project_name,
791 project_version,
792 requires_encryption,
793 supports_encryption,
794 properties)
795
796// ---------------------------------------------------------------------------
797// Log stream.
798// ---------------------------------------------------------------------------
799
800NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(LogEntry, level, message)
801
802// ---------------------------------------------------------------------------
803// Bluetooth proxy.
804// ---------------------------------------------------------------------------
805
806NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleServiceData, uuid, data)
807NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleManufacturerData, uuid, data)
808NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleAdvertisement,
809 address,
810 name,
811 rssi,
812 address_type,
813 service_data,
814 service_uuids,
815 manufacturer_data)
816NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
817 BleRawAdvertisement, address, rssi, address_type, data)
818NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleScannerState, state, mode, configured_mode)
819NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleConnection, address, connected, mtu, error)
820NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleConnectionsFree, free, limit, allocated)
821NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattRead, address, handle, data)
822NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattNotifyData, address, handle, data)
823NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattDescriptor, uuid, handle)
824NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
825 BleGattCharacteristic, uuid, handle, properties, descriptors)
826NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattService, uuid, handle, characteristics)
827NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattServices, address, services)
828NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattError, address, handle, error)
829NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattReadResult, address, handle, ok, data, error)
830NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattWriteResult, address, handle, ok, error)
831NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(BleGattServicesResult, address, ok, services, error)
832
833// ---------------------------------------------------------------------------
834// Serial proxy.
835// ---------------------------------------------------------------------------
836
837NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
838 SerialProxyConfig, instance, baudrate, flow_control, parity, stop_bits, data_size)
839NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SerialProxyData, instance, data)
840NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
841 SerialProxyResponse, instance, type, status, error_message)
842NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SerialProxyLineStates, rts, dtr)
843NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SerialProxyModemPins, instance, lines)
844
845} // namespace esphome::api
846
847#endif // ESPHOME_API_JSON_AVAILABLE
Bluetooth proxy subsystem: BLE scanning, connections, and GATT.
Typed view of the device's DeviceInfoResponse.
Local-network discovery of ESPHome devices via mDNS / DNS-SD (service _esphomelib....
std::string name
Definition emit_registry.cpp:14
Typed store of a device's entities: ingest ListEntities*‍/State messages, query typed info/state per ...
Typed mirrors of the ESPHome protobuf enums.
Typed Lock entity (info + state + command).
Device log streaming subsystem.
Definition bytes.hpp:10
TextMode
Mirror of proto enum TextMode.
Definition enums.hpp:364
TemperatureUnit
Mirror of proto enum TemperatureUnit.
Definition enums.hpp:131
LogLevel
Mirror of proto enum LogLevel.
Definition enums.hpp:91
CoverOperation
Mirror of proto enum CoverOperation.
Definition enums.hpp:33
SerialProxyStatus
Mirror of proto enum SerialProxyStatus.
Definition enums.hpp:405
ClimateSwingMode
Mirror of proto enum ClimateSwingMode.
Definition enums.hpp:163
SerialProxyParity
Mirror of proto enum SerialProxyParity.
Definition enums.hpp:391
ColorMode
Mirror of proto enum ColorMode.
Definition enums.hpp:60
FanDirection
Mirror of proto enum FanDirection.
Definition enums.hpp:54
NumberMode
Mirror of proto enum NumberMode.
Definition enums.hpp:217
WaterHeaterMode
Mirror of proto enum WaterHeaterMode.
Definition enums.hpp:194
BluetoothScannerState
Mirror of proto enum BluetoothScannerState.
Definition enums.hpp:282
EntityCategory
Mirror of proto enum EntityCategory.
Definition enums.hpp:20
BluetoothScannerMode
Mirror of proto enum BluetoothScannerMode.
Definition enums.hpp:292
MediaPlayerState
Mirror of proto enum MediaPlayerState.
Definition enums.hpp:236
ClimateMode
Mirror of proto enum ClimateMode.
Definition enums.hpp:138
MediaPlayerFormatPurpose
Mirror of proto enum MediaPlayerFormatPurpose.
Definition enums.hpp:265
ClimatePreset
Mirror of proto enum ClimatePreset.
Definition enums.hpp:182
SensorStateClass
Mirror of proto enum SensorStateClass.
Definition enums.hpp:75
ValveOperation
Mirror of proto enum ValveOperation.
Definition enums.hpp:370
AlarmControlPanelState
Mirror of proto enum AlarmControlPanelState.
Definition enums.hpp:339
ClimateAction
Mirror of proto enum ClimateAction.
Definition enums.hpp:171
SerialProxyRequestType
Mirror of proto enum SerialProxyRequestType.
Definition enums.hpp:398
ClimateFanMode
Mirror of proto enum ClimateFanMode.
Definition enums.hpp:149
LockState
Mirror of proto enum LockState.
Definition enums.hpp:224
SerialProxyPortType
Mirror of proto enum SerialProxyPortType.
Definition enums.hpp:13
LockCommand
Mirror of proto enum LockCommand (not present in the generated enums mirror).
Definition lock.hpp:24
Serial (UART) proxy subsystem.