Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions custom_components/keymaster/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,28 @@ async def async_register_strategy_resource(hass: HomeAssistant) -> None:
_LOGGER.debug("Manually loaded resources")
resources.loaded = True

try:
res_id = next(
data[CONF_ID] for data in resources.async_items() if data[CONF_URL] == STRATEGY_PATH
)
except StopIteration:
if isinstance(resources, ResourceYAMLCollection):
_LOGGER.warning(
"Strategy module can't automatically be registered because this "
"Home Assistant instance is running in YAML mode for resources. "
"Please add a new entry in the list under the resources key in "
'the lovelace section of your config as follows:\n - url: "%s"'
"\n type: module",
STRATEGY_PATH,
)
return
already_registered = any(data[CONF_URL] == STRATEGY_PATH for data in resources.async_items())

if already_registered:
_LOGGER.debug("Strategy module already registered")
return

data = await resources.async_create_item(
{CONF_RESOURCE_TYPE_WS: "module", CONF_URL: STRATEGY_PATH}
if isinstance(resources, ResourceYAMLCollection):
_LOGGER.warning(
"Strategy module can't automatically be registered because this "
"Home Assistant instance is running in YAML mode for resources. "
"Please add a new entry in the list under the resources key in "
'the lovelace section of your config as follows:\n - url: "%s"'
"\n type: module",
STRATEGY_PATH,
)
_LOGGER.debug("Registered strategy module (resource ID %s)", data[CONF_ID])
hass.data[DOMAIN]["resources"] = True
return

_LOGGER.debug("Strategy module already registered with resource ID %s", res_id)
data = await resources.async_create_item(
{CONF_RESOURCE_TYPE_WS: "module", CONF_URL: STRATEGY_PATH}
)
_LOGGER.debug("Registered strategy module (resource ID %s)", data[CONF_ID])
hass.data[DOMAIN]["resources"] = True


async def async_cleanup_strategy_resource(hass: HomeAssistant, hass_data: dict[str, Any]) -> None:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ async def test_register_strategy_resource_yaml_mode_warning(
assert STRATEGY_PATH in caplog.text


async def test_register_strategy_resource_yaml_mode_already_exists(
hass: HomeAssistant, mock_yaml_resources, caplog
):
"""Test no error when resource already exists in YAML mode (no 'id' field)."""
mock_yaml_resources.async_items.return_value = [
{"url": "/local/other.js", "type": "module"},
{"url": STRATEGY_PATH, "type": "module"},
]
hass.data[LOVELACE_DOMAIN] = MagicMock()
hass.data[LOVELACE_DOMAIN].resources = mock_yaml_resources
hass.data[DOMAIN] = {}

with caplog.at_level(logging.DEBUG):
await async_register_strategy_resource(hass)

assert "already registered" in caplog.text
assert "YAML mode" not in caplog.text


async def test_register_strategy_resource_no_resources(hass: HomeAssistant):
"""Test registering when no resources available."""
# No lovelace domain
Expand Down