Skip to content

Commit cbfbbcd

Browse files
committed
Fix bugs and add tests
1 parent 10ce72c commit cbfbbcd

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
lines changed

custom_components/lock_code_manager/__init__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,14 @@
6969
async def async_setup(hass: HomeAssistant, config: Config) -> bool:
7070
"""Set up integration."""
7171
hass.data.setdefault(DOMAIN, {CONF_LOCKS: {}, COORDINATORS: {}, "resources": False})
72+
# Expose strategy javascript
73+
hass.http.register_static_path(
74+
STRATEGY_PATH, Path(__file__).parent / "www" / STRATEGY_FILENAME
75+
)
76+
_LOGGER.debug("Exposed strategy module at %s", STRATEGY_PATH)
7277

7378
resources: ResourceStorageCollection | ResourceYAMLCollection
7479
if resources := hass.data.get(LL_DOMAIN, {}).get("resources"):
75-
# Expose strategy javascript
76-
hass.http.register_static_path(
77-
STRATEGY_PATH, Path(__file__).parent / "www" / STRATEGY_FILENAME
78-
)
79-
_LOGGER.debug("Exposed strategy module at %s", STRATEGY_PATH)
80-
8180
# Load resources if needed
8281
if not resources.loaded:
8382
await resources.async_load()
@@ -86,7 +85,7 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:
8685

8786
try:
8887
res_id = next(
89-
data[CONF_ID]
88+
data.get(CONF_ID)
9089
for data in resources.async_items()
9190
if data[CONF_URL] == STRATEGY_PATH
9291
)
@@ -114,9 +113,9 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:
114113
"Strategy module already registered with resource ID %s", res_id
115114
)
116115

117-
# Set up websocket API
118-
await async_websocket_setup(hass)
119-
_LOGGER.debug("Finished setting up websocket API")
116+
# Set up websocket API
117+
await async_websocket_setup(hass)
118+
_LOGGER.debug("Finished setting up websocket API")
120119

121120
# Hard refresh usercodes
122121
async def _hard_refresh_usercodes(service: ServiceCall) -> None:
@@ -266,7 +265,7 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
266265
COORDINATORS: {},
267266
}:
268267
resources: ResourceStorageCollection
269-
if resources := hass.data[LL_DOMAIN].get("resources"):
268+
if resources := hass.data.get(LL_DOMAIN, {}).get("resources"):
270269
if hass_data["resources"]:
271270
try:
272271
resource_id = next(

tests/conftest.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from collections.abc import Generator
6+
from typing import Any
67

78
import pytest
89
from pytest_homeassistant_custom_component.common import (
@@ -57,6 +58,20 @@ def config_flow_fixture(hass: HomeAssistant) -> Generator[None, None, None]:
5758
yield
5859

5960

61+
# @pytest.fixture(name="setup_lovelace_ui")
62+
# async def setup_lovelace_ui_fixture(hass: HomeAssistant):
63+
# """Set up Lovelace in UI mode."""
64+
# assert await async_setup_component(hass, LL_DOMAIN, {})
65+
# yield
66+
67+
68+
@pytest.fixture(name="setup_lovelace_ui")
69+
async def setup_lovelace_ui_fixture(hass: HomeAssistant, config: dict[str, Any]):
70+
"""Set up Lovelace in UI mode."""
71+
assert await async_setup_component(hass, LL_DOMAIN, {"lovelace": config})
72+
yield
73+
74+
6075
@pytest.fixture(name="mock_lock_config_entry")
6176
async def mock_lock_config_entry_fixture(hass: HomeAssistant, mock_config_flow):
6277
"""Set up lock entities using an entity platform."""
@@ -126,8 +141,6 @@ async def async_setup_entry_calendar_platform(
126141
assert await hass.config_entries.async_setup(config_entry.entry_id)
127142
await hass.async_block_till_done()
128143

129-
assert await async_setup_component(hass, LL_DOMAIN, {})
130-
131144
yield config_entry
132145

133146
await hass.config_entries.async_unload(config_entry.entry_id)

tests/test_init.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
_LOGGER = logging.getLogger(__name__)
4444

4545

46+
@pytest.mark.parametrize("config", [{}])
4647
async def test_entry_setup_and_unload(
4748
hass: HomeAssistant,
49+
setup_lovelace_ui,
4850
mock_lock_config_entry,
4951
lock_code_manager_config_entry,
5052
):
@@ -202,13 +204,15 @@ async def test_reauth(hass: HomeAssistant, lock_code_manager_config_entry):
202204
)
203205

204206

205-
async def test_resource_already_loaded(
207+
@pytest.mark.parametrize("config", [{}])
208+
async def test_resource_already_loaded_ui(
206209
hass: HomeAssistant,
210+
setup_lovelace_ui,
207211
mock_lock_config_entry,
208212
caplog: pytest.LogCaptureFixture,
209213
monkeypatch: pytest.MonkeyPatch,
210214
):
211-
"""Test when strategy resource is already loaded."""
215+
"""Test when strategy resource is already loaded in UI mode."""
212216
resources = hass.data[LL_DOMAIN].get("resources")
213217
assert resources
214218
await resources.async_load()
@@ -231,3 +235,61 @@ async def test_resource_already_loaded(
231235
assert "already registered" in caplog.text
232236

233237
await hass.config_entries.async_unload(config_entry.entry_id)
238+
239+
240+
@pytest.mark.parametrize(
241+
"config",
242+
[{"mode": "yaml", "resources": [{"type": "module", "url": STRATEGY_PATH}]}],
243+
)
244+
async def test_resource_already_loaded_yaml(
245+
hass: HomeAssistant,
246+
setup_lovelace_ui,
247+
mock_lock_config_entry,
248+
monkeypatch: pytest.MonkeyPatch,
249+
caplog: pytest.LogCaptureFixture,
250+
):
251+
"""Test when strategy resource is already loaded in YAML mode."""
252+
monkeypatch.setattr(
253+
"custom_components.lock_code_manager.helpers.INTEGRATIONS_CLASS_MAP",
254+
{"test": MockLCMLock},
255+
)
256+
257+
config_entry = MockConfigEntry(
258+
domain=DOMAIN, data=BASE_CONFIG, unique_id="Mock Title"
259+
)
260+
config_entry.add_to_hass(hass)
261+
await hass.config_entries.async_setup(config_entry.entry_id)
262+
await hass.async_block_till_done()
263+
264+
assert "already registered" in caplog.text
265+
266+
await hass.config_entries.async_unload(config_entry.entry_id)
267+
268+
269+
@pytest.mark.parametrize(
270+
"config",
271+
[{"mode": "yaml", "resources": [{"type": "module", "url": "fake_module.js"}]}],
272+
)
273+
async def test_resource_not_loaded_yaml(
274+
hass: HomeAssistant,
275+
setup_lovelace_ui,
276+
mock_lock_config_entry,
277+
monkeypatch: pytest.MonkeyPatch,
278+
caplog: pytest.LogCaptureFixture,
279+
):
280+
"""Test when strategy resource is not loaded in YAML mode."""
281+
monkeypatch.setattr(
282+
"custom_components.lock_code_manager.helpers.INTEGRATIONS_CLASS_MAP",
283+
{"test": MockLCMLock},
284+
)
285+
286+
config_entry = MockConfigEntry(
287+
domain=DOMAIN, data=BASE_CONFIG, unique_id="Mock Title"
288+
)
289+
config_entry.add_to_hass(hass)
290+
await hass.config_entries.async_setup(config_entry.entry_id)
291+
await hass.async_block_till_done()
292+
293+
assert "module can't automatically be registered" in caplog.text
294+
295+
await hass.config_entries.async_unload(config_entry.entry_id)

0 commit comments

Comments
 (0)