Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for queries with no results (fix for #12856) #12888

Merged
merged 6 commits into from Mar 6, 2018

Conversation

dgomes
Copy link
Contributor

@dgomes dgomes commented Mar 3, 2018

Description:

Tests if there are 0 results and clears the state and attributes.

Related issue (if applicable): fixes #12856

Checklist:

  • The code change is tested and works locally.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@@ -129,15 +129,17 @@ def update(self):
finally:
sess.close()

if not result.returns_rows:
_LOGGER.error("%s returned no results", self._query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should be a warning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely right!

@dgomes dgomes changed the title Addresses issue #12856 [WIP] Addresses issue #12856 Mar 4, 2018
@@ -35,3 +36,22 @@ def test_query(self):

state = self.hass.states.get('sensor.count_tables')
self.assertEqual(state.state, '0')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

@dgomes dgomes changed the title [WIP] Addresses issue #12856 Addresses issue #12856 Mar 4, 2018
for res in result:
_LOGGER.debug(res.items())
_LOGGER.debug("result = %s", res.items())
data = res[self._column_name]
self._attributes = {k: str(v) for k, v in res.items()}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we convert this to strings? What about numbers?

Copy link
Contributor Author

@dgomes dgomes Mar 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res.items() will return various columns, so it's not a scalar to convert to a number.

This call will resort to __str___ of sqlalchemy to get something human readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better for number types to remain numbers. Otherwise you can't do checks with it in templates without converting it to a number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I understand you initial comment.

Well casting to str() assured me nothing strange would show up in attributes.

@@ -129,15 +129,17 @@ def update(self):
finally:
sess.close()

if not result.returns_rows or result.rowcount == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returns_rows -> I would expect this to be validated in the PLATFORM_SCHEMA by checking if the query starts with SELECT ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is me being extra careful not to raise any exception :)

http://docs.sqlalchemy.org/en/latest/core/connections.html?highlight=returns_rows#sqlalchemy.engine.ResultProxy.returns_rows

How would I validade the query string using voluptuous ?

  • Should I create a new "ensure_sql_query" in home-assistant/homeassistant/helpers/config_validation.py ?

Copy link
Member

@balloob balloob Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add things to config validation if only used once. You can add a one off validator to this file and use that.

def validate_sql_select(value):
    if not value.lstrip().lower().startswith('select'):
        raise vol.Invalid('Only select queries allowed')
    return value

vol.Schema({
  'query': vol.All(cv.string, validate_sql_select)
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now we can remove the returns_rows check ?

Copy link
Contributor Author

@dgomes dgomes Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats is wrong with checking returns_rows ?

@@ -35,3 +36,22 @@ def test_query(self):

state = self.hass.states.get('sensor.count_tables')
self.assertEqual(state.state, '0')

def test_no_results_query(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test the opposite. This case is already covered by all the other tests.

Copy link
Contributor Author

@dgomes dgomes Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the test I missed in the first place (which led to the issue)... don't understand you 😕

@dgomes dgomes changed the title Addresses issue #12856 Support for queries with no results (fix for #12856) Mar 6, 2018
@balloob balloob merged commit 5063464 into home-assistant:dev Mar 6, 2018
@balloob balloob mentioned this pull request Mar 9, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
@dgomes dgomes deleted the fix-sql-sensor-no-results branch August 17, 2018 22:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed integration: sql small-pr PRs with less than 30 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQL Sensor Component UnboundLocalError when no data returned.
5 participants