Housecleaning: Use walrus operator in cover (#6562)

This commit is contained in:
Jesse Hills 2024-04-17 18:28:01 +12:00 committed by GitHub
parent 21e3faad38
commit fa1adf7528
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -122,8 +122,8 @@ COVER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).ex
async def setup_cover_core_(var, config): async def setup_cover_core_(var, config):
await setup_entity(var, config) await setup_entity(var, config)
if CONF_DEVICE_CLASS in config: if (device_class := config.get(CONF_DEVICE_CLASS)) is not None:
cg.add(var.set_device_class(config[CONF_DEVICE_CLASS])) cg.add(var.set_device_class(device_class))
for conf in config.get(CONF_ON_OPEN, []): for conf in config.get(CONF_ON_OPEN, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
@ -132,24 +132,20 @@ async def setup_cover_core_(var, config):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [], conf) await automation.build_automation(trigger, [], conf)
if CONF_MQTT_ID in config: if (mqtt_id := config.get(CONF_MQTT_ID)) is not None:
mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var) mqtt_ = cg.new_Pvariable(mqtt_id, var)
await mqtt.register_mqtt_component(mqtt_, config) await mqtt.register_mqtt_component(mqtt_, config)
if CONF_POSITION_STATE_TOPIC in config: if (position_state_topic := config.get(CONF_POSITION_STATE_TOPIC)) is not None:
cg.add( cg.add(mqtt_.set_custom_position_state_topic(position_state_topic))
mqtt_.set_custom_position_state_topic(config[CONF_POSITION_STATE_TOPIC]) if (
) position_command_topic := config.get(CONF_POSITION_COMMAND_TOPIC)
if CONF_POSITION_COMMAND_TOPIC in config: ) is not None:
cg.add( cg.add(mqtt_.set_custom_position_command_topic(position_command_topic))
mqtt_.set_custom_position_command_topic( if (tilt_state_topic := config.get(CONF_TILT_STATE_TOPIC)) is not None:
config[CONF_POSITION_COMMAND_TOPIC] cg.add(mqtt_.set_custom_tilt_state_topic(tilt_state_topic))
) if (tilt_command_topic := config.get(CONF_TILT_COMMAND_TOPIC)) is not None:
) cg.add(mqtt_.set_custom_tilt_command_topic(tilt_command_topic))
if CONF_TILT_STATE_TOPIC in config:
cg.add(mqtt_.set_custom_tilt_state_topic(config[CONF_TILT_STATE_TOPIC]))
if CONF_TILT_COMMAND_TOPIC in config:
cg.add(mqtt_.set_custom_tilt_command_topic(config[CONF_TILT_COMMAND_TOPIC]))
async def register_cover(var, config): async def register_cover(var, config):
@ -205,17 +201,17 @@ COVER_CONTROL_ACTION_SCHEMA = cv.Schema(
async def cover_control_to_code(config, action_id, template_arg, args): async def cover_control_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
if CONF_STOP in config: if (stop := config.get(CONF_STOP)) is not None:
template_ = await cg.templatable(config[CONF_STOP], args, bool) template_ = await cg.templatable(stop, args, bool)
cg.add(var.set_stop(template_)) cg.add(var.set_stop(template_))
if CONF_STATE in config: if (state := config.get(CONF_STATE)) is not None:
template_ = await cg.templatable(config[CONF_STATE], args, float) template_ = await cg.templatable(state, args, float)
cg.add(var.set_position(template_)) cg.add(var.set_position(template_))
if CONF_POSITION in config: if (position := config.get(CONF_POSITION)) is not None:
template_ = await cg.templatable(config[CONF_POSITION], args, float) template_ = await cg.templatable(position, args, float)
cg.add(var.set_position(template_)) cg.add(var.set_position(template_))
if CONF_TILT in config: if (tilt := config.get(CONF_TILT)) is not None:
template_ = await cg.templatable(config[CONF_TILT], args, float) template_ = await cg.templatable(tilt, args, float)
cg.add(var.set_tilt(template_)) cg.add(var.set_tilt(template_))
return var return var