Repository: robmarkcole/HASS-data-detective

Issue:
db_url_from_hass_config to error out when no recorder section found and DB file doesn't exist
If we can't find a recorder section in `config.db_url_from_hass_config`, we return the SQLite DB. We should only return SQLLite if the file actually exists in the config dir. Else we should error out.

Discussion:

Patch:
diff --git a/detective/config.py b/detective/config.py
index 1548e2b..964fabe 100644
--- a/detective/config.py
+++ b/detective/config.py
@@ -83,11 +83,18 @@ def load_hass_config(path):
 def db_url_from_hass_config(path):
     """Find the recorder database url from a HASS config dir."""
     config = load_hass_config(path)
-    default = "sqlite:///{}".format(os.path.join(path, "home-assistant_v2.db"))
+    default_path = os.path.join(path, "home-assistant_v2.db")
+    default_url = "sqlite:///{}".format(default_path)
 
     recorder = config.get("recorder")
 
-    if not recorder:
-        return default
+    if recorder:
+        db_url = recorder.get("db_url")
+        if db_url is not None:
+            return db_url
 
-    return recorder.get("db_url", default)
+    if not os.path.isfile(default_path):
+        raise ValueError(
+            "Unable to determine DB url from hass config at {}".format(path))
+
+    return default_url


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
index 2c64b64..5dd6beb 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -53,7 +53,18 @@ other_secret: test-other-secret
 
 def test_db_url_from_hass_config():
     """Test extracting recorder url from config."""
-    with patch('detective.config.load_hass_config', return_value={}):
+    with patch(
+        'detective.config.load_hass_config', return_value={}
+    ), patch(
+        'os.path.isfile', return_value=False
+    ), pytest.raises(ValueError):
+        config.db_url_from_hass_config('mock-path')
+
+    with patch(
+        'detective.config.load_hass_config', return_value={}
+    ), patch(
+        'os.path.isfile', return_value=True
+    ):
         assert config.db_url_from_hass_config('mock-path') == \
             'sqlite:///mock-path/home-assistant_v2.db'