Housecleaning: Use walrus operator in switch (#6558)

This commit is contained in:
Jesse Hills 2024-04-18 09:43:00 +12:00 committed by GitHub
parent 51ed6d62d9
commit c8cdb30459
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -138,8 +138,8 @@ SWITCH_SCHEMA = switch_schema() # for compatibility
async def setup_switch_core_(var, config): async def setup_switch_core_(var, config):
await setup_entity(var, config) await setup_entity(var, config)
if CONF_INVERTED in config: if (inverted := config.get(CONF_INVERTED)) is not None:
cg.add(var.set_inverted(config[CONF_INVERTED])) cg.add(var.set_inverted(inverted))
for conf in config.get(CONF_ON_TURN_ON, []): for conf in config.get(CONF_ON_TURN_ON, []):
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)
@ -147,12 +147,12 @@ async def setup_switch_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_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))
cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE])) cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE]))