diff --git a/homeassistant/components/sql/sensor.py b/homeassistant/components/sql/sensor.py index 1c8514d0d26..0a240469f83 100644 --- a/homeassistant/components/sql/sensor.py +++ b/homeassistant/components/sql/sensor.py @@ -172,7 +172,7 @@ class SQLSensor(SensorEntity): else: self._attr_native_value = data - if not data: + if data is None: _LOGGER.warning("%s returned no results", self._query) sess.close() diff --git a/tests/components/sql/test_sensor.py b/tests/components/sql/test_sensor.py index 0e543f98a21..05f49d553e9 100644 --- a/tests/components/sql/test_sensor.py +++ b/tests/components/sql/test_sensor.py @@ -115,7 +115,9 @@ async def test_query_limit(hass: HomeAssistant) -> None: assert state.attributes["value"] == 5 -async def test_query_no_value(hass: HomeAssistant) -> None: +async def test_query_no_value( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: """Test the SQL sensor with a query that returns no value.""" config = { "sensor": { @@ -137,6 +139,9 @@ async def test_query_no_value(hass: HomeAssistant) -> None: state = hass.states.get("sensor.count_tables") assert state.state == STATE_UNKNOWN + text = "SELECT 5 as value where 1=2 returned no results" + assert text in caplog.text + async def test_invalid_query(hass: HomeAssistant) -> None: """Test the SQL sensor for invalid queries."""