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

* Fix validation of get new state

* Add unittest to check posting a state with zero value
This commit is contained in:
Eugenio Panadero 2017-07-03 23:35:57 +02:00 committed by Pascal Vizeli
parent 1e655eea74
commit 22681fbe08
2 changed files with 18 additions and 1 deletions

View File

@ -213,7 +213,7 @@ class APIEntityStateView(HomeAssistantView):
new_state = data.get('state')
if not new_state:
if new_state is None:
return self.json_message('No state specified', HTTP_BAD_REQUEST)
attributes = data.get('attributes')

View File

@ -96,6 +96,23 @@ def test_api_state_change_with_bad_data(hass, mock_api_client):
assert resp.status == 400
# pylint: disable=invalid-name
@asyncio.coroutine
def test_api_state_change_to_zero_value(hass, mock_api_client):
"""Test if changing a state to a zero value is possible."""
resp = yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test_entity.with_zero_state"),
json={'state': 0})
assert resp.status == 201
resp = yield from mock_api_client.post(
const.URL_API_STATES_ENTITY.format("test_entity.with_zero_state"),
json={'state': 0.})
assert resp.status == 200
# pylint: disable=invalid-name
@asyncio.coroutine
def test_api_state_change_push(hass, mock_api_client):