Skip to content

Commit

Permalink
Added enable and disable config entry services.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneQi committed Aug 28, 2022
1 parent 441d7c0 commit b41fd0e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
58 changes: 54 additions & 4 deletions homeassistant/components/homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from homeassistant.auth.permissions.const import CAT_ENTITIES, POLICY_CONTROL
from homeassistant.components import persistent_notification
import homeassistant.config as conf_util
from homeassistant.config_entries import ConfigEntryDisabler
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_LATITUDE,
Expand Down Expand Up @@ -37,11 +38,13 @@
DOMAIN = ha.DOMAIN
SERVICE_RELOAD_CORE_CONFIG = "reload_core_config"
SERVICE_RELOAD_CONFIG_ENTRY = "reload_config_entry"
SERVICE_ENABLE_CONFIG_ENTRY = "enable_config_entry"
SERVICE_DISABLE_CONFIG_ENTRY = "disable_config_entry"
SERVICE_CHECK_CONFIG = "check_config"
SERVICE_UPDATE_ENTITY = "update_entity"
SERVICE_SET_LOCATION = "set_location"
SCHEMA_UPDATE_ENTITY = vol.Schema({ATTR_ENTITY_ID: cv.entity_ids})
SCHEMA_RELOAD_CONFIG_ENTRY = vol.All(
SCHEMA_RELOAD_ENABLE_DISABLE_CONFIG_ENTRY = vol.All(
vol.Schema(
{
vol.Optional(ATTR_ENTRY_ID): str,
Expand Down Expand Up @@ -252,12 +255,16 @@ async def async_set_location(call: ha.ServiceCall) -> None:
vol.Schema({ATTR_LATITUDE: cv.latitude, ATTR_LONGITUDE: cv.longitude}),
)

async def async_handle_reload_config_entry(call: ha.ServiceCall) -> None:
"""Service handler for reloading a config entry."""
async def async_get_entries_ids_set(call: ha.ServiceCall) -> set:
reload_entries = set()
if ATTR_ENTRY_ID in call.data:
reload_entries.add(call.data[ATTR_ENTRY_ID])
reload_entries.update(await async_extract_config_entry_ids(hass, call))
return reload_entries

async def async_handle_reload_config_entry(call: ha.ServiceCall) -> None:
"""Service handler for reloading a config entry."""
reload_entries = await async_get_entries_ids_set(call)
if not reload_entries:
raise ValueError("There were no matching config entries to reload")
await asyncio.gather(
Expand All @@ -272,7 +279,50 @@ async def async_handle_reload_config_entry(call: ha.ServiceCall) -> None:
ha.DOMAIN,
SERVICE_RELOAD_CONFIG_ENTRY,
async_handle_reload_config_entry,
schema=SCHEMA_RELOAD_CONFIG_ENTRY,
schema=SCHEMA_RELOAD_ENABLE_DISABLE_CONFIG_ENTRY,
)

async def async_handle_enable_config_entry(call: ha.ServiceCall) -> None:
"""Service handler for enabling a config entry."""
enable_entries = await async_get_entries_ids_set(call)
if not enable_entries:
raise ValueError("There were no matching config entries to enable")
await asyncio.gather(
*(
hass.config_entries.async_set_disabled_by(config_entry_id, None)
for config_entry_id in enable_entries
)
)

async_register_admin_service(
hass,
ha.DOMAIN,
SERVICE_ENABLE_CONFIG_ENTRY,
async_handle_enable_config_entry,
schema=SCHEMA_RELOAD_ENABLE_DISABLE_CONFIG_ENTRY,
)

async def async_handle_disable_config_entry(call: ha.ServiceCall) -> None:
"""Service handler for disabling a config entry."""
disable_entries = await async_get_entries_ids_set(call)
if not disable_entries:
raise ValueError("There were no matching config entries to disable")
await asyncio.gather(
*(
hass.config_entries.async_set_disabled_by(
config_entry_id,
ConfigEntryDisabler.USER,
)
for config_entry_id in disable_entries
)
)

async_register_admin_service(
hass,
ha.DOMAIN,
SERVICE_DISABLE_CONFIG_ENTRY,
async_handle_disable_config_entry,
schema=SCHEMA_RELOAD_ENABLE_DISABLE_CONFIG_ENTRY,
)

return True
32 changes: 32 additions & 0 deletions homeassistant/components/homeassistant/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ reload_config_entry:
selector:
text:

enable_config_entry:
name: Enable config entry
description: Enable a config entry that matches a target.
target:
entity: {}
device: {}
fields:
entry_id:
advanced: true
name: Config entry id
description: A configuration entry id
required: false
example: 8955375327824e14ba89e4b29cc3ec9a
selector:
text:

disable_config_entry:
name: Disable config entry
description: Disable a config entry that matches a target.
target:
entity: {}
device: {}
fields:
entry_id:
advanced: true
name: Config entry id
description: A configuration entry id
required: false
example: 8955375327824e14ba89e4b29cc3ec9a
selector:
text:

save_persistent_states:
name: Save Persistent States
description:
Expand Down

0 comments on commit b41fd0e

Please sign in to comment.