Skip to content

Commit

Permalink
check if entry is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4nt0r committed Oct 6, 2024
1 parent 23dc760 commit 9fb8866
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
28 changes: 7 additions & 21 deletions homeassistant/components/habitica/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""The habitica integration."""

import logging
from http import HTTPStatus
import logging
from typing import Any, cast

import voluptuous as vol
from aiohttp import ClientResponseError
from habitipy.aio import HabitipyAsync
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -56,6 +56,7 @@
SERVICE_GET_TASKS,
)
from .coordinator import HabiticaDataUpdateCoordinator
from .util import get_config_entry

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -153,14 +154,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

async def cast_skill(call: ServiceCall) -> ServiceResponse:
"""Skill action."""
entry: HabiticaConfigEntry | None
if not (
entry := hass.config_entries.async_get_entry(call.data[ATTR_CONFIG_ENTRY])
):
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="entry_not_found",
)

entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])

Check warning on line 158 in homeassistant/components/habitica/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/habitica/__init__.py#L158

Added line #L158 was not covered by tests
coordinator = entry.runtime_data
skill = {
"pickpocket": {"spellId": "pickPocket", "cost": "10 MP"},
Expand Down Expand Up @@ -219,19 +214,10 @@ async def cast_skill(call: ServiceCall) -> ServiceResponse:
return response

async def get_tasks(call: ServiceCall) -> ServiceResponse:
"""Skill action."""
entry: HabiticaConfigEntry | None

if not (
entry := hass.config_entries.async_get_entry(call.data[ATTR_CONFIG_ENTRY])
):
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="entry_not_found",
)
"""Get tasks action."""

entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY])
coordinator = entry.runtime_data

response = coordinator.data.tasks

Check warning on line 221 in homeassistant/components/habitica/__init__.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/habitica/__init__.py#L219-L221

Added lines #L219 - L221 were not covered by tests

if types := call.data.get(ATTR_TYPE):
Expand Down
17 changes: 17 additions & 0 deletions homeassistant/components/habitica/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

from homeassistant.components.automation import automations_with_entity
from homeassistant.components.script import scripts_with_entity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.util import dt as dt_util

from .const import DOMAIN


def next_due_date(task: dict[str, Any], last_cron: str) -> datetime.date | None:
"""Calculate due date for dailies and yesterdailies."""
Expand Down Expand Up @@ -59,3 +63,16 @@ def entity_used_in(hass: HomeAssistant, entity_id: str) -> list[str]:
used_in = automations_with_entity(hass, entity_id)
used_in += scripts_with_entity(hass, entity_id)
return used_in


def get_config_entry(hass: HomeAssistant, entry_id: str) -> ConfigEntry:
"""Return config entry or raise if not found or not loaded."""
entry: ConfigEntry | None
if not (

Check warning on line 71 in homeassistant/components/habitica/util.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/habitica/util.py#L71

Added line #L71 was not covered by tests
entry := hass.config_entries.async_get_entry(entry_id)
) or entry not in hass.config_entries.async_loaded_entries(DOMAIN):
raise ServiceValidationError(

Check warning on line 74 in homeassistant/components/habitica/util.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/habitica/util.py#L74

Added line #L74 was not covered by tests
translation_domain=DOMAIN,
translation_key="entry_not_found",
)
return entry

Check warning on line 78 in homeassistant/components/habitica/util.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/habitica/util.py#L78

Added line #L78 was not covered by tests

0 comments on commit 9fb8866

Please sign in to comment.