unified way how all platforms handle defaults

This commit is contained in:
Tomasz Duda 2024-11-03 09:28:34 +01:00
parent 77bb46ff3b
commit b01e224d84

View file

@ -1644,100 +1644,40 @@ def _get_priority_default(*args):
class SplitDefault(Optional): class SplitDefault(Optional):
"""Mark this key to have a split default for ESP8266/ESP32.""" """Mark this key to have a split default for ESP8266/ESP32."""
def __init__( def __init__(self, key, **kwargs):
self,
key,
esp8266=vol.UNDEFINED,
esp32=vol.UNDEFINED,
esp32_arduino=vol.UNDEFINED,
esp32_idf=vol.UNDEFINED,
esp32_s2=vol.UNDEFINED,
esp32_s2_arduino=vol.UNDEFINED,
esp32_s2_idf=vol.UNDEFINED,
esp32_s3=vol.UNDEFINED,
esp32_s3_arduino=vol.UNDEFINED,
esp32_s3_idf=vol.UNDEFINED,
esp32_c3=vol.UNDEFINED,
esp32_c3_arduino=vol.UNDEFINED,
esp32_c3_idf=vol.UNDEFINED,
rp2040=vol.UNDEFINED,
bk72xx=vol.UNDEFINED,
rtl87xx=vol.UNDEFINED,
host=vol.UNDEFINED,
):
super().__init__(key) super().__init__(key)
self._esp8266_default = vol.default_factory(esp8266) self._defaults = {}
self._esp32_arduino_default = vol.default_factory( priority_mapping = {
_get_priority_default(esp32_arduino, esp32) "esp32_arduino": ["esp32"],
) "esp32_idf": ["esp32"],
self._esp32_idf_default = vol.default_factory( "esp32_s2_arduino": ["esp32_s2", "esp32_arduino", "esp32"],
_get_priority_default(esp32_idf, esp32) "esp32_s2_idf": ["esp32_s2", "esp32_idf", "esp32"],
) "esp32_s3_arduino": ["esp32_s3", "esp32_arduino", "esp32"],
self._esp32_s2_arduino_default = vol.default_factory( "esp32_s3_idf": ["esp32_s3", "esp32_idf", "esp32"],
_get_priority_default(esp32_s2_arduino, esp32_s2, esp32_arduino, esp32) "esp32_c3_arduino": ["esp32_c3", "esp32_arduino", "esp32"],
) "esp32_c3_idf": ["esp32_c3", "esp32_idf", "esp32"],
self._esp32_s2_idf_default = vol.default_factory( }
_get_priority_default(esp32_s2_idf, esp32_s2, esp32_idf, esp32) for platform_key, value in kwargs.items():
) if platform_key in priority_mapping:
self._esp32_s3_arduino_default = vol.default_factory( prioritized_default = _get_priority_default(
_get_priority_default(esp32_s3_arduino, esp32_s3, esp32_arduino, esp32) value, *[kwargs.get(p) for p in priority_mapping[platform_key]]
) )
self._esp32_s3_idf_default = vol.default_factory( self._defaults[platform_key] = vol.default_factory(prioritized_default)
_get_priority_default(esp32_s3_idf, esp32_s3, esp32_idf, esp32) else:
) self._defaults[platform_key] = vol.default_factory(value)
self._esp32_c3_arduino_default = vol.default_factory(
_get_priority_default(esp32_c3_arduino, esp32_c3, esp32_arduino, esp32)
)
self._esp32_c3_idf_default = vol.default_factory(
_get_priority_default(esp32_c3_idf, esp32_c3, esp32_idf, esp32)
)
self._rp2040_default = vol.default_factory(rp2040)
self._bk72xx_default = vol.default_factory(bk72xx)
self._rtl87xx_default = vol.default_factory(rtl87xx)
self._host_default = vol.default_factory(host)
@property @property
def default(self): def default(self):
if CORE.is_esp8266: key = [CORE.target_platform]
return self._esp8266_default
if CORE.is_esp32: if CORE.is_esp32:
from esphome.components.esp32 import get_esp32_variant from esphome.components.esp32 import get_esp32_variant
from esphome.components.esp32.const import ( from esphome.components.esp32.const import VARIANT_ESP32
VARIANT_ESP32C3,
VARIANT_ESP32S2,
VARIANT_ESP32S3,
)
variant = get_esp32_variant() variant = get_esp32_variant().replace(VARIANT_ESP32, "").lower()
if variant == VARIANT_ESP32S2: if variant:
if CORE.using_arduino: key += [variant]
return self._esp32_s2_arduino_default key += [CORE.target_framework.replace("esp-", "")]
if CORE.using_esp_idf: return self._defaults.get("_".join(key), vol.default_factory(vol.UNDEFINED))
return self._esp32_s2_idf_default
elif variant == VARIANT_ESP32S3:
if CORE.using_arduino:
return self._esp32_s3_arduino_default
if CORE.using_esp_idf:
return self._esp32_s3_idf_default
elif variant == VARIANT_ESP32C3:
if CORE.using_arduino:
return self._esp32_c3_arduino_default
if CORE.using_esp_idf:
return self._esp32_c3_idf_default
else:
if CORE.using_arduino:
return self._esp32_arduino_default
if CORE.using_esp_idf:
return self._esp32_idf_default
if CORE.is_rp2040:
return self._rp2040_default
if CORE.is_bk72xx:
return self._bk72xx_default
if CORE.is_rtl87xx:
return self._rtl87xx_default
if CORE.is_host:
return self._host_default
raise NotImplementedError
@default.setter @default.setter
def default(self, value): def default(self, value):