Repository: robmarkcole/HASS-data-detective

Issue:
Add db type as attribute 
As pointed out by @balloob 'Most DBAPIs have built in support for the datetime module, with the noted exception of SQLite. In the case of SQLite, date and time types are stored as strings which are then converted back to datetime objects when rows are returned.' Detective should have the db type as an attribute, allowing for db type specific behaviour if required.

Comments:

Patch:
diff --git a/detective/config.py b/detective/config.py
index 1548e2b..346bff4 100644
--- a/detective/config.py
+++ b/detective/config.py
@@ -51,6 +51,7 @@ def _secret_yaml(loader, node):
 
 def _stub_dict(constructor, node):
     """Stub a constructor with a dictionary."""
+    print("Unsupported YAML found: {} {}".format(node.tag, node.value))
     return {}
 
 
@@ -83,11 +84,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
diff --git a/detective/core.py b/detective/core.py
index 9e2b5be..e3d228c 100644
--- a/detective/core.py
+++ b/detective/core.py
@@ -1,12 +1,14 @@
 """
 Classes and functions for parsing home-assistant data.
 """
+from urllib.parse import urlparse
+from typing import List
 
-from . import config, functions
 import matplotlib.pyplot as plt
 import pandas as pd
 from sqlalchemy import create_engine, text
-from typing import List
+
+from . import config, functions
 
 
 def db_from_hass_config(path=None, **kwargs):
@@ -18,6 +20,10 @@ def db_from_hass_config(path=None, **kwargs):
     return HassDatabase(url, **kwargs)
 
 
+def get_db_type(url):
+    return urlparse(url).scheme.split('+')[0]
+
+
 class HassDatabase:
     """
     Initializing the parser fetches all of the data from the database and
@@ -31,7 +37,8 @@ class HassDatabase:
         url : str
             The URL to the database.
         """
-        self._url = url
+        self.url = url
+
         self._master_df = None
         self._domains = None
         self._entities = None
@@ -41,9 +48,19 @@ class HassDatabase:
             if fetch_entities:
                 self.fetch_entities()
         except Exception as exc:
+            if isinstance(exc, ImportError):
+                raise RuntimeError(
+                    "The right dependency to connect to your database is "
+                    "missing. Please make sure that it is installed."
+                )
+
             print(exc)
             raise
 
+
+        self.db_type = get_db_type(url)
+
+
     def perform_query(self, query):
         """Perform a query, where query is a string."""
         try:


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'
 
diff --git a/tests/test_core.py b/tests/test_core.py
index 2139787..65002af 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -1,1 +1,6 @@
-# To do
+from detective.core import get_db_type
+
+
+def test_get_db_type():
+    assert get_db_type('mysql://localhost') == 'mysql'
+    assert get_db_type('mysql+pymysql://localhost') == 'mysql'