Remove redundant is not None checks in Template integration (#139790)

Remove redundant is not None checks
pull/139809/head^2
Petro31 2025-03-04 21:58:41 -05:00 committed by GitHub
parent 3eb7302fde
commit e51d9bd6f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 58 deletions

View File

@ -325,9 +325,9 @@ class CoverTemplate(TemplateEntity, CoverEntity):
async def async_open_cover(self, **kwargs: Any) -> None:
"""Move the cover up."""
if (open_script := self._action_scripts.get(OPEN_ACTION)) is not None:
if open_script := self._action_scripts.get(OPEN_ACTION):
await self.async_run_script(open_script, context=self._context)
elif (position_script := self._action_scripts.get(POSITION_ACTION)) is not None:
elif position_script := self._action_scripts.get(POSITION_ACTION):
await self.async_run_script(
position_script,
run_variables={"position": 100},
@ -339,9 +339,9 @@ class CoverTemplate(TemplateEntity, CoverEntity):
async def async_close_cover(self, **kwargs: Any) -> None:
"""Move the cover down."""
if (close_script := self._action_scripts.get(CLOSE_ACTION)) is not None:
if close_script := self._action_scripts.get(CLOSE_ACTION):
await self.async_run_script(close_script, context=self._context)
elif (position_script := self._action_scripts.get(POSITION_ACTION)) is not None:
elif position_script := self._action_scripts.get(POSITION_ACTION):
await self.async_run_script(
position_script,
run_variables={"position": 0},
@ -353,7 +353,7 @@ class CoverTemplate(TemplateEntity, CoverEntity):
async def async_stop_cover(self, **kwargs: Any) -> None:
"""Fire the stop action."""
if (stop_script := self._action_scripts.get(STOP_ACTION)) is not None:
if stop_script := self._action_scripts.get(STOP_ACTION):
await self.async_run_script(stop_script, context=self._context)
async def async_set_cover_position(self, **kwargs: Any) -> None:

View File

@ -37,8 +37,6 @@ class AbstractTemplateEntity(Entity):
):
"""Add an action script."""
# Cannot use self.hass because it may be None in child class
# at instantiation.
self._action_scripts[script_id] = Script(
self.hass,
config,

View File

@ -260,7 +260,7 @@ class TemplateFan(TemplateEntity, FanEntity):
"""Set the percentage speed of the fan."""
self._percentage = percentage
if (script := self._action_scripts.get(CONF_SET_PERCENTAGE_ACTION)) is not None:
if script := self._action_scripts.get(CONF_SET_PERCENTAGE_ACTION):
await self.async_run_script(
script,
run_variables={ATTR_PERCENTAGE: self._percentage},
@ -275,9 +275,7 @@ class TemplateFan(TemplateEntity, FanEntity):
"""Set the preset_mode of the fan."""
self._preset_mode = preset_mode
if (
script := self._action_scripts.get(CONF_SET_PRESET_MODE_ACTION)
) is not None:
if script := self._action_scripts.get(CONF_SET_PRESET_MODE_ACTION):
await self.async_run_script(
script,
run_variables={ATTR_PRESET_MODE: self._preset_mode},

View File

@ -222,7 +222,7 @@ class LightTemplate(TemplateEntity, LightEntity):
(CONF_RGBW_ACTION, ColorMode.RGBW),
(CONF_RGBWW_ACTION, ColorMode.RGBWW),
):
if (action_config := config.get(action_id)) is not None:
if action_config := config.get(action_id):
self.add_script(action_id, action_config, name, DOMAIN)
color_modes.add(color_mode)
self._supported_color_modes = filter_supported_color_modes(color_modes)
@ -232,7 +232,7 @@ class LightTemplate(TemplateEntity, LightEntity):
self._color_mode = next(iter(self._supported_color_modes))
self._attr_supported_features = LightEntityFeature(0)
if self._action_scripts.get(CONF_EFFECT_ACTION) is not None:
if self._action_scripts.get(CONF_EFFECT_ACTION):
self._attr_supported_features |= LightEntityFeature.EFFECT
if self._supports_transition is True:
self._attr_supported_features |= LightEntityFeature.TRANSITION
@ -530,12 +530,8 @@ class LightTemplate(TemplateEntity, LightEntity):
if ATTR_TRANSITION in kwargs and self._supports_transition is True:
common_params["transition"] = kwargs[ATTR_TRANSITION]
if (
ATTR_COLOR_TEMP_KELVIN in kwargs
and (
temperature_script := self._action_scripts.get(CONF_TEMPERATURE_ACTION)
)
is not None
if ATTR_COLOR_TEMP_KELVIN in kwargs and (
temperature_script := self._action_scripts.get(CONF_TEMPERATURE_ACTION)
):
common_params["color_temp"] = color_util.color_temperature_kelvin_to_mired(
kwargs[ATTR_COLOR_TEMP_KELVIN]
@ -546,10 +542,8 @@ class LightTemplate(TemplateEntity, LightEntity):
run_variables=common_params,
context=self._context,
)
elif (
ATTR_EFFECT in kwargs
and (effect_script := self._action_scripts.get(CONF_EFFECT_ACTION))
is not None
elif ATTR_EFFECT in kwargs and (
effect_script := self._action_scripts.get(CONF_EFFECT_ACTION)
):
assert self._effect_list is not None
effect = kwargs[ATTR_EFFECT]
@ -567,10 +561,8 @@ class LightTemplate(TemplateEntity, LightEntity):
await self.async_run_script(
effect_script, run_variables=common_params, context=self._context
)
elif (
ATTR_HS_COLOR in kwargs
and (color_script := self._action_scripts.get(CONF_COLOR_ACTION))
is not None
elif ATTR_HS_COLOR in kwargs and (
color_script := self._action_scripts.get(CONF_COLOR_ACTION)
):
hs_value = kwargs[ATTR_HS_COLOR]
common_params["hs"] = hs_value
@ -580,9 +572,8 @@ class LightTemplate(TemplateEntity, LightEntity):
await self.async_run_script(
color_script, run_variables=common_params, context=self._context
)
elif (
ATTR_HS_COLOR in kwargs
and (hs_script := self._action_scripts.get(CONF_HS_ACTION)) is not None
elif ATTR_HS_COLOR in kwargs and (
hs_script := self._action_scripts.get(CONF_HS_ACTION)
):
hs_value = kwargs[ATTR_HS_COLOR]
common_params["hs"] = hs_value
@ -592,10 +583,8 @@ class LightTemplate(TemplateEntity, LightEntity):
await self.async_run_script(
hs_script, run_variables=common_params, context=self._context
)
elif (
ATTR_RGBWW_COLOR in kwargs
and (rgbww_script := self._action_scripts.get(CONF_RGBWW_ACTION))
is not None
elif ATTR_RGBWW_COLOR in kwargs and (
rgbww_script := self._action_scripts.get(CONF_RGBWW_ACTION)
):
rgbww_value = kwargs[ATTR_RGBWW_COLOR]
common_params["rgbww"] = rgbww_value
@ -613,9 +602,8 @@ class LightTemplate(TemplateEntity, LightEntity):
await self.async_run_script(
rgbww_script, run_variables=common_params, context=self._context
)
elif (
ATTR_RGBW_COLOR in kwargs
and (rgbw_script := self._action_scripts.get(CONF_RGBW_ACTION)) is not None
elif ATTR_RGBW_COLOR in kwargs and (
rgbw_script := self._action_scripts.get(CONF_RGBW_ACTION)
):
rgbw_value = kwargs[ATTR_RGBW_COLOR]
common_params["rgbw"] = rgbw_value
@ -632,9 +620,8 @@ class LightTemplate(TemplateEntity, LightEntity):
await self.async_run_script(
rgbw_script, run_variables=common_params, context=self._context
)
elif (
ATTR_RGB_COLOR in kwargs
and (rgb_script := self._action_scripts.get(CONF_RGB_ACTION)) is not None
elif ATTR_RGB_COLOR in kwargs and (
rgb_script := self._action_scripts.get(CONF_RGB_ACTION)
):
rgb_value = kwargs[ATTR_RGB_COLOR]
common_params["rgb"] = rgb_value
@ -645,10 +632,8 @@ class LightTemplate(TemplateEntity, LightEntity):
await self.async_run_script(
rgb_script, run_variables=common_params, context=self._context
)
elif (
ATTR_BRIGHTNESS in kwargs
and (level_script := self._action_scripts.get(CONF_LEVEL_ACTION))
is not None
elif ATTR_BRIGHTNESS in kwargs and (
level_script := self._action_scripts.get(CONF_LEVEL_ACTION)
):
await self.async_run_script(
level_script, run_variables=common_params, context=self._context

View File

@ -208,7 +208,7 @@ class TemplateNumber(TemplateEntity, NumberEntity):
if self._optimistic:
self._attr_native_value = value
self.async_write_ha_state()
if (set_value := self._action_scripts.get(CONF_SET_VALUE)) is not None:
if set_value := self._action_scripts.get(CONF_SET_VALUE):
await self.async_run_script(
set_value,
run_variables={ATTR_VALUE: value},

View File

@ -142,10 +142,8 @@ class TemplateSelect(TemplateEntity, SelectEntity):
super().__init__(hass, config=config, unique_id=unique_id)
assert self._attr_name is not None
self._value_template = config[CONF_STATE]
if (selection_option := config.get(CONF_SELECT_OPTION)) is not None:
self.add_script(
CONF_SELECT_OPTION, selection_option, self._attr_name, DOMAIN
)
if select_option := config.get(CONF_SELECT_OPTION):
self.add_script(CONF_SELECT_OPTION, select_option, self._attr_name, DOMAIN)
self._options_template = config[ATTR_OPTIONS]
self._attr_assumed_state = self._optimistic = config.get(CONF_OPTIMISTIC, False)
self._attr_options = []
@ -177,7 +175,7 @@ class TemplateSelect(TemplateEntity, SelectEntity):
if self._optimistic:
self._attr_current_option = option
self.async_write_ha_state()
if (select_option := self._action_scripts.get(CONF_SELECT_OPTION)) is not None:
if select_option := self._action_scripts.get(CONF_SELECT_OPTION):
await self.async_run_script(
select_option,
run_variables={ATTR_OPTION: option},

View File

@ -206,7 +206,7 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Fire the on action."""
if (on_script := self._action_scripts.get(CONF_TURN_ON)) is not None:
if on_script := self._action_scripts.get(CONF_TURN_ON):
await self.async_run_script(on_script, context=self._context)
if self._template is None:
self._state = True
@ -214,7 +214,7 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Fire the off action."""
if (off_script := self._action_scripts.get(CONF_TURN_OFF)) is not None:
if off_script := self._action_scripts.get(CONF_TURN_OFF):
await self.async_run_script(off_script, context=self._context)
if self._template is None:
self._state = False

View File

@ -185,27 +185,27 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
async def async_pause(self) -> None:
"""Pause the cleaning task."""
if (script := self._action_scripts.get(SERVICE_PAUSE)) is not None:
if script := self._action_scripts.get(SERVICE_PAUSE):
await self.async_run_script(script, context=self._context)
async def async_stop(self, **kwargs: Any) -> None:
"""Stop the cleaning task."""
if (script := self._action_scripts.get(SERVICE_STOP)) is not None:
if script := self._action_scripts.get(SERVICE_STOP):
await self.async_run_script(script, context=self._context)
async def async_return_to_base(self, **kwargs: Any) -> None:
"""Set the vacuum cleaner to return to the dock."""
if (script := self._action_scripts.get(SERVICE_RETURN_TO_BASE)) is not None:
if script := self._action_scripts.get(SERVICE_RETURN_TO_BASE):
await self.async_run_script(script, context=self._context)
async def async_clean_spot(self, **kwargs: Any) -> None:
"""Perform a spot clean-up."""
if (script := self._action_scripts.get(SERVICE_CLEAN_SPOT)) is not None:
if script := self._action_scripts.get(SERVICE_CLEAN_SPOT):
await self.async_run_script(script, context=self._context)
async def async_locate(self, **kwargs: Any) -> None:
"""Locate the vacuum cleaner."""
if (script := self._action_scripts.get(SERVICE_LOCATE)) is not None:
if script := self._action_scripts.get(SERVICE_LOCATE):
await self.async_run_script(script, context=self._context)
async def async_set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None:
@ -219,7 +219,7 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
)
return
if (script := self._action_scripts.get(SERVICE_SET_FAN_SPEED)) is not None:
if script := self._action_scripts.get(SERVICE_SET_FAN_SPEED):
await self.async_run_script(
script, run_variables={ATTR_FAN_SPEED: fan_speed}, context=self._context
)