Remove unneeded dynamic lookup of domain (#59423)

This commit is contained in:
Erik Montnemery 2021-11-09 16:41:45 +01:00 committed by GitHub
parent d05c80c8e4
commit 3d909b00d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 59 additions and 65 deletions

View File

@ -0,0 +1,2 @@
"""Constants for the lg_netcast component."""
DOMAIN = "lg_netcast"

View File

@ -31,6 +31,8 @@ from homeassistant.const import (
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import DOMAIN
DEFAULT_NAME = "LG TV Remote" DEFAULT_NAME = "LG TV Remote"
CONF_ON_ACTION = "turn_on_action" 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) on_action = config.get(CONF_ON_ACTION)
client = LgNetCastClient(host, access_token) 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) add_entities([LgTVDevice(client, name, on_action_script)], True)

View File

@ -36,6 +36,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -158,18 +159,17 @@ class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity):
self._disarm_script = None self._disarm_script = None
self._code_arm_required = code_arm_required self._code_arm_required = code_arm_required
self._code_format = code_format self._code_format = code_format
domain = __name__.split(".")[-2]
if disarm_action is not None: 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 self._arm_away_script = None
if arm_away_action is not 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 self._arm_home_script = None
if arm_home_action is not 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 self._arm_night_script = None
if arm_night_action is not 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._state = None
self._unique_id = unique_id self._unique_id = unique_id

View File

@ -40,7 +40,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -195,21 +195,20 @@ class CoverTemplate(TemplateEntity, CoverEntity):
self._tilt_template = tilt_template self._tilt_template = tilt_template
self._device_class = device_class self._device_class = device_class
self._open_script = None self._open_script = None
domain = __name__.split(".")[-2]
if open_action is not None: 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 self._close_script = None
if close_action is not 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 self._stop_script = None
if stop_action is not 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 self._position_script = None
if position_action is not 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 self._tilt_script = None
if tilt_action is not 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._optimistic = optimistic or (not state_template and not position_template)
self._tilt_optimistic = tilt_optimistic or not tilt_template self._tilt_optimistic = tilt_optimistic or not tilt_template
self._position = None self._position = None

View File

@ -39,7 +39,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -209,39 +209,37 @@ class TemplateFan(TemplateEntity, FanEntity):
self._direction_template = direction_template self._direction_template = direction_template
self._supported_features = 0 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 self._set_speed_script = None
if set_speed_action: if set_speed_action:
self._set_speed_script = Script( self._set_speed_script = Script(
hass, set_speed_action, friendly_name, domain hass, set_speed_action, friendly_name, DOMAIN
) )
self._set_percentage_script = None self._set_percentage_script = None
if set_percentage_action: if set_percentage_action:
self._set_percentage_script = Script( 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 self._set_preset_mode_script = None
if set_preset_mode_action: if set_preset_mode_action:
self._set_preset_mode_script = Script( 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 self._set_oscillating_script = None
if set_oscillating_action: if set_oscillating_action:
self._set_oscillating_script = Script( self._set_oscillating_script = Script(
hass, set_oscillating_action, friendly_name, domain hass, set_oscillating_action, friendly_name, DOMAIN
) )
self._set_direction_script = None self._set_direction_script = None
if set_direction_action: if set_direction_action:
self._set_direction_script = Script( self._set_direction_script = Script(
hass, set_direction_action, friendly_name, domain hass, set_direction_action, friendly_name, DOMAIN
) )
self._state = STATE_OFF self._state = STATE_OFF

View File

@ -37,7 +37,7 @@ from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -211,32 +211,31 @@ class LightTemplate(TemplateEntity, LightEntity):
) )
self._name = friendly_name self._name = friendly_name
self._template = state_template self._template = state_template
domain = __name__.split(".")[-2] self._on_script = Script(hass, on_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._off_script = Script(hass, off_action, friendly_name, domain)
self._level_script = None self._level_script = None
if level_action is not 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._level_template = level_template
self._temperature_script = None self._temperature_script = None
if temperature_action is not None: if temperature_action is not None:
self._temperature_script = Script( self._temperature_script = Script(
hass, temperature_action, friendly_name, domain hass, temperature_action, friendly_name, DOMAIN
) )
self._temperature_template = temperature_template self._temperature_template = temperature_template
self._color_script = None self._color_script = None
if color_action is not 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._color_template = color_template
self._white_value_script = None self._white_value_script = None
if white_value_action is not None: if white_value_action is not None:
self._white_value_script = Script( 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._white_value_template = white_value_template
self._effect_script = None self._effect_script = None
if effect_action is not 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_list_template = effect_list_template
self._effect_template = effect_template self._effect_template = effect_template
self._max_mireds_template = max_mireds_template self._max_mireds_template = max_mireds_template

View File

@ -22,7 +22,7 @@ from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
CONF_LOCK = "lock" CONF_LOCK = "lock"
@ -88,9 +88,8 @@ class TemplateLock(TemplateEntity, LockEntity):
self._state = None self._state = None
self._name = name self._name = name
self._state_template = value_template self._state_template = value_template
domain = __name__.split(".")[-2] self._command_lock = Script(hass, command_lock, name, DOMAIN)
self._command_lock = Script(hass, command_lock, name, domain) self._command_unlock = Script(hass, command_unlock, name, DOMAIN)
self._command_unlock = Script(hass, command_unlock, name, domain)
self._optimistic = optimistic self._optimistic = optimistic
self._unique_id = unique_id self._unique_id = unique_id

View File

@ -31,7 +31,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from homeassistant.helpers.template import Template, TemplateError from homeassistant.helpers.template import Template, TemplateError
from .const import CONF_AVAILABILITY from .const import CONF_AVAILABILITY, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
from .trigger_entity import TriggerEntity from .trigger_entity import TriggerEntity
@ -139,9 +139,8 @@ class TemplateNumber(TemplateEntity, NumberEntity):
with contextlib.suppress(TemplateError): with contextlib.suppress(TemplateError):
self._attr_name = name_template.async_render(parse_result=False) self._attr_name = name_template.async_render(parse_result=False)
self._value_template = value_template self._value_template = value_template
domain = __name__.split(".")[-2]
self._command_set_value = Script( 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._step_template = step_template
self._min_value_template = minimum_template self._min_value_template = minimum_template
@ -212,12 +211,11 @@ class TriggerNumberEntity(TriggerEntity, NumberEntity):
) -> None: ) -> None:
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(hass, coordinator, config) super().__init__(hass, coordinator, config)
domain = __name__.split(".")[-2]
self._command_set_value = Script( self._command_set_value = Script(
hass, hass,
config[CONF_SET_VALUE], config[CONF_SET_VALUE],
self._rendered.get(CONF_NAME, DEFAULT_NAME), self._rendered.get(CONF_NAME, DEFAULT_NAME),
domain, DOMAIN,
) )
@property @property

View File

@ -27,7 +27,7 @@ from homeassistant.helpers.script import Script
from homeassistant.helpers.template import Template, TemplateError from homeassistant.helpers.template import Template, TemplateError
from . import TriggerUpdateCoordinator from . import TriggerUpdateCoordinator
from .const import CONF_AVAILABILITY from .const import CONF_AVAILABILITY, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
from .trigger_entity import TriggerEntity from .trigger_entity import TriggerEntity
@ -129,9 +129,8 @@ class TemplateSelect(TemplateEntity, SelectEntity):
self._attr_name = name_template.async_render(parse_result=False) self._attr_name = name_template.async_render(parse_result=False)
self._name_template = name_template self._name_template = name_template
self._value_template = value_template self._value_template = value_template
domain = __name__.split(".")[-2]
self._command_select_option = Script( 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._options_template = options_template
self._attr_assumed_state = self._optimistic = optimistic self._attr_assumed_state = self._optimistic = optimistic
@ -182,12 +181,11 @@ class TriggerSelectEntity(TriggerEntity, SelectEntity):
) -> None: ) -> None:
"""Initialize the entity.""" """Initialize the entity."""
super().__init__(hass, coordinator, config) super().__init__(hass, coordinator, config)
domain = __name__.split(".")[-2]
self._command_select_option = Script( self._command_select_option = Script(
hass, hass,
config[CONF_SELECT_OPTION], config[CONF_SELECT_OPTION],
self._rendered.get(CONF_NAME, DEFAULT_NAME), self._rendered.get(CONF_NAME, DEFAULT_NAME),
domain, DOMAIN,
) )
@property @property

View File

@ -25,7 +25,7 @@ from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"] _VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
@ -119,9 +119,8 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
) )
self._name = friendly_name self._name = friendly_name
self._template = state_template self._template = state_template
domain = __name__.split(".")[-2] self._on_script = Script(hass, on_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._off_script = Script(hass, off_action, friendly_name, domain)
self._state = False self._state = False
self._unique_id = unique_id self._unique_id = unique_id

View File

@ -43,7 +43,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
from .template_entity import TemplateEntity from .template_entity import TemplateEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -187,43 +187,41 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
self._fan_speed_template = fan_speed_template self._fan_speed_template = fan_speed_template
self._supported_features = SUPPORT_START 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 self._pause_script = None
if pause_action: 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._supported_features |= SUPPORT_PAUSE
self._stop_script = None self._stop_script = None
if stop_action: 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._supported_features |= SUPPORT_STOP
self._return_to_base_script = None self._return_to_base_script = None
if return_to_base_action: if return_to_base_action:
self._return_to_base_script = Script( 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._supported_features |= SUPPORT_RETURN_HOME
self._clean_spot_script = None self._clean_spot_script = None
if clean_spot_action: if clean_spot_action:
self._clean_spot_script = Script( 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._supported_features |= SUPPORT_CLEAN_SPOT
self._locate_script = None self._locate_script = None
if locate_action: 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._supported_features |= SUPPORT_LOCATE
self._set_fan_speed_script = None self._set_fan_speed_script = None
if set_fan_speed_action: if set_fan_speed_action:
self._set_fan_speed_script = Script( 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 self._supported_features |= SUPPORT_FAN_SPEED

View File

@ -8,9 +8,9 @@ import wakeonlan
from homeassistant.const import CONF_BROADCAST_ADDRESS, CONF_BROADCAST_PORT, CONF_MAC from homeassistant.const import CONF_BROADCAST_ADDRESS, CONF_BROADCAST_PORT, CONF_MAC
import homeassistant.helpers.config_validation as cv 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" SERVICE_SEND_MAGIC_PACKET = "send_magic_packet"

View File

@ -0,0 +1,2 @@
"""Constants for the Wake-On-LAN component."""
DOMAIN = "wake_on_lan"

View File

@ -18,6 +18,8 @@ from homeassistant.helpers import device_registry as dr
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_OFF_ACTION = "turn_off" CONF_OFF_ACTION = "turn_off"
@ -82,9 +84,8 @@ class WolSwitch(SwitchEntity):
self._mac_address = mac_address self._mac_address = mac_address
self._broadcast_address = broadcast_address self._broadcast_address = broadcast_address
self._broadcast_port = broadcast_port self._broadcast_port = broadcast_port
domain = __name__.split(".")[-2]
self._off_script = ( 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._state = False
self._assumed_state = host is None self._assumed_state = host is None