Support MSSQL in SQL Sensor (#42778)

* add mssql support

* add tests and odbc dependency

* fix requirements

* no pyodbc dependency
pull/44215/head
Diogo Gomes 2020-12-14 09:57:22 +00:00 committed by GitHub
parent 7fa26ef515
commit c3d8b1323c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -74,6 +74,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if value_template is not None: if value_template is not None:
value_template.hass = hass value_template.hass = hass
# MSSQL uses TOP and not LIMIT
if not ("LIMIT" in query_str or "SELECT TOP" in query_str):
query_str = (
query_str.replace("SELECT", "SELECT TOP 1")
if "mssql" in db_url
else query_str.replace(";", " LIMIT 1;")
)
sensor = SQLSensor( sensor = SQLSensor(
name, sessmaker, query_str, column_name, unit, value_template name, sessmaker, query_str, column_name, unit, value_template
) )
@ -88,10 +96,7 @@ class SQLSensor(Entity):
def __init__(self, name, sessmaker, query, column, unit, value_template): def __init__(self, name, sessmaker, query, column, unit, value_template):
"""Initialize the SQL sensor.""" """Initialize the SQL sensor."""
self._name = name self._name = name
if "LIMIT" in query: self._query = query
self._query = query
else:
self._query = query.replace(";", " LIMIT 1;")
self._unit_of_measurement = unit self._unit_of_measurement = unit
self._template = value_template self._template = value_template
self._column_name = column self._column_name = column