mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	[api] Add timezone support to GetTimeResponse for automatic timezone synchronization (#10661)
This commit is contained in:
		| @@ -818,6 +818,7 @@ message GetTimeResponse { | |||||||
|   option (no_delay) = true; |   option (no_delay) = true; | ||||||
|  |  | ||||||
|   fixed32 epoch_seconds = 1; |   fixed32 epoch_seconds = 1; | ||||||
|  |   string timezone = 2; | ||||||
| } | } | ||||||
|  |  | ||||||
| // ==================== USER-DEFINES SERVICES ==================== | // ==================== USER-DEFINES SERVICES ==================== | ||||||
|   | |||||||
| @@ -1070,8 +1070,14 @@ void APIConnection::camera_image(const CameraImageRequest &msg) { | |||||||
|  |  | ||||||
| #ifdef USE_HOMEASSISTANT_TIME | #ifdef USE_HOMEASSISTANT_TIME | ||||||
| void APIConnection::on_get_time_response(const GetTimeResponse &value) { | void APIConnection::on_get_time_response(const GetTimeResponse &value) { | ||||||
|   if (homeassistant::global_homeassistant_time != nullptr) |   if (homeassistant::global_homeassistant_time != nullptr) { | ||||||
|     homeassistant::global_homeassistant_time->set_epoch_time(value.epoch_seconds); |     homeassistant::global_homeassistant_time->set_epoch_time(value.epoch_seconds); | ||||||
|  | #ifdef USE_TIME_TIMEZONE | ||||||
|  |     if (!value.timezone.empty() && value.timezone != homeassistant::global_homeassistant_time->get_timezone()) { | ||||||
|  |       homeassistant::global_homeassistant_time->set_timezone(value.timezone); | ||||||
|  |     } | ||||||
|  | #endif | ||||||
|  |   } | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|   | |||||||
| @@ -901,6 +901,16 @@ bool HomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDel | |||||||
|   return true; |   return true; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  | bool GetTimeResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) { | ||||||
|  |   switch (field_id) { | ||||||
|  |     case 2: | ||||||
|  |       this->timezone = value.as_string(); | ||||||
|  |       break; | ||||||
|  |     default: | ||||||
|  |       return false; | ||||||
|  |   } | ||||||
|  |   return true; | ||||||
|  | } | ||||||
| bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { | bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { | ||||||
|   switch (field_id) { |   switch (field_id) { | ||||||
|     case 1: |     case 1: | ||||||
| @@ -911,8 +921,14 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { | |||||||
|   } |   } | ||||||
|   return true; |   return true; | ||||||
| } | } | ||||||
| void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); } | void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { | ||||||
| void GetTimeResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->epoch_seconds); } |   buffer.encode_fixed32(1, this->epoch_seconds); | ||||||
|  |   buffer.encode_string(2, this->timezone_ref_); | ||||||
|  | } | ||||||
|  | void GetTimeResponse::calculate_size(ProtoSize &size) const { | ||||||
|  |   size.add_fixed32(1, this->epoch_seconds); | ||||||
|  |   size.add_length(1, this->timezone_ref_.size()); | ||||||
|  | } | ||||||
| #ifdef USE_API_SERVICES | #ifdef USE_API_SERVICES | ||||||
| void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { | void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const { | ||||||
|   buffer.encode_string(1, this->name_ref_); |   buffer.encode_string(1, this->name_ref_); | ||||||
|   | |||||||
| @@ -1174,11 +1174,14 @@ class GetTimeRequest final : public ProtoMessage { | |||||||
| class GetTimeResponse final : public ProtoDecodableMessage { | class GetTimeResponse final : public ProtoDecodableMessage { | ||||||
|  public: |  public: | ||||||
|   static constexpr uint8_t MESSAGE_TYPE = 37; |   static constexpr uint8_t MESSAGE_TYPE = 37; | ||||||
|   static constexpr uint8_t ESTIMATED_SIZE = 5; |   static constexpr uint8_t ESTIMATED_SIZE = 14; | ||||||
| #ifdef HAS_PROTO_MESSAGE_DUMP | #ifdef HAS_PROTO_MESSAGE_DUMP | ||||||
|   const char *message_name() const override { return "get_time_response"; } |   const char *message_name() const override { return "get_time_response"; } | ||||||
| #endif | #endif | ||||||
|   uint32_t epoch_seconds{0}; |   uint32_t epoch_seconds{0}; | ||||||
|  |   std::string timezone{}; | ||||||
|  |   StringRef timezone_ref_{}; | ||||||
|  |   void set_timezone(const StringRef &ref) { this->timezone_ref_ = ref; } | ||||||
|   void encode(ProtoWriteBuffer buffer) const override; |   void encode(ProtoWriteBuffer buffer) const override; | ||||||
|   void calculate_size(ProtoSize &size) const override; |   void calculate_size(ProtoSize &size) const override; | ||||||
| #ifdef HAS_PROTO_MESSAGE_DUMP | #ifdef HAS_PROTO_MESSAGE_DUMP | ||||||
| @@ -1187,6 +1190,7 @@ class GetTimeResponse final : public ProtoDecodableMessage { | |||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   bool decode_32bit(uint32_t field_id, Proto32Bit value) override; |   bool decode_32bit(uint32_t field_id, Proto32Bit value) override; | ||||||
|  |   bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override; | ||||||
| }; | }; | ||||||
| #ifdef USE_API_SERVICES | #ifdef USE_API_SERVICES | ||||||
| class ListEntitiesServicesArgument final : public ProtoMessage { | class ListEntitiesServicesArgument final : public ProtoMessage { | ||||||
|   | |||||||
| @@ -1110,7 +1110,17 @@ void HomeAssistantStateResponse::dump_to(std::string &out) const { | |||||||
| } | } | ||||||
| #endif | #endif | ||||||
| void GetTimeRequest::dump_to(std::string &out) const { out.append("GetTimeRequest {}"); } | void GetTimeRequest::dump_to(std::string &out) const { out.append("GetTimeRequest {}"); } | ||||||
| void GetTimeResponse::dump_to(std::string &out) const { dump_field(out, "epoch_seconds", this->epoch_seconds); } | void GetTimeResponse::dump_to(std::string &out) const { | ||||||
|  |   MessageDumpHelper helper(out, "GetTimeResponse"); | ||||||
|  |   dump_field(out, "epoch_seconds", this->epoch_seconds); | ||||||
|  |   out.append("  timezone: "); | ||||||
|  |   if (!this->timezone_ref_.empty()) { | ||||||
|  |     out.append("'").append(this->timezone_ref_.c_str()).append("'"); | ||||||
|  |   } else { | ||||||
|  |     out.append("'").append(this->timezone).append("'"); | ||||||
|  |   } | ||||||
|  |   out.append("\n"); | ||||||
|  | } | ||||||
| #ifdef USE_API_SERVICES | #ifdef USE_API_SERVICES | ||||||
| void ListEntitiesServicesArgument::dump_to(std::string &out) const { | void ListEntitiesServicesArgument::dump_to(std::string &out) const { | ||||||
|   MessageDumpHelper helper(out, "ListEntitiesServicesArgument"); |   MessageDumpHelper helper(out, "ListEntitiesServicesArgument"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user