mirror of
https://github.com/espressif/esp-matter.git
synced 2026-04-27 19:13:13 +00:00
42075d5c75
- data_model/legacy/: moved old data model to this folder - data_model/generated/: contain the automatically generated data model - tools/data_model_gen: contains the script to generate the data model
56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
# Copyright 2026 Espressif Systems (Shanghai) PTE LTD
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
|
|
def convert_to_int(value, default=None):
|
|
"""Convert a string or int value to an integer. Handles hex (0x...) and decimal strings."""
|
|
if value is None:
|
|
return default
|
|
try:
|
|
if isinstance(value, int):
|
|
return value
|
|
if isinstance(value, str):
|
|
return int(value, 16) if value.startswith("0x") else int(value)
|
|
return default
|
|
except ValueError:
|
|
return default
|
|
|
|
|
|
def hex_to_int(value):
|
|
"""Convert a hex string (or list of hex strings) to integer(s)."""
|
|
if isinstance(value, list):
|
|
return [hex_to_int(v) for v in value]
|
|
if isinstance(value, int):
|
|
return value
|
|
if isinstance(value, str):
|
|
return int(value, 16)
|
|
return value
|
|
|
|
|
|
def is_hex_value(value):
|
|
"""Check if a value is a valid hex value e.g. 0x0001"""
|
|
try:
|
|
int(value, 16)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
def format_hex_value(hex_value):
|
|
"""Format a hex value by removing unnecessary leading zeros e.g. 0x00000001 -> 0x0001"""
|
|
if hex_value and hex_value.startswith("0x"):
|
|
int_value = int(hex_value, 16)
|
|
return f"0x{int_value:04X}"
|
|
return hex_value
|