Fix sql false warning (#67614)

This commit is contained in:
G Johansson 2022-03-04 19:20:10 +01:00 committed by GitHub
parent 5965b015dd
commit 3f9a6bbaa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -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()

View File

@ -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."""