diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index c60b95d1cae..907ae8ba51b 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -153,8 +153,8 @@ class Recorder(threading.Thread): def run(self): """Start processing events to save.""" - from sqlalchemy.exc import SQLAlchemyError from .models import States, Events + from homeassistant.components import persistent_notification while True: try: @@ -163,10 +163,15 @@ class Recorder(threading.Thread): self._setup_run() self.hass.loop.call_soon_threadsafe(self.async_db_ready.set) break - except SQLAlchemyError as err: + except Exception as err: # pylint: disable=broad-except _LOGGER.error("Error during connection setup: %s (retrying " "in %s seconds)", err, CONNECT_RETRY_WAIT) time.sleep(CONNECT_RETRY_WAIT) + retry = locals().setdefault('retry', 10) - 1 + if retry == 0: + msg = "The recorder could not start, please check the log" + persistent_notification.create(self.hass, msg, 'Recorder') + return purge_task = object() shutdown_task = object() diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 0724313dcea..c43caefb67c 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -1,14 +1,17 @@ """The tests for the Recorder component.""" # pylint: disable=protected-access import unittest +from unittest.mock import patch import pytest from homeassistant.core import callback from homeassistant.const import MATCH_ALL +from homeassistant.components.recorder import Recorder from homeassistant.components.recorder.const import DATA_INSTANCE from homeassistant.components.recorder.util import session_scope from homeassistant.components.recorder.models import States, Events + from tests.common import get_test_home_assistant, init_recorder_component @@ -163,3 +166,18 @@ def test_saving_state_include_domain_exclude_entity(hass_recorder): assert len(states) == 1 assert hass.states.get('test.ok') == states[0] assert hass.states.get('test.ok').state == 'state2' + + +def test_recorder_setup_failure(): + """Test some exceptions.""" + hass = get_test_home_assistant() + + with patch.object(Recorder, '_setup_connection') as setup, \ + patch('homeassistant.components.recorder.time.sleep'): + setup.side_effect = ImportError("driver not found") + rec = Recorder( + hass, purge_days=0, uri='sqlite://', include={}, exclude={}) + rec.start() + rec.join() + + hass.stop()