Skip to content

API POST has new state validation (fix for state = 0)#8324

Merged
pvizeli merged 2 commits into
home-assistant:devfrom
azogue:api-changes
Jul 3, 2017
Merged

API POST has new state validation (fix for state = 0)#8324
pvizeli merged 2 commits into
home-assistant:devfrom
azogue:api-changes

Conversation

@azogue
Copy link
Copy Markdown
Member

@azogue azogue commented Jul 3, 2017

Description:

When posting a new state of an entity through the API, if the state value is 0, the validation acts as if a state had not been sent, so it returns a 404 error code and does not update the entity state.

Problem is this validation:

new_state = data.get('state')
if not new_state:
    return self.json_message('No state specified', HTTP_BAD_REQUEST)

If new_state is a float or an integer, the 0 / 0.0 value returns the error! This has to be a null comparison (if new_state is None).

I was going crazy until I discovered the error... I have a custom sensor in an AppDaemon app which measures the difference of temperature between the interior and the exterior of the house, and it was failing mysteriously when ∆T=0.

Example entry to check the error:

curl -X POST -H "x-ha-access: [HA_PASSWD]" -H "Content-Type: application/json" -d '{"state": 0, "attributes": {"friendly_name": "Test Sensor "}}' http://[HA_IP]:8123/api/states/sensor.test_sensor

Checklist:

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.

@mention-bot
Copy link
Copy Markdown

@azogue, thanks for your PR! By analyzing the history of the files in this pull request, we identified @balloob, @JshWright and @fabaff to be potential reviewers.

@pvizeli pvizeli added this to the 0.48.1 milestone Jul 3, 2017
@pvizeli pvizeli merged commit 22681fb into home-assistant:dev Jul 3, 2017
balloob pushed a commit that referenced this pull request Jul 5, 2017
* Fix validation of get new state

* Add unittest to check posting a state with zero value
@balloob
Copy link
Copy Markdown
Member

balloob commented Jul 5, 2017

Cherry picked for 0.48.1

@balloob balloob mentioned this pull request Jul 5, 2017
@balloob balloob mentioned this pull request Jul 13, 2017
@azogue azogue deleted the api-changes branch July 30, 2017 09:33
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
…#8324)

* Fix validation of get new state

* Add unittest to check posting a state with zero value
@home-assistant home-assistant locked and limited conversation to collaborators Dec 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants