use fv.full_config.get().data for final validate

This commit is contained in:
oarcher 2024-08-12 22:51:05 +02:00
parent 685deedfce
commit 0b4d2cbe07
3 changed files with 20 additions and 22 deletions

View file

@ -51,6 +51,9 @@ MODEM_MODELS_POWER = {
MODEM_MODELS_POWER["SIM7670"] = MODEM_MODELS_POWER["SIM7600"]
KEY_MODEM_MODEL = "modem_model"
KEY_MODEM_CMUX = "modem_cmux"
modem_ns = cg.esphome_ns.namespace("modem")
ModemComponent = modem_ns.class_("ModemComponent", cg.Component)
ModemComponentState = modem_ns.enum("ModemComponentState")
@ -106,17 +109,17 @@ CONFIG_SCHEMA = cv.All(
def final_validate_platform(config):
if modem_config := fv.full_config.get().get(CONF_MODEM, None):
if not modem_config[CONF_ENABLE_CMUX]:
raise cv.Invalid(
f"'{CONF_MODEM}' platform require '{CONF_ENABLE_CMUX}' to be 'true'."
)
if not fv.full_config.get().data.get(KEY_MODEM_CMUX, None):
raise cv.Invalid(
f"'{CONF_MODEM}' platform require '{CONF_ENABLE_CMUX}' to be 'true'."
)
return config
def _final_validate(config):
full_config = fv.full_config.get()
# uncomment after PR#4091 merged
# if wifi_config := fv.full_config.get().get(CONF_WIFI, None):
# if wifi_config := full_config.get(CONF_WIFI, None):
# if wifi_has_sta(wifi_config):
# raise cv.Invalid("Wifi must be AP only when using ethernet")
if config.get(CONF_POWER_PIN, None):
@ -124,6 +127,8 @@ def _final_validate(config):
raise cv.Invalid(
f"Modem model '{config[CONF_MODEL]}' has no power power specs."
)
full_config.data[KEY_MODEM_MODEL] = config[CONF_MODEL]
full_config.data[KEY_MODEM_CMUX] = config[CONF_ENABLE_CMUX]
FINAL_VALIDATE_SCHEMA = _final_validate

View file

@ -8,7 +8,6 @@ from esphome.const import (
CONF_ID,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_PLATFORM,
CONF_SPEED,
DEVICE_CLASS_SIGNAL_STRENGTH,
ENTITY_CATEGORY_DIAGNOSTIC,
@ -21,7 +20,7 @@ from esphome.const import (
)
import esphome.final_validate as fv
from .. import CONF_MODEM, final_validate_platform, modem_ns, switch
from .. import final_validate_platform, modem_ns, switch
CODEOWNERS = ["@oarcher"]
@ -109,16 +108,7 @@ CONFIG_SCHEMA = cv.All(
def _final_validate_gnss(config):
# GNSS sensors needs GNSS switch
if config.get(CONF_LATITUDE, None) or config.get(CONF_LONGITUDE, None):
gnss = False
if switches := fv.full_config.get().get("switch", None):
modem_switches = filter(
lambda x: x.get(CONF_PLATFORM, None) and x[CONF_PLATFORM] == CONF_MODEM,
switches,
)
for sw in modem_switches:
if switch.CONF_GNSS in sw:
gnss = True
if not gnss:
if not fv.full_config.get().data.get(switch.KEY_MODEM_GNSS, None):
raise cv.Invalid("Using GNSS modem sensors require GNSS modem switch.")
return config

View file

@ -4,7 +4,7 @@ import esphome.config_validation as cv
from esphome.const import DEVICE_CLASS_SWITCH
import esphome.final_validate as fv
from .. import CONF_MODEL, CONF_MODEM, final_validate_platform, modem_ns
from .. import KEY_MODEM_MODEL, final_validate_platform, modem_ns
CODEOWNERS = ["@oarcher"]
@ -15,7 +15,9 @@ DEPENDENCIES = ["modem"]
IS_PLATFORM_COMPONENT = True
CONF_GNSS = "gnss"
CONF_GNSS_COMMAND = "gnss_command"
CONF_GNSS_COMMAND = "gnss_command" # will be set by _final_validate_gnss
KEY_MODEM_GNSS = "modem_gnss"
ICON_SATELLITE = "mdi:satellite-variant"
@ -38,11 +40,12 @@ CONFIG_SCHEMA = cv.Schema(
def _final_validate_gnss(config):
if config.get(CONF_GNSS, None):
modem_config = fv.full_config.get().get(CONF_MODEM)
modem_model = modem_config.get(CONF_MODEL, None)
full_config = fv.full_config.get()
modem_model = full_config.data.get(KEY_MODEM_MODEL, None)
if modem_model not in MODEM_MODELS_GNSS_COMMAND:
raise cv.Invalid(f"GNSS not supported for modem '{modem_model}'.")
config[CONF_GNSS_COMMAND] = MODEM_MODELS_GNSS_COMMAND[modem_model]
full_config.data[KEY_MODEM_GNSS] = True
return config