diff --git a/homeassistant/components/sql/sensor.py b/homeassistant/components/sql/sensor.py index c4e6db4c623..063627f9f43 100644 --- a/homeassistant/components/sql/sensor.py +++ b/homeassistant/components/sql/sensor.py @@ -184,10 +184,14 @@ async def async_setup_sensor( async_add_entities: AddEntitiesCallback, ) -> None: """Set up the SQL sensor.""" - instance = get_instance(hass) + try: + instance = get_instance(hass) + except KeyError: # No recorder loaded + uses_recorder_db = False + else: + uses_recorder_db = db_url == instance.db_url sessmaker: scoped_session | None sql_data = _async_get_or_init_domain_data(hass) - uses_recorder_db = db_url == instance.db_url use_database_executor = False if uses_recorder_db and instance.dialect_name == SupportedDialect.SQLITE: use_database_executor = True diff --git a/tests/components/sql/test_sensor.py b/tests/components/sql/test_sensor.py index 6c2686cb6fe..dd0bd06c008 100644 --- a/tests/components/sql/test_sensor.py +++ b/tests/components/sql/test_sensor.py @@ -635,3 +635,13 @@ async def test_query_recover_from_rollback( state = hass.states.get("sensor.select_value_sql_query") assert state.state == "5" assert state.attributes.get("value") == 5 + + +async def test_setup_without_recorder(hass: HomeAssistant) -> None: + """Test the SQL sensor without recorder.""" + + assert await async_setup_component(hass, DOMAIN, YAML_CONFIG) + await hass.async_block_till_done() + + state = hass.states.get("sensor.get_value") + assert state.state == "5"