Skip to content

Commit

Permalink
Add enable/disable config entry services
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed Feb 23, 2023
1 parent 6e28288 commit 235ec78
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 6 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ Don't.

## Configuration

You shouldn't
You shouldn't.

## Services

`spook.random_fail` - Will randomly fail and raise an error.

`homeassistant.disable_config_entry` - Will disable an integration config entry.
`homeassistant.enable_config_entry` - Will enable an integration config entry.

## Changelog & Releases

Expand All @@ -52,7 +59,6 @@ the version will be incremented based on the following:
We've set up a separate document for our
[contribution guidelines](CONTRIBUTING.md).


## Authors & contributors

The original setup of this repository is by [Franck Nijhof][frenck].
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from a config entry."""
async_setup_services(hass)
await async_setup_services(hass)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True

Expand Down
1 change: 0 additions & 1 deletion custom_components/spook/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Any

import voluptuous as vol

from homeassistant.config_entries import ConfigFlow
from homeassistant.data_entry_flow import FlowResult

Expand Down
1 change: 1 addition & 0 deletions custom_components/spook/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"issue_tracker": "https://github.com/frenck/spook/issues",
"quality_scale": "platinum",
"requirements": [],
"dependencies": ["homeassistant"],
"version": "0.0.1"
}
26 changes: 25 additions & 1 deletion custom_components/spook/services.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
random_fail:
name: Random fail
name: Random fail 👻
description: A call to this service will randomly fail.

disable_config_entry:
name: Disable a config entry 👻
description: >-
Disables a integration configuration entry.
fields:
config_entry_id:
name: Config entry
description: The integration configuration entry to disable.
required: true
selector:
config_entry:

enable_config_entry:
name: Enable a config entry 👻
description: >-
Enables a integration configuration entry.
fields:
config_entry_id:
name: Config entry
description: The integration configuration entry to enable.
required: true
selector:
config_entry:
57 changes: 56 additions & 1 deletion custom_components/spook/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,36 @@
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.backports.enum import StrEnum
from homeassistant.config_entries import ConfigEntryDisabler
from homeassistant.loader import async_get_integration
from homeassistant.helpers.service import _load_services_file, async_set_service_schema
async_set_service_schema,
_load_services_file,
)
from homeassistant.components import homeassistant


from ..const import DOMAIN


class SpookServices(StrEnum):
"""Spook services."""

DISABLE_CONFIG_ENTRY = "disable_config_entry"
ENABLE_CONFIG_ENTRY = "enable_config_entry"
RANDOM_FAIL = "random_fail"


@callback
def async_setup_services(hass: HomeAssistant) -> None:
async def async_setup_services(hass: HomeAssistant) -> None:
"""Set up Spook services."""
# Ensure cache is populated
integration = await async_get_integration(hass, DOMAIN)
services_file = await hass.async_add_executor_job(
_load_services_file, hass, integration
)

print(services_file)

async def _async_random_fail(_: ServiceCall) -> None:
"""Randomly let this service call fail."""
Expand All @@ -28,3 +45,41 @@ async def _async_random_fail(_: ServiceCall) -> None:
SpookServices.RANDOM_FAIL,
_async_random_fail,
)

async def _async_disable_config_entry(call: ServiceCall) -> None:
"""Service to disable a config entry."""
await hass.config_entries.async_set_disabled_by(
call.data["config_entry_id"], ConfigEntryDisabler.USER
)

hass.services.async_register(
homeassistant.DOMAIN,
SpookServices.DISABLE_CONFIG_ENTRY,
_async_disable_config_entry,
)

async_set_service_schema(
hass,
homeassistant.DOMAIN,
SpookServices.DISABLE_CONFIG_ENTRY,
services_file[SpookServices.DISABLE_CONFIG_ENTRY],
)

async def _async_enable_config_entry(call: ServiceCall) -> None:
"""Service to disable a config entry."""
await hass.config_entries.async_set_disabled_by(
call.data["config_entry_id"], None
)

hass.services.async_register(
homeassistant.DOMAIN,
SpookServices.ENABLE_CONFIG_ENTRY,
_async_enable_config_entry,
)

async_set_service_schema(
hass,
homeassistant.DOMAIN,
SpookServices.ENABLE_CONFIG_ENTRY,
services_file[SpookServices.ENABLE_CONFIG_ENTRY],
)

0 comments on commit 235ec78

Please sign in to comment.