Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4nt0r committed May 1, 2024
1 parent d9323fb commit 6ac8ad6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
11 changes: 8 additions & 3 deletions homeassistant/components/habitica/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The habitica integration."""

import logging
from typing import TYPE_CHECKING

from habitipy.aio import HabitipyAsync
import voluptuous as vol
Expand Down Expand Up @@ -119,12 +120,15 @@ def __call__(self, **kwargs):
async def handle_api_call(call: ServiceCall) -> None:
path = call.data[ATTR_PATH]
entry_id: str = call.data[ATTR_CONFIG_ENTRY]
entry: ConfigEntry | None = hass.config_entries.async_get_entry(entry_id)
if TYPE_CHECKING:
assert entry

api = None
api = hass.data[DOMAIN].get(entry_id)

if api is None:
_LOGGER.error("API_CALL: User '%s' not configured", name)
_LOGGER.error("API_CALL: User '%s' not configured", entry.title)
return
try:
for element in path:
Expand All @@ -137,7 +141,8 @@ async def handle_api_call(call: ServiceCall) -> None:
kwargs = call.data.get(ATTR_ARGS, {})
data = await api(**kwargs)
hass.bus.async_fire(
EVENT_API_CALL_SUCCESS, {ATTR_NAME: name, ATTR_PATH: path, ATTR_DATA: data}
EVENT_API_CALL_SUCCESS,
{ATTR_NAME: entry.title, ATTR_PATH: path, ATTR_DATA: data},
)

data = hass.data.setdefault(DOMAIN, {})
Expand All @@ -148,7 +153,7 @@ async def handle_api_call(call: ServiceCall) -> None:
url = config[CONF_URL]
username = config[CONF_API_USER]
password = config[CONF_API_KEY]
name = config.get(CONF_NAME)

config_dict = {"url": url, "login": username, "password": password}
api = HAHabitipyAsync(config_dict)

Expand Down
7 changes: 2 additions & 5 deletions homeassistant/components/habitica/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_URL
from homeassistant.const import CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -168,7 +168,6 @@ async def async_setup_entry(
) -> None:
"""Set up the habitica sensors."""

name = config_entry.data[CONF_NAME]
sensor_data = HabitipyData(hass.data[DOMAIN][config_entry.entry_id])
await sensor_data.update()

Expand All @@ -177,7 +176,7 @@ async def async_setup_entry(
for description in SENSOR_DESCRIPTIONS.values()
]
entities.extend(
HabitipyTaskSensor(name, task_type, sensor_data, config_entry)
HabitipyTaskSensor(config_entry.title, task_type, sensor_data, config_entry)
for task_type in TASKS_TYPES
)
async_add_entities(entities, True)
Expand Down Expand Up @@ -258,7 +257,6 @@ def __init__(
entry_type=DeviceEntryType.SERVICE,
manufacturer=MANUFACTURER,
model=NAME,
name=entry.data[CONF_NAME],
configuration_url=entry.data[CONF_URL],
identifiers={(DOMAIN, entry.unique_id)},
)
Expand Down Expand Up @@ -287,7 +285,6 @@ def __init__(self, name, task_name, updater, entry):
entry_type=DeviceEntryType.SERVICE,
manufacturer=MANUFACTURER,
model=NAME,
name=entry.data[CONF_NAME],
configuration_url=entry.data[CONF_URL],
identifiers={(DOMAIN, entry.unique_id)},
)
Expand Down
11 changes: 3 additions & 8 deletions tests/components/habitica/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ async def test_form_login(hass: HomeAssistant) -> None:

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "test-username"
assert result["data"] == {
**MOCK_DATA_ADVANCED_STEP,
CONF_USERNAME: "test-username",
}
assert result["data"] == MOCK_DATA_ADVANCED_STEP

assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1

Expand Down Expand Up @@ -171,10 +169,7 @@ async def test_form_advanced(hass: HomeAssistant) -> None:

assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["title"] == "test-username"
assert result2["data"] == {
**MOCK_DATA_ADVANCED_STEP,
CONF_USERNAME: "test-username",
}
assert result2["data"] == MOCK_DATA_ADVANCED_STEP
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1

Expand Down
10 changes: 7 additions & 3 deletions tests/components/habitica/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from homeassistant.components.habitica.const import (
ATTR_ARGS,
ATTR_CONFIG_ENTRY,
ATTR_DATA,
ATTR_PATH,
DEFAULT_URL,
Expand All @@ -14,7 +15,6 @@
SERVICE_API_CALL,
)
from homeassistant.components.habitica.sensor import TASKS_TYPES
from homeassistant.const import ATTR_NAME
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry, async_capture_events
Expand Down Expand Up @@ -121,7 +121,7 @@ async def test_service_call(
assert len(capture_api_call_success) == 0

TEST_SERVICE_DATA = {
ATTR_NAME: "test_user",
ATTR_CONFIG_ENTRY: habitica_entry.entry_id,
ATTR_PATH: ["tasks", "user", "post"],
ATTR_ARGS: TEST_API_CALL_ARGS,
}
Expand All @@ -133,7 +133,11 @@ async def test_service_call(
captured_data = capture_api_call_success[0].data
captured_data[ATTR_ARGS] = captured_data[ATTR_DATA]
del captured_data[ATTR_DATA]
assert captured_data == TEST_SERVICE_DATA
assert captured_data == {
"name": habitica_entry.title,
ATTR_PATH: ["tasks", "user", "post"],
ATTR_ARGS: TEST_API_CALL_ARGS,
}

assert await hass.config_entries.async_unload(habitica_entry.entry_id)

Expand Down

0 comments on commit 6ac8ad6

Please sign in to comment.