mirror of
https://github.com/esphome/esphome.git
synced 2025-07-29 06:36:45 +00:00
[api] Add new flag to request state/attribute once from HA only (#7258)
This commit is contained in:
parent
ab51bbd8f7
commit
2b25daa199
@ -686,6 +686,7 @@ message SubscribeHomeAssistantStateResponse {
|
|||||||
option (source) = SOURCE_SERVER;
|
option (source) = SOURCE_SERVER;
|
||||||
string entity_id = 1;
|
string entity_id = 1;
|
||||||
string attribute = 2;
|
string attribute = 2;
|
||||||
|
bool once = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
message HomeAssistantStateResponse {
|
message HomeAssistantStateResponse {
|
||||||
|
@ -3109,6 +3109,16 @@ void SubscribeHomeAssistantStatesRequest::dump_to(std::string &out) const {
|
|||||||
out.append("SubscribeHomeAssistantStatesRequest {}");
|
out.append("SubscribeHomeAssistantStatesRequest {}");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
bool SubscribeHomeAssistantStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
|
||||||
|
switch (field_id) {
|
||||||
|
case 3: {
|
||||||
|
this->once = value.as_bool();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
bool SubscribeHomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
|
bool SubscribeHomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
|
||||||
switch (field_id) {
|
switch (field_id) {
|
||||||
case 1: {
|
case 1: {
|
||||||
@ -3126,6 +3136,7 @@ bool SubscribeHomeAssistantStateResponse::decode_length(uint32_t field_id, Proto
|
|||||||
void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const {
|
void SubscribeHomeAssistantStateResponse::encode(ProtoWriteBuffer buffer) const {
|
||||||
buffer.encode_string(1, this->entity_id);
|
buffer.encode_string(1, this->entity_id);
|
||||||
buffer.encode_string(2, this->attribute);
|
buffer.encode_string(2, this->attribute);
|
||||||
|
buffer.encode_bool(3, this->once);
|
||||||
}
|
}
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
void SubscribeHomeAssistantStateResponse::dump_to(std::string &out) const {
|
void SubscribeHomeAssistantStateResponse::dump_to(std::string &out) const {
|
||||||
@ -3138,6 +3149,10 @@ void SubscribeHomeAssistantStateResponse::dump_to(std::string &out) const {
|
|||||||
out.append(" attribute: ");
|
out.append(" attribute: ");
|
||||||
out.append("'").append(this->attribute).append("'");
|
out.append("'").append(this->attribute).append("'");
|
||||||
out.append("\n");
|
out.append("\n");
|
||||||
|
|
||||||
|
out.append(" once: ");
|
||||||
|
out.append(YESNO(this->once));
|
||||||
|
out.append("\n");
|
||||||
out.append("}");
|
out.append("}");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -836,6 +836,7 @@ class SubscribeHomeAssistantStateResponse : public ProtoMessage {
|
|||||||
public:
|
public:
|
||||||
std::string entity_id{};
|
std::string entity_id{};
|
||||||
std::string attribute{};
|
std::string attribute{};
|
||||||
|
bool once{false};
|
||||||
void encode(ProtoWriteBuffer buffer) const override;
|
void encode(ProtoWriteBuffer buffer) const override;
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
void dump_to(std::string &out) const override;
|
void dump_to(std::string &out) const override;
|
||||||
@ -843,6 +844,7 @@ class SubscribeHomeAssistantStateResponse : public ProtoMessage {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||||
|
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||||
};
|
};
|
||||||
class HomeAssistantStateResponse : public ProtoMessage {
|
class HomeAssistantStateResponse : public ProtoMessage {
|
||||||
public:
|
public:
|
||||||
|
@ -359,8 +359,18 @@ void APIServer::subscribe_home_assistant_state(std::string entity_id, optional<s
|
|||||||
.entity_id = std::move(entity_id),
|
.entity_id = std::move(entity_id),
|
||||||
.attribute = std::move(attribute),
|
.attribute = std::move(attribute),
|
||||||
.callback = std::move(f),
|
.callback = std::move(f),
|
||||||
|
.once = false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
void APIServer::get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
|
||||||
|
std::function<void(std::string)> f) {
|
||||||
|
this->state_subs_.push_back(HomeAssistantStateSubscription{
|
||||||
|
.entity_id = std::move(entity_id),
|
||||||
|
.attribute = std::move(attribute),
|
||||||
|
.callback = std::move(f),
|
||||||
|
.once = true,
|
||||||
|
});
|
||||||
|
};
|
||||||
const std::vector<APIServer::HomeAssistantStateSubscription> &APIServer::get_state_subs() const {
|
const std::vector<APIServer::HomeAssistantStateSubscription> &APIServer::get_state_subs() const {
|
||||||
return this->state_subs_;
|
return this->state_subs_;
|
||||||
}
|
}
|
||||||
|
@ -112,10 +112,13 @@ class APIServer : public Component, public Controller {
|
|||||||
std::string entity_id;
|
std::string entity_id;
|
||||||
optional<std::string> attribute;
|
optional<std::string> attribute;
|
||||||
std::function<void(std::string)> callback;
|
std::function<void(std::string)> callback;
|
||||||
|
bool once;
|
||||||
};
|
};
|
||||||
|
|
||||||
void subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
|
void subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
|
||||||
std::function<void(std::string)> f);
|
std::function<void(std::string)> f);
|
||||||
|
void get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
|
||||||
|
std::function<void(std::string)> f);
|
||||||
const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
|
const std::vector<HomeAssistantStateSubscription> &get_state_subs() const;
|
||||||
const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
|
const std::vector<UserServiceDescriptor *> &get_user_services() const { return this->user_services_; }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user