Skip to content

Commit

Permalink
Add random failing service
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed Feb 23, 2023
1 parent 70b1063 commit 6e28288
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions custom_components/spook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from homeassistant.core import HomeAssistant

from .const import PLATFORMS
from .services import async_setup_services


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

Expand Down
2 changes: 1 addition & 1 deletion custom_components/spook/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def async_step_user(
) -> FlowResult:
"""Handle a flow initialized someone that didn't read the warnings."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
return self.async_abort(reason="already_spooked")

if user_input is not None:
return self.async_create_entry(title="Not your homey", data={})
Expand Down
3 changes: 3 additions & 0 deletions custom_components/spook/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
random_fail:
name: Random fail
description: A call to this service will randomly fail.
30 changes: 30 additions & 0 deletions custom_components/spook/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Spook - Not your homey."""
import random

from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.backports.enum import StrEnum

from ..const import DOMAIN


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

RANDOM_FAIL = "random_fail"


@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up Spook services."""

async def _async_random_fail(_: ServiceCall) -> None:
"""Randomly let this service call fail."""
if random.choice([True, False]):
raise HomeAssistantError("Spooked!")

hass.services.async_register(
DOMAIN,
SpookServices.RANDOM_FAIL,
_async_random_fail,
)
2 changes: 1 addition & 1 deletion custom_components/spook/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"config": {
"abort": {
"single_instance_allowed": "Yeah... well... no. K, thx, bye!"
"already_spooked": "Yeah... well... no. K, thx, bye!"
},
"step": {
"user": {
Expand Down

0 comments on commit 6e28288

Please sign in to comment.