mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Convert decimals from SQL results (#13059)
This commit is contained in:
parent
b98d2e2485
commit
42359b3b48
@ -4,6 +4,7 @@ Sensor from an SQL Query.
|
|||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.sql/
|
https://home-assistant.io/components/sensor.sql/
|
||||||
"""
|
"""
|
||||||
|
import decimal
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -131,17 +132,20 @@ class SQLSensor(Entity):
|
|||||||
try:
|
try:
|
||||||
sess = self.sessionmaker()
|
sess = self.sessionmaker()
|
||||||
result = sess.execute(self._query)
|
result = sess.execute(self._query)
|
||||||
|
self._attributes = {}
|
||||||
|
|
||||||
if not result.returns_rows or result.rowcount == 0:
|
if not result.returns_rows or result.rowcount == 0:
|
||||||
_LOGGER.warning("%s returned no results", self._query)
|
_LOGGER.warning("%s returned no results", self._query)
|
||||||
self._state = None
|
self._state = None
|
||||||
self._attributes = {}
|
|
||||||
return
|
return
|
||||||
|
|
||||||
for res in result:
|
for res in result:
|
||||||
_LOGGER.debug("result = %s", res.items())
|
_LOGGER.debug("result = %s", res.items())
|
||||||
data = res[self._column_name]
|
data = res[self._column_name]
|
||||||
self._attributes = {k: v for k, v in res.items()}
|
for key, value in res.items():
|
||||||
|
if isinstance(value, decimal.Decimal):
|
||||||
|
value = float(decimal)
|
||||||
|
self._attributes[key] = value
|
||||||
except sqlalchemy.exc.SQLAlchemyError as err:
|
except sqlalchemy.exc.SQLAlchemyError as err:
|
||||||
_LOGGER.error("Error executing query %s: %s", self._query, err)
|
_LOGGER.error("Error executing query %s: %s", self._query, err)
|
||||||
return
|
return
|
||||||
|
@ -29,7 +29,7 @@ class TestSQLSensor(unittest.TestCase):
|
|||||||
'db_url': 'sqlite://',
|
'db_url': 'sqlite://',
|
||||||
'queries': [{
|
'queries': [{
|
||||||
'name': 'count_tables',
|
'name': 'count_tables',
|
||||||
'query': 'SELECT count(*) value FROM sqlite_master;',
|
'query': 'SELECT 5 as value',
|
||||||
'column': 'value',
|
'column': 'value',
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
@ -38,7 +38,8 @@ class TestSQLSensor(unittest.TestCase):
|
|||||||
assert setup_component(self.hass, 'sensor', config)
|
assert setup_component(self.hass, 'sensor', config)
|
||||||
|
|
||||||
state = self.hass.states.get('sensor.count_tables')
|
state = self.hass.states.get('sensor.count_tables')
|
||||||
self.assertEqual(state.state, '0')
|
assert state.state == '5'
|
||||||
|
assert state.attributes['value'] == 5
|
||||||
|
|
||||||
def test_invalid_query(self):
|
def test_invalid_query(self):
|
||||||
"""Test the SQL sensor for invalid queries."""
|
"""Test the SQL sensor for invalid queries."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user