Adjust DOMAIN imports in homeassistant integration (#122774)

pull/122808/head
epenet 2024-07-29 22:14:05 +02:00 committed by GitHub
parent 3e1aee4cbc
commit 2102a104d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 45 deletions

View File

@ -23,7 +23,13 @@ from homeassistant.const import (
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
import homeassistant.core as ha
from homeassistant.core import (
HomeAssistant,
ServiceCall,
ServiceResponse,
callback,
split_entity_id,
)
from homeassistant.exceptions import HomeAssistantError, Unauthorized, UnknownUser
from homeassistant.helpers import config_validation as cv, recorder, restore_state
from homeassistant.helpers.entity_component import async_update_entity
@ -76,14 +82,14 @@ SCHEMA_RESTART = vol.Schema({vol.Optional(ATTR_SAFE_MODE, default=False): bool})
SHUTDOWN_SERVICES = (SERVICE_HOMEASSISTANT_STOP, SERVICE_HOMEASSISTANT_RESTART)
async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # noqa: C901
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: C901
"""Set up general services related to Home Assistant."""
async def async_save_persistent_states(service: ha.ServiceCall) -> None:
async def async_save_persistent_states(service: ServiceCall) -> None:
"""Handle calls to homeassistant.save_persistent_states."""
await restore_state.RestoreStateData.async_save_persistent_states(hass)
async def async_handle_turn_service(service: ha.ServiceCall) -> None:
async def async_handle_turn_service(service: ServiceCall) -> None:
"""Handle calls to homeassistant.turn_on/off."""
referenced = async_extract_referenced_entity_ids(hass, service)
all_referenced = referenced.referenced | referenced.indirectly_referenced
@ -98,10 +104,10 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
# Group entity_ids by domain. groupby requires sorted data.
by_domain = it.groupby(
sorted(all_referenced), lambda item: ha.split_entity_id(item)[0]
sorted(all_referenced), lambda item: split_entity_id(item)[0]
)
tasks: list[Coroutine[Any, Any, ha.ServiceResponse]] = []
tasks: list[Coroutine[Any, Any, ServiceResponse]] = []
unsupported_entities: set[str] = set()
for domain, ent_ids in by_domain:
@ -145,24 +151,24 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
await asyncio.gather(*tasks)
hass.services.async_register(
ha.DOMAIN, SERVICE_SAVE_PERSISTENT_STATES, async_save_persistent_states
DOMAIN, SERVICE_SAVE_PERSISTENT_STATES, async_save_persistent_states
)
service_schema = vol.Schema({ATTR_ENTITY_ID: cv.entity_ids}, extra=vol.ALLOW_EXTRA)
hass.services.async_register(
ha.DOMAIN, SERVICE_TURN_OFF, async_handle_turn_service, schema=service_schema
DOMAIN, SERVICE_TURN_OFF, async_handle_turn_service, schema=service_schema
)
hass.services.async_register(
ha.DOMAIN, SERVICE_TURN_ON, async_handle_turn_service, schema=service_schema
DOMAIN, SERVICE_TURN_ON, async_handle_turn_service, schema=service_schema
)
hass.services.async_register(
ha.DOMAIN, SERVICE_TOGGLE, async_handle_turn_service, schema=service_schema
DOMAIN, SERVICE_TOGGLE, async_handle_turn_service, schema=service_schema
)
async def async_handle_core_service(call: ha.ServiceCall) -> None:
async def async_handle_core_service(call: ServiceCall) -> None:
"""Service handler for handling core services."""
stop_handler: Callable[[ha.HomeAssistant, bool], Coroutine[Any, Any, None]]
stop_handler: Callable[[HomeAssistant, bool], Coroutine[Any, Any, None]]
if call.service in SHUTDOWN_SERVICES and recorder.async_migration_in_progress(
hass
@ -193,7 +199,7 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
hass,
"Config error. See [the logs](/config/logs) for details.",
"Config validating",
f"{ha.DOMAIN}.check_config",
f"{DOMAIN}.check_config",
)
raise HomeAssistantError(
f"The system cannot {call.service} "
@ -206,7 +212,7 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
stop_handler = hass.data[DATA_STOP_HANDLER]
await stop_handler(hass, True)
async def async_handle_update_service(call: ha.ServiceCall) -> None:
async def async_handle_update_service(call: ServiceCall) -> None:
"""Service handler for updating an entity."""
if call.context.user_id:
user = await hass.auth.async_get_user(call.context.user_id)
@ -235,26 +241,26 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
await asyncio.gather(*tasks)
async_register_admin_service(
hass, ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service
hass, DOMAIN, SERVICE_HOMEASSISTANT_STOP, async_handle_core_service
)
async_register_admin_service(
hass,
ha.DOMAIN,
DOMAIN,
SERVICE_HOMEASSISTANT_RESTART,
async_handle_core_service,
SCHEMA_RESTART,
)
async_register_admin_service(
hass, ha.DOMAIN, SERVICE_CHECK_CONFIG, async_handle_core_service
hass, DOMAIN, SERVICE_CHECK_CONFIG, async_handle_core_service
)
hass.services.async_register(
ha.DOMAIN,
DOMAIN,
SERVICE_UPDATE_ENTITY,
async_handle_update_service,
schema=SCHEMA_UPDATE_ENTITY,
)
async def async_handle_reload_config(call: ha.ServiceCall) -> None:
async def async_handle_reload_config(call: ServiceCall) -> None:
"""Service handler for reloading core config."""
try:
conf = await conf_util.async_hass_config_yaml(hass)
@ -263,13 +269,13 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
return
# auth only processed during startup
await conf_util.async_process_ha_core_config(hass, conf.get(ha.DOMAIN) or {})
await conf_util.async_process_ha_core_config(hass, conf.get(DOMAIN) or {})
async_register_admin_service(
hass, ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config
hass, DOMAIN, SERVICE_RELOAD_CORE_CONFIG, async_handle_reload_config
)
async def async_set_location(call: ha.ServiceCall) -> None:
async def async_set_location(call: ServiceCall) -> None:
"""Service handler to set location."""
service_data = {
"latitude": call.data[ATTR_LATITUDE],
@ -283,7 +289,7 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
async_register_admin_service(
hass,
ha.DOMAIN,
DOMAIN,
SERVICE_SET_LOCATION,
async_set_location,
vol.Schema(
@ -295,15 +301,15 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
),
)
async def async_handle_reload_templates(call: ha.ServiceCall) -> None:
async def async_handle_reload_templates(call: ServiceCall) -> None:
"""Service handler to reload custom Jinja."""
await async_load_custom_templates(hass)
async_register_admin_service(
hass, ha.DOMAIN, SERVICE_RELOAD_CUSTOM_TEMPLATES, async_handle_reload_templates
hass, DOMAIN, SERVICE_RELOAD_CUSTOM_TEMPLATES, async_handle_reload_templates
)
async def async_handle_reload_config_entry(call: ha.ServiceCall) -> None:
async def async_handle_reload_config_entry(call: ServiceCall) -> None:
"""Service handler for reloading a config entry."""
reload_entries: set[str] = set()
if ATTR_ENTRY_ID in call.data:
@ -320,13 +326,13 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
async_register_admin_service(
hass,
ha.DOMAIN,
DOMAIN,
SERVICE_RELOAD_CONFIG_ENTRY,
async_handle_reload_config_entry,
schema=SCHEMA_RELOAD_CONFIG_ENTRY,
)
async def async_handle_reload_all(call: ha.ServiceCall) -> None:
async def async_handle_reload_all(call: ServiceCall) -> None:
"""Service handler for calling all integration reload services.
Calls all reload services on all active domains, which triggers the
@ -363,16 +369,16 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
domain, service, context=call.context, blocking=True
)
for domain, service in (
(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG),
(DOMAIN, SERVICE_RELOAD_CORE_CONFIG),
("frontend", "reload_themes"),
(ha.DOMAIN, SERVICE_RELOAD_CUSTOM_TEMPLATES),
(DOMAIN, SERVICE_RELOAD_CUSTOM_TEMPLATES),
)
]
await asyncio.gather(*tasks)
async_register_admin_service(
hass, ha.DOMAIN, SERVICE_RELOAD_ALL, async_handle_reload_all
hass, DOMAIN, SERVICE_RELOAD_ALL, async_handle_reload_all
)
exposed_entities = ExposedEntities(hass)
@ -383,17 +389,17 @@ async def async_setup(hass: ha.HomeAssistant, config: ConfigType) -> bool: # no
return True
async def _async_stop(hass: ha.HomeAssistant, restart: bool) -> None:
async def _async_stop(hass: HomeAssistant, restart: bool) -> None:
"""Stop home assistant."""
exit_code = RESTART_EXIT_CODE if restart else 0
# Track trask in hass.data. No need to cleanup, we're stopping.
hass.data[KEY_HA_STOP] = asyncio.create_task(hass.async_stop(exit_code))
@ha.callback
@callback
def async_set_stop_handler(
hass: ha.HomeAssistant,
stop_handler: Callable[[ha.HomeAssistant, bool], Coroutine[Any, Any, None]],
hass: HomeAssistant,
stop_handler: Callable[[HomeAssistant, bool], Coroutine[Any, Any, None]],
) -> None:
"""Set function which is called by the stop and restart services."""
hass.data[DATA_STOP_HANDLER] = stop_handler

View File

@ -15,7 +15,7 @@ from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers.typing import NoEventData
from homeassistant.util.event_type import EventType
from . import DOMAIN
from .const import DOMAIN
EVENT_TO_NAME: dict[EventType[Any] | str, str] = {
EVENT_HOMEASSISTANT_STOP: "stopped",

View File

@ -23,13 +23,7 @@ from homeassistant.const import (
STATE_OFF,
STATE_ON,
)
from homeassistant.core import (
DOMAIN as HOMEASSISTANT_DOMAIN,
HomeAssistant,
ServiceCall,
State,
callback,
)
from homeassistant.core import HomeAssistant, ServiceCall, State, callback
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.entity_platform import AddEntitiesCallback, EntityPlatform
@ -41,6 +35,8 @@ from homeassistant.helpers.state import async_reproduce_state
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.loader import async_get_integration
from .const import DOMAIN
def _convert_states(states: dict[str, Any]) -> dict[str, State]:
"""Convert state definitions to State objects."""
@ -92,7 +88,7 @@ STATES_SCHEMA = vol.All(dict, _convert_states)
PLATFORM_SCHEMA = vol.Schema(
{
vol.Required(CONF_PLATFORM): HOMEASSISTANT_DOMAIN,
vol.Required(CONF_PLATFORM): DOMAIN,
vol.Required(STATES): vol.All(
cv.ensure_list,
[
@ -206,7 +202,7 @@ async def async_setup_platform(
# Extract only the config for the Home Assistant platform, ignore the rest.
for p_type, p_config in conf_util.config_per_platform(conf, SCENE_DOMAIN):
if p_type != HOMEASSISTANT_DOMAIN:
if p_type != DOMAIN:
continue
_process_scenes_config(hass, async_add_entities, p_config)