Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default_db_url flag to WS command recorder/info #139333

Merged
merged 2 commits into from
Feb 26, 2025
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
9 changes: 6 additions & 3 deletions homeassistant/components/recorder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
commit_interval = conf[CONF_COMMIT_INTERVAL]
db_max_retries = conf[CONF_DB_MAX_RETRIES]
db_retry_wait = conf[CONF_DB_RETRY_WAIT]
db_url = conf.get(CONF_DB_URL) or DEFAULT_URL.format(
hass_config_path=hass.config.path(DEFAULT_DB_FILE)
)
db_url = conf.get(CONF_DB_URL) or get_default_url(hass)
exclude = conf[CONF_EXCLUDE]
exclude_event_types: set[EventType[Any] | str] = set(
exclude.get(CONF_EVENT_TYPES, [])
Expand Down Expand Up @@ -200,3 +198,8 @@ def _process_recorder_platform(
instance.queue_task(AddRecorderPlatformTask(domain, platform))

await async_process_integration_platforms(hass, DOMAIN, _process_recorder_platform)


def get_default_url(hass: HomeAssistant) -> str:
"""Return the default URL."""
return DEFAULT_URL.format(hass_config_path=hass.config.path(DEFAULT_DB_FILE))
3 changes: 3 additions & 0 deletions homeassistant/components/recorder/basic_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import recorder as recorder_helper

from . import get_default_url
from .util import get_instance


Expand All @@ -34,6 +35,7 @@ async def ws_info(
await hass.data[recorder_helper.DATA_RECORDER].db_connected
instance = get_instance(hass)
backlog = instance.backlog
db_in_default_location = instance.db_url == get_default_url(hass)
migration_in_progress = instance.migration_in_progress
migration_is_live = instance.migration_is_live
recording = instance.recording
Expand All @@ -44,6 +46,7 @@ async def ws_info(

recorder_info = {
"backlog": backlog,
"db_in_default_location": db_in_default_location,
"max_backlog": max_backlog,
"migration_in_progress": migration_in_progress,
"migration_is_live": migration_is_live,
Expand Down
40 changes: 40 additions & 0 deletions tests/components/recorder/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,7 @@ async def test_recorder_info(
assert response["success"]
assert response["result"] == {
"backlog": 0,
"db_in_default_location": False, # We never use the default URL in tests
"max_backlog": 65000,
"migration_in_progress": False,
"migration_is_live": False,
Expand All @@ -2570,6 +2571,44 @@ async def test_recorder_info(
}


@pytest.mark.parametrize(
("db_url", "db_in_default_location"),
[
("sqlite:///{config_dir}/home-assistant_v2.db", True),
("sqlite:///{config_dir}/custom.db", False),
("mysql://root:[email protected]:3316/homeassistant-test", False),
],
)
async def test_recorder_info_default_url(
recorder_mock: Recorder,
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
db_url: str,
db_in_default_location: bool,
) -> None:
"""Test getting recorder status."""
client = await hass_ws_client()

# Ensure there are no queued events
await async_wait_recording_done(hass)

with patch.object(
recorder_mock, "db_url", db_url.format(config_dir=hass.config.config_dir)
):
await client.send_json_auto_id({"type": "recorder/info"})
response = await client.receive_json()
assert response["success"]
assert response["result"] == {
"backlog": 0,
"db_in_default_location": db_in_default_location,
"max_backlog": 65000,
"migration_in_progress": False,
"migration_is_live": False,
"recording": True,
"thread_running": True,
}


async def test_recorder_info_no_recorder(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None:
Expand Down Expand Up @@ -2624,6 +2663,7 @@ async def test_recorder_info_wait_database_connect(
assert response["success"]
assert response["result"] == {
"backlog": ANY,
"db_in_default_location": False,
"max_backlog": 65000,
"migration_in_progress": False,
"migration_is_live": False,
Expand Down