esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
entity_store.hpp
Go to the documentation of this file.
1#pragma once
2
6
36
37#include <cstdint>
38#include <functional>
39#include <memory>
40#include <optional>
41#include <string>
42#include <unordered_map>
43#include <vector>
44
45namespace esphome::api {
46
51 std::uint32_t key = 0;
52 std::string object_id;
53 std::string name;
54 std::shared_ptr<ProtoMessage> info;
55 std::shared_ptr<ProtoMessage> state;
56};
57
61public:
62 using StateCallback = std::function<void(EntityType type, std::uint32_t key)>;
63
67 bool ingest(const ProtoMessage& msg);
68
70 void clear();
71
74 state_cb_ = std::move(cb);
75 }
76
77 [[nodiscard]] std::size_t size() const noexcept {
78 return entities_.size();
79 }
80 [[nodiscard]] const StoredEntity* find(std::uint32_t key) const;
81 [[nodiscard]] const StoredEntity* find_by_object_id(const std::string& object_id) const;
83 [[nodiscard]] std::vector<const StoredEntity*> entities() const;
84
85 [[nodiscard]] std::optional<BinarySensorInfo> binary_sensor_info(std::uint32_t key) const;
86 [[nodiscard]] std::optional<BinarySensorState> binary_sensor_state(std::uint32_t key) const;
87 [[nodiscard]] std::optional<SensorInfo> sensor_info(std::uint32_t key) const;
88 [[nodiscard]] std::optional<SensorState> sensor_state(std::uint32_t key) const;
89 [[nodiscard]] std::optional<TextSensorInfo> text_sensor_info(std::uint32_t key) const;
90 [[nodiscard]] std::optional<TextSensorState> text_sensor_state(std::uint32_t key) const;
91 [[nodiscard]] std::optional<SwitchInfo> switch_info(std::uint32_t key) const;
92 [[nodiscard]] std::optional<SwitchState> switch_state(std::uint32_t key) const;
93 [[nodiscard]] std::optional<LightInfo> light_info(std::uint32_t key) const;
94 [[nodiscard]] std::optional<LightState> light_state(std::uint32_t key) const;
95 [[nodiscard]] std::optional<CoverInfo> cover_info(std::uint32_t key) const;
96 [[nodiscard]] std::optional<CoverState> cover_state(std::uint32_t key) const;
97 [[nodiscard]] std::optional<FanInfo> fan_info(std::uint32_t key) const;
98 [[nodiscard]] std::optional<FanState> fan_state(std::uint32_t key) const;
99 [[nodiscard]] std::optional<ClimateInfo> climate_info(std::uint32_t key) const;
100 [[nodiscard]] std::optional<ClimateState> climate_state(std::uint32_t key) const;
101 [[nodiscard]] std::optional<NumberInfo> number_info(std::uint32_t key) const;
102 [[nodiscard]] std::optional<NumberState> number_state(std::uint32_t key) const;
103 [[nodiscard]] std::optional<SelectInfo> select_info(std::uint32_t key) const;
104 [[nodiscard]] std::optional<SelectState> select_state(std::uint32_t key) const;
105 [[nodiscard]] std::optional<TextInfo> text_info(std::uint32_t key) const;
106 [[nodiscard]] std::optional<TextState> text_state(std::uint32_t key) const;
107 [[nodiscard]] std::optional<ButtonInfo> button_info(std::uint32_t key) const;
108 [[nodiscard]] std::optional<LockInfo> lock_info(std::uint32_t key) const;
109 [[nodiscard]] std::optional<LockEntityState> lock_state(std::uint32_t key) const;
110 [[nodiscard]] std::optional<MediaPlayerInfo> media_player_info(std::uint32_t key) const;
111 [[nodiscard]] std::optional<MediaPlayerStatus> media_player_state(std::uint32_t key) const;
112 [[nodiscard]] std::optional<CameraInfo> camera_info(std::uint32_t key) const;
113 [[nodiscard]] std::optional<SirenInfo> siren_info(std::uint32_t key) const;
114 [[nodiscard]] std::optional<SirenState> siren_state(std::uint32_t key) const;
115 [[nodiscard]] std::optional<AlarmControlPanelInfo>
116 alarm_control_panel_info(std::uint32_t key) const;
117 [[nodiscard]] std::optional<AlarmControlPanelStatus>
118 alarm_control_panel_state(std::uint32_t key) const;
119 [[nodiscard]] std::optional<DateInfo> date_info(std::uint32_t key) const;
120 [[nodiscard]] std::optional<DateState> date_state(std::uint32_t key) const;
121 [[nodiscard]] std::optional<TimeInfo> time_info(std::uint32_t key) const;
122 [[nodiscard]] std::optional<TimeState> time_state(std::uint32_t key) const;
123 [[nodiscard]] std::optional<DateTimeInfo> datetime_info(std::uint32_t key) const;
124 [[nodiscard]] std::optional<DateTimeState> datetime_state(std::uint32_t key) const;
125 [[nodiscard]] std::optional<ValveInfo> valve_info(std::uint32_t key) const;
126 [[nodiscard]] std::optional<ValveState> valve_state(std::uint32_t key) const;
127 [[nodiscard]] std::optional<EventInfo> event_info(std::uint32_t key) const;
128 [[nodiscard]] std::optional<EventState> event_state(std::uint32_t key) const;
129 [[nodiscard]] std::optional<UpdateInfo> update_info(std::uint32_t key) const;
130 [[nodiscard]] std::optional<UpdateState> update_state(std::uint32_t key) const;
131 [[nodiscard]] std::optional<WaterHeaterInfo> water_heater_info(std::uint32_t key) const;
132 [[nodiscard]] std::optional<WaterHeaterState> water_heater_state(std::uint32_t key) const;
133 [[nodiscard]] std::optional<InfraredInfo> infrared_info(std::uint32_t key) const;
134 [[nodiscard]] std::optional<RadioFrequencyInfo> radio_frequency_info(std::uint32_t key) const;
135
136private:
137 std::unordered_map<std::uint32_t, StoredEntity> entities_;
138 StateCallback state_cb_;
139};
140
141} // namespace esphome::api
Typed AlarmControlPanel entity (info + state + command).
Typed BinarySensor entity (info + state).
Typed Button entity (info + command).
Typed Camera entity (info only; images are streamed separately).
Collects a device's entity descriptions and live states.
Definition entity_store.hpp:60
std::optional< BinarySensorInfo > binary_sensor_info(std::uint32_t key) const
std::optional< SelectInfo > select_info(std::uint32_t key) const
std::size_t size() const noexcept
Definition entity_store.hpp:77
std::optional< WaterHeaterState > water_heater_state(std::uint32_t key) const
std::optional< AlarmControlPanelInfo > alarm_control_panel_info(std::uint32_t key) const
std::optional< DateTimeInfo > datetime_info(std::uint32_t key) const
std::optional< LockEntityState > lock_state(std::uint32_t key) const
std::optional< ClimateState > climate_state(std::uint32_t key) const
std::optional< LightInfo > light_info(std::uint32_t key) const
std::optional< TimeInfo > time_info(std::uint32_t key) const
std::optional< CoverInfo > cover_info(std::uint32_t key) const
void on_state(StateCallback cb)
Fired after each state update.
Definition entity_store.hpp:73
std::optional< TextState > text_state(std::uint32_t key) const
std::optional< SirenInfo > siren_info(std::uint32_t key) const
std::optional< MediaPlayerStatus > media_player_state(std::uint32_t key) const
std::optional< CameraInfo > camera_info(std::uint32_t key) const
std::optional< AlarmControlPanelStatus > alarm_control_panel_state(std::uint32_t key) const
std::optional< EventInfo > event_info(std::uint32_t key) const
std::optional< DateTimeState > datetime_state(std::uint32_t key) const
std::optional< RadioFrequencyInfo > radio_frequency_info(std::uint32_t key) const
std::optional< LockInfo > lock_info(std::uint32_t key) const
bool ingest(const ProtoMessage &msg)
Ingest one decoded message.
std::optional< ClimateInfo > climate_info(std::uint32_t key) const
std::optional< MediaPlayerInfo > media_player_info(std::uint32_t key) const
std::optional< EventState > event_state(std::uint32_t key) const
const StoredEntity * find_by_object_id(const std::string &object_id) const
std::optional< CoverState > cover_state(std::uint32_t key) const
std::optional< FanState > fan_state(std::uint32_t key) const
std::optional< WaterHeaterInfo > water_heater_info(std::uint32_t key) const
std::optional< SwitchInfo > switch_info(std::uint32_t key) const
std::optional< ValveInfo > valve_info(std::uint32_t key) const
std::optional< SensorState > sensor_state(std::uint32_t key) const
std::optional< UpdateInfo > update_info(std::uint32_t key) const
std::optional< DateInfo > date_info(std::uint32_t key) const
std::optional< SensorInfo > sensor_info(std::uint32_t key) const
std::optional< TimeState > time_state(std::uint32_t key) const
std::optional< DateState > date_state(std::uint32_t key) const
std::optional< TextInfo > text_info(std::uint32_t key) const
std::vector< const StoredEntity * > entities() const
All tracked entities (unordered).
std::optional< NumberState > number_state(std::uint32_t key) const
std::optional< ButtonInfo > button_info(std::uint32_t key) const
const StoredEntity * find(std::uint32_t key) const
std::optional< TextSensorState > text_sensor_state(std::uint32_t key) const
std::optional< ValveState > valve_state(std::uint32_t key) const
std::optional< LightState > light_state(std::uint32_t key) const
std::optional< SwitchState > switch_state(std::uint32_t key) const
std::optional< TextSensorInfo > text_sensor_info(std::uint32_t key) const
std::optional< NumberInfo > number_info(std::uint32_t key) const
std::optional< UpdateState > update_state(std::uint32_t key) const
std::function< void(EntityType type, std::uint32_t key)> StateCallback
Definition entity_store.hpp:62
std::optional< SelectState > select_state(std::uint32_t key) const
std::optional< BinarySensorState > binary_sensor_state(std::uint32_t key) const
std::optional< SirenState > siren_state(std::uint32_t key) const
std::optional< InfraredInfo > infrared_info(std::uint32_t key) const
std::optional< FanInfo > fan_info(std::uint32_t key) const
void clear()
Remove all tracked entities.
Base class every generated message derives from.
Definition proto_message.hpp:23
Typed Climate entity (info + state + command).
Typed Cover entity (info + state + command).
Typed Date entity (info + state + command).
Typed DateTime entity (info + state + command).
Common entity metadata shared by every entity domain.
Enumeration of ESPHome entity domains and name conversion helpers.
Typed Event entity (info + state).
Typed Fan entity (info + state + command).
Typed Infrared entity (info only).
Typed Light entity (info + state + command).
Typed Lock entity (info + state + command).
Typed MediaPlayer entity (info + state + command).
Definition bytes.hpp:10
EntityType
One per ESPHome entity domain (the <X> in ListEntities<X>Response).
Definition entity_type.hpp:11
Typed Number entity (info + state + command).
Lightweight base class for every generated ESPHome API message.
Typed RadioFrequency entity (info only).
Typed Select entity (info + state + command).
Typed Sensor entity (info + state).
Typed Siren entity (info + state + command).
A single tracked entity: its domain, identity, and the latest raw info/ state protobuf messages (deco...
Definition entity_store.hpp:49
std::string object_id
Definition entity_store.hpp:52
std::shared_ptr< ProtoMessage > state
Definition entity_store.hpp:55
std::shared_ptr< ProtoMessage > info
Definition entity_store.hpp:54
std::string name
Definition entity_store.hpp:53
std::uint32_t key
Definition entity_store.hpp:51
EntityType type
Definition entity_store.hpp:50
Typed Switch entity (info + state + command).
Typed Text entity (info + state + command).
Typed TextSensor entity (info + state).
Typed Time entity (info + state + command).
Typed Update entity (info + state + command).
Typed Valve entity (info + state + command).
Typed WaterHeater entity (info + state + command).