From 3d909b00d50af11eec707599475e29f6f6736c90 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 9 Nov 2021 16:41:45 +0100 Subject: [PATCH] Remove unneeded dynamic lookup of domain (#59423) --- homeassistant/components/lg_netcast/const.py | 2 ++ .../components/lg_netcast/media_player.py | 5 +++-- .../components/template/alarm_control_panel.py | 10 +++++----- homeassistant/components/template/cover.py | 13 ++++++------- homeassistant/components/template/fan.py | 18 ++++++++---------- homeassistant/components/template/light.py | 17 ++++++++--------- homeassistant/components/template/lock.py | 7 +++---- homeassistant/components/template/number.py | 8 +++----- homeassistant/components/template/select.py | 8 +++----- homeassistant/components/template/switch.py | 7 +++---- homeassistant/components/template/vacuum.py | 18 ++++++++---------- .../components/wake_on_lan/__init__.py | 4 ++-- homeassistant/components/wake_on_lan/const.py | 2 ++ homeassistant/components/wake_on_lan/switch.py | 5 +++-- 14 files changed, 59 insertions(+), 65 deletions(-) create mode 100644 homeassistant/components/lg_netcast/const.py create mode 100644 homeassistant/components/wake_on_lan/const.py diff --git a/homeassistant/components/lg_netcast/const.py b/homeassistant/components/lg_netcast/const.py new file mode 100644 index 00000000000..6cb44a5a3f8 --- /dev/null +++ b/homeassistant/components/lg_netcast/const.py @@ -0,0 +1,2 @@ +"""Constants for the lg_netcast component.""" +DOMAIN = "lg_netcast" diff --git a/homeassistant/components/lg_netcast/media_player.py b/homeassistant/components/lg_netcast/media_player.py index 5b5ce313689..5938fc8d616 100644 --- a/homeassistant/components/lg_netcast/media_player.py +++ b/homeassistant/components/lg_netcast/media_player.py @@ -31,6 +31,8 @@ from homeassistant.const import ( import homeassistant.helpers.config_validation as cv from homeassistant.helpers.script import Script +from .const import DOMAIN + DEFAULT_NAME = "LG TV Remote" CONF_ON_ACTION = "turn_on_action" @@ -69,8 +71,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): on_action = config.get(CONF_ON_ACTION) client = LgNetCastClient(host, access_token) - domain = __name__.split(".")[-2] - on_action_script = Script(hass, on_action, name, domain) if on_action else None + on_action_script = Script(hass, on_action, name, DOMAIN) if on_action else None add_entities([LgTVDevice(client, name, on_action_script)], True) diff --git a/homeassistant/components/template/alarm_control_panel.py b/homeassistant/components/template/alarm_control_panel.py index 2cb830e54c2..05006049b02 100644 --- a/homeassistant/components/template/alarm_control_panel.py +++ b/homeassistant/components/template/alarm_control_panel.py @@ -36,6 +36,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.script import Script +from .const import DOMAIN from .template_entity import TemplateEntity _LOGGER = logging.getLogger(__name__) @@ -158,18 +159,17 @@ class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity): self._disarm_script = None self._code_arm_required = code_arm_required self._code_format = code_format - domain = __name__.split(".")[-2] if disarm_action is not None: - self._disarm_script = Script(hass, disarm_action, name, domain) + self._disarm_script = Script(hass, disarm_action, name, DOMAIN) self._arm_away_script = None if arm_away_action is not None: - self._arm_away_script = Script(hass, arm_away_action, name, domain) + self._arm_away_script = Script(hass, arm_away_action, name, DOMAIN) self._arm_home_script = None if arm_home_action is not None: - self._arm_home_script = Script(hass, arm_home_action, name, domain) + self._arm_home_script = Script(hass, arm_home_action, name, DOMAIN) self._arm_night_script = None if arm_night_action is not None: - self._arm_night_script = Script(hass, arm_night_action, name, domain) + self._arm_night_script = Script(hass, arm_night_action, name, DOMAIN) self._state = None self._unique_id = unique_id diff --git a/homeassistant/components/template/cover.py b/homeassistant/components/template/cover.py index a9f28d56669..d5cd04e94f2 100644 --- a/homeassistant/components/template/cover.py +++ b/homeassistant/components/template/cover.py @@ -40,7 +40,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.script import Script -from .const import CONF_AVAILABILITY_TEMPLATE +from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN from .template_entity import TemplateEntity _LOGGER = logging.getLogger(__name__) @@ -195,21 +195,20 @@ class CoverTemplate(TemplateEntity, CoverEntity): self._tilt_template = tilt_template self._device_class = device_class self._open_script = None - domain = __name__.split(".")[-2] if open_action is not None: - self._open_script = Script(hass, open_action, friendly_name, domain) + self._open_script = Script(hass, open_action, friendly_name, DOMAIN) self._close_script = None if close_action is not None: - self._close_script = Script(hass, close_action, friendly_name, domain) + self._close_script = Script(hass, close_action, friendly_name, DOMAIN) self._stop_script = None if stop_action is not None: - self._stop_script = Script(hass, stop_action, friendly_name, domain) + self._stop_script = Script(hass, stop_action, friendly_name, DOMAIN) self._position_script = None if position_action is not None: - self._position_script = Script(hass, position_action, friendly_name, domain) + self._position_script = Script(hass, position_action, friendly_name, DOMAIN) self._tilt_script = None if tilt_action is not None: - self._tilt_script = Script(hass, tilt_action, friendly_name, domain) + self._tilt_script = Script(hass, tilt_action, friendly_name, DOMAIN) self._optimistic = optimistic or (not state_template and not position_template) self._tilt_optimistic = tilt_optimistic or not tilt_template self._position = None diff --git a/homeassistant/components/template/fan.py b/homeassistant/components/template/fan.py index f39d49fa9fd..4a872d51dbc 100644 --- a/homeassistant/components/template/fan.py +++ b/homeassistant/components/template/fan.py @@ -39,7 +39,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.script import Script -from .const import CONF_AVAILABILITY_TEMPLATE +from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN from .template_entity import TemplateEntity _LOGGER = logging.getLogger(__name__) @@ -209,39 +209,37 @@ class TemplateFan(TemplateEntity, FanEntity): self._direction_template = direction_template self._supported_features = 0 - domain = __name__.split(".")[-2] - - self._on_script = Script(hass, on_action, friendly_name, domain) - self._off_script = Script(hass, off_action, friendly_name, domain) + self._on_script = Script(hass, on_action, friendly_name, DOMAIN) + self._off_script = Script(hass, off_action, friendly_name, DOMAIN) self._set_speed_script = None if set_speed_action: self._set_speed_script = Script( - hass, set_speed_action, friendly_name, domain + hass, set_speed_action, friendly_name, DOMAIN ) self._set_percentage_script = None if set_percentage_action: self._set_percentage_script = Script( - hass, set_percentage_action, friendly_name, domain + hass, set_percentage_action, friendly_name, DOMAIN ) self._set_preset_mode_script = None if set_preset_mode_action: self._set_preset_mode_script = Script( - hass, set_preset_mode_action, friendly_name, domain + hass, set_preset_mode_action, friendly_name, DOMAIN ) self._set_oscillating_script = None if set_oscillating_action: self._set_oscillating_script = Script( - hass, set_oscillating_action, friendly_name, domain + hass, set_oscillating_action, friendly_name, DOMAIN ) self._set_direction_script = None if set_direction_action: self._set_direction_script = Script( - hass, set_direction_action, friendly_name, domain + hass, set_direction_action, friendly_name, DOMAIN ) self._state = STATE_OFF diff --git a/homeassistant/components/template/light.py b/homeassistant/components/template/light.py index b8ebe03ceba..fce35f312f8 100644 --- a/homeassistant/components/template/light.py +++ b/homeassistant/components/template/light.py @@ -37,7 +37,7 @@ from homeassistant.helpers.config_validation import PLATFORM_SCHEMA from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.script import Script -from .const import CONF_AVAILABILITY_TEMPLATE +from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN from .template_entity import TemplateEntity _LOGGER = logging.getLogger(__name__) @@ -211,32 +211,31 @@ class LightTemplate(TemplateEntity, LightEntity): ) self._name = friendly_name self._template = state_template - domain = __name__.split(".")[-2] - self._on_script = Script(hass, on_action, friendly_name, domain) - self._off_script = Script(hass, off_action, friendly_name, domain) + self._on_script = Script(hass, on_action, friendly_name, DOMAIN) + self._off_script = Script(hass, off_action, friendly_name, DOMAIN) self._level_script = None if level_action is not None: - self._level_script = Script(hass, level_action, friendly_name, domain) + self._level_script = Script(hass, level_action, friendly_name, DOMAIN) self._level_template = level_template self._temperature_script = None if temperature_action is not None: self._temperature_script = Script( - hass, temperature_action, friendly_name, domain + hass, temperature_action, friendly_name, DOMAIN ) self._temperature_template = temperature_template self._color_script = None if color_action is not None: - self._color_script = Script(hass, color_action, friendly_name, domain) + self._color_script = Script(hass, color_action, friendly_name, DOMAIN) self._color_template = color_template self._white_value_script = None if white_value_action is not None: self._white_value_script = Script( - hass, white_value_action, friendly_name, domain + hass, white_value_action, friendly_name, DOMAIN ) self._white_value_template = white_value_template self._effect_script = None if effect_action is not None: - self._effect_script = Script(hass, effect_action, friendly_name, domain) + self._effect_script = Script(hass, effect_action, friendly_name, DOMAIN) self._effect_list_template = effect_list_template self._effect_template = effect_template self._max_mireds_template = max_mireds_template diff --git a/homeassistant/components/template/lock.py b/homeassistant/components/template/lock.py index 51431d133f7..a078ce778b6 100644 --- a/homeassistant/components/template/lock.py +++ b/homeassistant/components/template/lock.py @@ -22,7 +22,7 @@ from homeassistant.exceptions import TemplateError import homeassistant.helpers.config_validation as cv from homeassistant.helpers.script import Script -from .const import CONF_AVAILABILITY_TEMPLATE +from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN from .template_entity import TemplateEntity CONF_LOCK = "lock" @@ -88,9 +88,8 @@ class TemplateLock(TemplateEntity, LockEntity): self._state = None self._name = name self._state_template = value_template - domain = __name__.split(".")[-2] - self._command_lock = Script(hass, command_lock, name, domain) - self._command_unlock = Script(hass, command_unlock, name, domain) + self._command_lock = Script(hass, command_lock, name, DOMAIN) + self._command_unlock = Script(hass, command_unlock, name, DOMAIN) self._optimistic = optimistic self._unique_id = unique_id diff --git a/homeassistant/components/template/number.py b/homeassistant/components/template/number.py index 86cc4886430..0f5e8e4bee8 100644 --- a/homeassistant/components/template/number.py +++ b/homeassistant/components/template/number.py @@ -31,7 +31,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.script import Script from homeassistant.helpers.template import Template, TemplateError -from .const import CONF_AVAILABILITY +from .const import CONF_AVAILABILITY, DOMAIN from .template_entity import TemplateEntity from .trigger_entity import TriggerEntity @@ -139,9 +139,8 @@ class TemplateNumber(TemplateEntity, NumberEntity): with contextlib.suppress(TemplateError): self._attr_name = name_template.async_render(parse_result=False) self._value_template = value_template - domain = __name__.split(".")[-2] self._command_set_value = Script( - hass, command_set_value, self._attr_name, domain + hass, command_set_value, self._attr_name, DOMAIN ) self._step_template = step_template self._min_value_template = minimum_template @@ -212,12 +211,11 @@ class TriggerNumberEntity(TriggerEntity, NumberEntity): ) -> None: """Initialize the entity.""" super().__init__(hass, coordinator, config) - domain = __name__.split(".")[-2] self._command_set_value = Script( hass, config[CONF_SET_VALUE], self._rendered.get(CONF_NAME, DEFAULT_NAME), - domain, + DOMAIN, ) @property diff --git a/homeassistant/components/template/select.py b/homeassistant/components/template/select.py index 03136c0193d..ee43ba906c0 100644 --- a/homeassistant/components/template/select.py +++ b/homeassistant/components/template/select.py @@ -27,7 +27,7 @@ from homeassistant.helpers.script import Script from homeassistant.helpers.template import Template, TemplateError from . import TriggerUpdateCoordinator -from .const import CONF_AVAILABILITY +from .const import CONF_AVAILABILITY, DOMAIN from .template_entity import TemplateEntity from .trigger_entity import TriggerEntity @@ -129,9 +129,8 @@ class TemplateSelect(TemplateEntity, SelectEntity): self._attr_name = name_template.async_render(parse_result=False) self._name_template = name_template self._value_template = value_template - domain = __name__.split(".")[-2] self._command_select_option = Script( - hass, command_select_option, self._attr_name, domain + hass, command_select_option, self._attr_name, DOMAIN ) self._options_template = options_template self._attr_assumed_state = self._optimistic = optimistic @@ -182,12 +181,11 @@ class TriggerSelectEntity(TriggerEntity, SelectEntity): ) -> None: """Initialize the entity.""" super().__init__(hass, coordinator, config) - domain = __name__.split(".")[-2] self._command_select_option = Script( hass, config[CONF_SELECT_OPTION], self._rendered.get(CONF_NAME, DEFAULT_NAME), - domain, + DOMAIN, ) @property diff --git a/homeassistant/components/template/switch.py b/homeassistant/components/template/switch.py index a654e59eaa2..a5b85e4b408 100644 --- a/homeassistant/components/template/switch.py +++ b/homeassistant/components/template/switch.py @@ -25,7 +25,7 @@ from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.script import Script -from .const import CONF_AVAILABILITY_TEMPLATE +from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN from .template_entity import TemplateEntity _VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"] @@ -119,9 +119,8 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity): ) self._name = friendly_name self._template = state_template - domain = __name__.split(".")[-2] - self._on_script = Script(hass, on_action, friendly_name, domain) - self._off_script = Script(hass, off_action, friendly_name, domain) + self._on_script = Script(hass, on_action, friendly_name, DOMAIN) + self._off_script = Script(hass, off_action, friendly_name, DOMAIN) self._state = False self._unique_id = unique_id diff --git a/homeassistant/components/template/vacuum.py b/homeassistant/components/template/vacuum.py index 78c51d2009c..a6b8bf0f379 100644 --- a/homeassistant/components/template/vacuum.py +++ b/homeassistant/components/template/vacuum.py @@ -43,7 +43,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.script import Script -from .const import CONF_AVAILABILITY_TEMPLATE +from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN from .template_entity import TemplateEntity _LOGGER = logging.getLogger(__name__) @@ -187,43 +187,41 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity): self._fan_speed_template = fan_speed_template self._supported_features = SUPPORT_START - domain = __name__.split(".")[-2] - - self._start_script = Script(hass, start_action, friendly_name, domain) + self._start_script = Script(hass, start_action, friendly_name, DOMAIN) self._pause_script = None if pause_action: - self._pause_script = Script(hass, pause_action, friendly_name, domain) + self._pause_script = Script(hass, pause_action, friendly_name, DOMAIN) self._supported_features |= SUPPORT_PAUSE self._stop_script = None if stop_action: - self._stop_script = Script(hass, stop_action, friendly_name, domain) + self._stop_script = Script(hass, stop_action, friendly_name, DOMAIN) self._supported_features |= SUPPORT_STOP self._return_to_base_script = None if return_to_base_action: self._return_to_base_script = Script( - hass, return_to_base_action, friendly_name, domain + hass, return_to_base_action, friendly_name, DOMAIN ) self._supported_features |= SUPPORT_RETURN_HOME self._clean_spot_script = None if clean_spot_action: self._clean_spot_script = Script( - hass, clean_spot_action, friendly_name, domain + hass, clean_spot_action, friendly_name, DOMAIN ) self._supported_features |= SUPPORT_CLEAN_SPOT self._locate_script = None if locate_action: - self._locate_script = Script(hass, locate_action, friendly_name, domain) + self._locate_script = Script(hass, locate_action, friendly_name, DOMAIN) self._supported_features |= SUPPORT_LOCATE self._set_fan_speed_script = None if set_fan_speed_action: self._set_fan_speed_script = Script( - hass, set_fan_speed_action, friendly_name, domain + hass, set_fan_speed_action, friendly_name, DOMAIN ) self._supported_features |= SUPPORT_FAN_SPEED diff --git a/homeassistant/components/wake_on_lan/__init__.py b/homeassistant/components/wake_on_lan/__init__.py index d6a254ebf52..dba25b44d75 100644 --- a/homeassistant/components/wake_on_lan/__init__.py +++ b/homeassistant/components/wake_on_lan/__init__.py @@ -8,9 +8,9 @@ import wakeonlan from homeassistant.const import CONF_BROADCAST_ADDRESS, CONF_BROADCAST_PORT, CONF_MAC import homeassistant.helpers.config_validation as cv -_LOGGER = logging.getLogger(__name__) +from .const import DOMAIN -DOMAIN = "wake_on_lan" +_LOGGER = logging.getLogger(__name__) SERVICE_SEND_MAGIC_PACKET = "send_magic_packet" diff --git a/homeassistant/components/wake_on_lan/const.py b/homeassistant/components/wake_on_lan/const.py new file mode 100644 index 00000000000..14f2bd0263f --- /dev/null +++ b/homeassistant/components/wake_on_lan/const.py @@ -0,0 +1,2 @@ +"""Constants for the Wake-On-LAN component.""" +DOMAIN = "wake_on_lan" diff --git a/homeassistant/components/wake_on_lan/switch.py b/homeassistant/components/wake_on_lan/switch.py index 4bbd1522c91..f7e5426c73f 100644 --- a/homeassistant/components/wake_on_lan/switch.py +++ b/homeassistant/components/wake_on_lan/switch.py @@ -18,6 +18,8 @@ from homeassistant.helpers import device_registry as dr import homeassistant.helpers.config_validation as cv from homeassistant.helpers.script import Script +from .const import DOMAIN + _LOGGER = logging.getLogger(__name__) CONF_OFF_ACTION = "turn_off" @@ -82,9 +84,8 @@ class WolSwitch(SwitchEntity): self._mac_address = mac_address self._broadcast_address = broadcast_address self._broadcast_port = broadcast_port - domain = __name__.split(".")[-2] self._off_script = ( - Script(hass, off_action, name, domain) if off_action else None + Script(hass, off_action, name, DOMAIN) if off_action else None ) self._state = False self._assumed_state = host is None