mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Fixes for setup of OpenThread either using TLV or entering Credentials directly (#9157)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		| @@ -46,7 +46,7 @@ def set_sdkconfig_options(config): | |||||||
|     add_idf_sdkconfig_option("CONFIG_OPENTHREAD_NETWORK_PANID", config[CONF_PAN_ID]) |     add_idf_sdkconfig_option("CONFIG_OPENTHREAD_NETWORK_PANID", config[CONF_PAN_ID]) | ||||||
|     add_idf_sdkconfig_option("CONFIG_OPENTHREAD_NETWORK_CHANNEL", config[CONF_CHANNEL]) |     add_idf_sdkconfig_option("CONFIG_OPENTHREAD_NETWORK_CHANNEL", config[CONF_CHANNEL]) | ||||||
|     add_idf_sdkconfig_option( |     add_idf_sdkconfig_option( | ||||||
|         "CONFIG_OPENTHREAD_NETWORK_MASTERKEY", f"{config[CONF_NETWORK_KEY]:X}" |         "CONFIG_OPENTHREAD_NETWORK_MASTERKEY", f"{config[CONF_NETWORK_KEY]:X}".lower() | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     if network_name := config.get(CONF_NETWORK_NAME): |     if network_name := config.get(CONF_NETWORK_NAME): | ||||||
| @@ -54,14 +54,14 @@ def set_sdkconfig_options(config): | |||||||
|  |  | ||||||
|     if (ext_pan_id := config.get(CONF_EXT_PAN_ID)) is not None: |     if (ext_pan_id := config.get(CONF_EXT_PAN_ID)) is not None: | ||||||
|         add_idf_sdkconfig_option( |         add_idf_sdkconfig_option( | ||||||
|             "CONFIG_OPENTHREAD_NETWORK_EXTPANID", f"{ext_pan_id:X}" |             "CONFIG_OPENTHREAD_NETWORK_EXTPANID", f"{ext_pan_id:X}".lower() | ||||||
|         ) |         ) | ||||||
|     if (mesh_local_prefix := config.get(CONF_MESH_LOCAL_PREFIX)) is not None: |     if (mesh_local_prefix := config.get(CONF_MESH_LOCAL_PREFIX)) is not None: | ||||||
|         add_idf_sdkconfig_option( |         add_idf_sdkconfig_option( | ||||||
|             "CONFIG_OPENTHREAD_MESH_LOCAL_PREFIX", f"{mesh_local_prefix:X}" |             "CONFIG_OPENTHREAD_MESH_LOCAL_PREFIX", f"{mesh_local_prefix}".lower() | ||||||
|         ) |         ) | ||||||
|     if (pskc := config.get(CONF_PSKC)) is not None: |     if (pskc := config.get(CONF_PSKC)) is not None: | ||||||
|         add_idf_sdkconfig_option("CONFIG_OPENTHREAD_NETWORK_PSKC", f"{pskc:X}") |         add_idf_sdkconfig_option("CONFIG_OPENTHREAD_NETWORK_PSKC", f"{pskc:X}".lower()) | ||||||
|  |  | ||||||
|     if CONF_FORCE_DATASET in config: |     if CONF_FORCE_DATASET in config: | ||||||
|         if config[CONF_FORCE_DATASET]: |         if config[CONF_FORCE_DATASET]: | ||||||
| @@ -98,7 +98,7 @@ _CONNECTION_SCHEMA = cv.Schema( | |||||||
|         cv.Optional(CONF_EXT_PAN_ID): cv.hex_int, |         cv.Optional(CONF_EXT_PAN_ID): cv.hex_int, | ||||||
|         cv.Optional(CONF_NETWORK_NAME): cv.string_strict, |         cv.Optional(CONF_NETWORK_NAME): cv.string_strict, | ||||||
|         cv.Optional(CONF_PSKC): cv.hex_int, |         cv.Optional(CONF_PSKC): cv.hex_int, | ||||||
|         cv.Optional(CONF_MESH_LOCAL_PREFIX): cv.hex_int, |         cv.Optional(CONF_MESH_LOCAL_PREFIX): cv.ipv6network, | ||||||
|     } |     } | ||||||
| ) | ) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -137,7 +137,7 @@ void OpenThreadSrpComponent::setup() { | |||||||
|   // Copy the mdns services to our local instance so that the c_str pointers remain valid for the lifetime of this |   // Copy the mdns services to our local instance so that the c_str pointers remain valid for the lifetime of this | ||||||
|   // component |   // component | ||||||
|   this->mdns_services_ = this->mdns_->get_services(); |   this->mdns_services_ = this->mdns_->get_services(); | ||||||
|   ESP_LOGW(TAG, "Setting up SRP services. count = %d\n", this->mdns_services_.size()); |   ESP_LOGD(TAG, "Setting up SRP services. count = %d\n", this->mdns_services_.size()); | ||||||
|   for (const auto &service : this->mdns_services_) { |   for (const auto &service : this->mdns_services_) { | ||||||
|     otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance); |     otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance); | ||||||
|     if (!entry) { |     if (!entry) { | ||||||
| @@ -185,11 +185,11 @@ void OpenThreadSrpComponent::setup() { | |||||||
|     if (error != OT_ERROR_NONE) { |     if (error != OT_ERROR_NONE) { | ||||||
|       ESP_LOGW(TAG, "Failed to add service: %s", otThreadErrorToString(error)); |       ESP_LOGW(TAG, "Failed to add service: %s", otThreadErrorToString(error)); | ||||||
|     } |     } | ||||||
|     ESP_LOGW(TAG, "Added service: %s", full_service.c_str()); |     ESP_LOGD(TAG, "Added service: %s", full_service.c_str()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   otSrpClientEnableAutoStartMode(instance, srp_start_callback, nullptr); |   otSrpClientEnableAutoStartMode(instance, srp_start_callback, nullptr); | ||||||
|   ESP_LOGW(TAG, "Finished SRP setup"); |   ESP_LOGD(TAG, "Finished SRP setup"); | ||||||
| } | } | ||||||
|  |  | ||||||
| void *OpenThreadSrpComponent::pool_alloc_(size_t size) { | void *OpenThreadSrpComponent::pool_alloc_(size_t size) { | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| # Sourced from https://gist.github.com/agners/0338576e0003318b63ec1ea75adc90f9 | # Sourced from https://gist.github.com/agners/0338576e0003318b63ec1ea75adc90f9 | ||||||
| import binascii | import binascii | ||||||
|  | import ipaddress | ||||||
|  |  | ||||||
| from esphome.const import CONF_CHANNEL | from esphome.const import CONF_CHANNEL | ||||||
|  |  | ||||||
| @@ -37,6 +38,12 @@ def parse_tlv(tlv) -> dict: | |||||||
|         if tag in TLV_TYPES: |         if tag in TLV_TYPES: | ||||||
|             if tag == 3: |             if tag == 3: | ||||||
|                 output[TLV_TYPES[tag]] = val.decode("utf-8") |                 output[TLV_TYPES[tag]] = val.decode("utf-8") | ||||||
|  |             elif tag == 7: | ||||||
|  |                 mesh_local_prefix = binascii.hexlify(val).decode("utf-8") | ||||||
|  |                 mesh_local_prefix_str = f"{mesh_local_prefix}0000000000000000" | ||||||
|  |                 ipv6_bytes = bytes.fromhex(mesh_local_prefix_str) | ||||||
|  |                 ipv6_address = ipaddress.IPv6Address(ipv6_bytes) | ||||||
|  |                 output[TLV_TYPES[tag]] = f"{ipv6_address}/64" | ||||||
|             else: |             else: | ||||||
|                 output[TLV_TYPES[tag]] = int.from_bytes(val) |                 output[TLV_TYPES[tag]] = int.from_bytes(val) | ||||||
|     return output |     return output | ||||||
|   | |||||||
| @@ -8,4 +8,6 @@ openthread: | |||||||
|   pan_id: 0x8f28 |   pan_id: 0x8f28 | ||||||
|   ext_pan_id: 0xd63e8e3e495ebbc3 |   ext_pan_id: 0xd63e8e3e495ebbc3 | ||||||
|   pskc: 0xc23a76e98f1a6483639b1ac1271e2e27 |   pskc: 0xc23a76e98f1a6483639b1ac1271e2e27 | ||||||
|  |   mesh_local_prefix: fd53:145f:ed22:ad81::/64 | ||||||
|   force_dataset: true |   force_dataset: true | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user