Skip to content

Commit

Permalink
Moved protocol validation for switches to _final_validate
Browse files Browse the repository at this point in the history
  • Loading branch information
paveldn committed Sep 11, 2024
1 parent b6b25c7 commit cfd4fd6
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions components/haier/switch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import esphome.codegen as cg
import esphome.config_validation as cv
import esphome.final_validate as fv
from esphome.components import switch
from esphome.const import (
CONF_BEEPER,
Expand All @@ -8,9 +9,10 @@
)
from ..climate import (
CONF_HAIER_ID,
CONF_PROTOCOL,
HaierClimateBase,
HonClimate,
haier_ns,
PROTOCOL_HON,
)

CODEOWNERS = ["@paveldn"]
Expand Down Expand Up @@ -60,22 +62,28 @@
}
)

def _final_validate(config):
full_config = fv.full_config.get()
for switch_type in [CONF_BEEPER, CONF_QUIET_MODE]:
# Check switches that are only supported for HonClimate
if config.get(switch_type):
climate_path = full_config.get_path_for_id(config[CONF_HAIER_ID])[:-1]
climate_conf = full_config.get_config_for_path(climate_path)
protocol_type= climate_conf.get(CONF_PROTOCOL)
if protocol_type.casefold() != PROTOCOL_HON.casefold():
raise cv.Invalid(
f"{switch_type} switch is only supported for hon climate"
)
return config

FINAL_VALIDATE_SCHEMA = _final_validate

async def to_code(config):
full_id, parent = await cg.get_variable_with_full_id(config[CONF_HAIER_ID])
parent = await cg.get_variable(config[CONF_HAIER_ID])

for switch_type in [CONF_DISPLAY, CONF_HEALTH_MODE]:
for switch_type in [CONF_DISPLAY, CONF_HEALTH_MODE, CONF_BEEPER, CONF_QUIET_MODE]:
if conf := config.get(switch_type):
sw_var = await switch.new_switch(conf)
await cg.register_parented(sw_var, parent)
cg.add(getattr(parent, f"set_{switch_type}_switch")(sw_var))
for switch_type in [CONF_BEEPER, CONF_QUIET_MODE]:
if conf := config.get(switch_type):
if full_id.type is HonClimate:
sw_var = await switch.new_switch(conf)
await cg.register_parented(sw_var, parent)
cg.add(getattr(parent, f"set_{switch_type}_switch")(sw_var))
else:
raise ValueError(
f"{switch_type} switch is only supported for hon climate"
)

0 comments on commit cfd4fd6

Please sign in to comment.