Use HTTPStatus instead of HTTP_ consts and magic values in comp.../[de]* (#57990)

This commit is contained in:
Ville Skyttä 2021-10-22 17:28:56 +03:00 committed by GitHub
parent c84fee7c6e
commit 8bc1509afa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 119 additions and 117 deletions

View file

@ -1,6 +1,7 @@
"""The tests for the emulated Hue component."""
import asyncio
from datetime import timedelta
from http import HTTPStatus
from ipaddress import ip_address
import json
from unittest.mock import patch
@ -43,9 +44,6 @@ from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
CONTENT_TYPE_JSON,
HTTP_NOT_FOUND,
HTTP_OK,
HTTP_UNAUTHORIZED,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
@ -263,7 +261,7 @@ async def test_discover_lights(hue_client):
"""Test the discovery of lights."""
result = await hue_client.get("/api/username/lights")
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
result_json = await result.json()
@ -296,7 +294,7 @@ async def test_discover_lights(hue_client):
async def test_light_without_brightness_supported(hass_hue, hue_client):
"""Test that light without brightness is supported."""
light_without_brightness_json = await perform_get_light_state(
hue_client, "light.no_brightness", HTTP_OK
hue_client, "light.no_brightness", HTTPStatus.OK
)
assert light_without_brightness_json["state"][HUE_API_STATE_ON] is True
@ -329,7 +327,7 @@ async def test_lights_all_dimmable(hass, hass_client_no_auth):
HueOneLightStateView(config).register(web_app, web_app.router)
client = await hass_client_no_auth()
light_without_brightness_json = await perform_get_light_state(
client, "light.no_brightness", HTTP_OK
client, "light.no_brightness", HTTPStatus.OK
)
assert light_without_brightness_json["state"][HUE_API_STATE_ON] is True
assert light_without_brightness_json["type"] == "Dimmable light"
@ -360,7 +358,7 @@ async def test_light_without_brightness_can_be_turned_off(hass_hue, hue_client):
)
no_brightness_result_json = await no_brightness_result.json()
assert no_brightness_result.status == HTTP_OK
assert no_brightness_result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in no_brightness_result.headers["content-type"]
assert len(no_brightness_result_json) == 1
@ -402,7 +400,7 @@ async def test_light_without_brightness_can_be_turned_on(hass_hue, hue_client):
no_brightness_result_json = await no_brightness_result.json()
assert no_brightness_result.status == HTTP_OK
assert no_brightness_result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in no_brightness_result.headers["content-type"]
assert len(no_brightness_result_json) == 1
@ -430,7 +428,7 @@ async def test_reachable_for_state(hass_hue, hue_client, state, is_reachable):
hass_hue.states.async_set(entity_id, state)
state_json = await perform_get_light_state(hue_client, entity_id, HTTP_OK)
state_json = await perform_get_light_state(hue_client, entity_id, HTTPStatus.OK)
assert state_json["state"]["reachable"] == is_reachable, state_json
@ -439,7 +437,7 @@ async def test_discover_full_state(hue_client):
"""Test the discovery of full state."""
result = await hue_client.get(f"/api/{HUE_API_USERNAME}")
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
result_json = await result.json()
@ -489,7 +487,7 @@ async def test_discover_config(hue_client):
"""Test the discovery of configuration."""
result = await hue_client.get(f"/api/{HUE_API_USERNAME}/config")
assert result.status == 200
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
config_json = await result.json()
@ -526,7 +524,7 @@ async def test_discover_config(hue_client):
# Test without username
result = await hue_client.get("/api/config")
assert result.status == 200
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
config_json = await result.json()
@ -535,7 +533,7 @@ async def test_discover_config(hue_client):
# Test with wrong username username
result = await hue_client.get("/api/wronguser/config")
assert result.status == 200
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
config_json = await result.json()
@ -557,7 +555,7 @@ async def test_get_light_state(hass_hue, hue_client):
)
office_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
assert office_json["state"][HUE_API_STATE_ON] is True
@ -568,7 +566,7 @@ async def test_get_light_state(hass_hue, hue_client):
# Check all lights view
result = await hue_client.get("/api/username/lights")
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
result_json = await result.json()
@ -590,7 +588,7 @@ async def test_get_light_state(hass_hue, hue_client):
)
office_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
assert office_json["state"][HUE_API_STATE_ON] is False
@ -599,10 +597,14 @@ async def test_get_light_state(hass_hue, hue_client):
assert office_json["state"][HUE_API_STATE_SAT] == 0
# Make sure bedroom light isn't accessible
await perform_get_light_state(hue_client, "light.bed_light", HTTP_UNAUTHORIZED)
await perform_get_light_state(
hue_client, "light.bed_light", HTTPStatus.UNAUTHORIZED
)
# Make sure kitchen light isn't accessible
await perform_get_light_state(hue_client, "light.kitchen_lights", HTTP_UNAUTHORIZED)
await perform_get_light_state(
hue_client, "light.kitchen_lights", HTTPStatus.UNAUTHORIZED
)
async def test_put_light_state(hass, hass_hue, hue_client):
@ -653,7 +655,7 @@ async def test_put_light_state(hass, hass_hue, hue_client):
# go through api to get the state back
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
assert ceiling_json["state"][HUE_API_STATE_BRI] == 123
assert ceiling_json["state"][HUE_API_STATE_HUE] == 4369
@ -672,7 +674,7 @@ async def test_put_light_state(hass, hass_hue, hue_client):
# go through api to get the state back
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
assert ceiling_json["state"][HUE_API_STATE_BRI] == 254
assert ceiling_json["state"][HUE_API_STATE_HUE] == 4369
@ -690,7 +692,7 @@ async def test_put_light_state(hass, hass_hue, hue_client):
# go through api to get the state back
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
assert ceiling_json["state"][HUE_API_STATE_BRI] == 100
assert hass.states.get("light.ceiling_lights").attributes[light.ATTR_XY_COLOR] == (
@ -704,7 +706,7 @@ async def test_put_light_state(hass, hass_hue, hue_client):
ceiling_result_json = await ceiling_result.json()
assert ceiling_result.status == HTTP_OK
assert ceiling_result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in ceiling_result.headers["content-type"]
assert len(ceiling_result_json) == 1
@ -713,7 +715,7 @@ async def test_put_light_state(hass, hass_hue, hue_client):
ceiling_lights = hass_hue.states.get("light.ceiling_lights")
assert ceiling_lights.state == STATE_OFF
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
# Removed assert HUE_API_STATE_BRI == 0 as Hue API states bri must be 1..254
assert ceiling_json["state"][HUE_API_STATE_HUE] == 0
@ -723,13 +725,13 @@ async def test_put_light_state(hass, hass_hue, hue_client):
bedroom_result = await perform_put_light_state(
hass_hue, hue_client, "light.bed_light", True
)
assert bedroom_result.status == HTTP_UNAUTHORIZED
assert bedroom_result.status == HTTPStatus.UNAUTHORIZED
# Make sure we can't change the kitchen light state
kitchen_result = await perform_put_light_state(
hass_hue, hue_client, "light.kitchen_lights", True
)
assert kitchen_result.status == HTTP_UNAUTHORIZED
assert kitchen_result.status == HTTPStatus.UNAUTHORIZED
# Turn the ceiling lights on first and color temp.
await hass_hue.services.async_call(
@ -794,7 +796,7 @@ async def test_put_light_state_script(hass, hass_hue, hue_client):
script_result_json = await script_result.json()
assert script_result.status == HTTP_OK
assert script_result.status == HTTPStatus.OK
assert len(script_result_json) == 2
kitchen_light = hass_hue.states.get("light.kitchen_lights")
@ -817,7 +819,7 @@ async def test_put_light_state_climate_set_temperature(hass_hue, hue_client):
hvac_result_json = await hvac_result.json()
assert hvac_result.status == HTTP_OK
assert hvac_result.status == HTTPStatus.OK
assert len(hvac_result_json) == 2
hvac = hass_hue.states.get("climate.hvac")
@ -828,7 +830,7 @@ async def test_put_light_state_climate_set_temperature(hass_hue, hue_client):
ecobee_result = await perform_put_light_state(
hass_hue, hue_client, "climate.ecobee", True
)
assert ecobee_result.status == HTTP_UNAUTHORIZED
assert ecobee_result.status == HTTPStatus.UNAUTHORIZED
async def test_put_light_state_humidifier_set_humidity(hass_hue, hue_client):
@ -850,7 +852,7 @@ async def test_put_light_state_humidifier_set_humidity(hass_hue, hue_client):
humidifier_result_json = await humidifier_result.json()
assert humidifier_result.status == HTTP_OK
assert humidifier_result.status == HTTPStatus.OK
assert len(humidifier_result_json) == 2
hvac = hass_hue.states.get("humidifier.humidifier")
@ -861,7 +863,7 @@ async def test_put_light_state_humidifier_set_humidity(hass_hue, hue_client):
hygrostat_result = await perform_put_light_state(
hass_hue, hue_client, "humidifier.hygrostat", True
)
assert hygrostat_result.status == HTTP_UNAUTHORIZED
assert hygrostat_result.status == HTTPStatus.UNAUTHORIZED
async def test_put_light_state_media_player(hass_hue, hue_client):
@ -884,7 +886,7 @@ async def test_put_light_state_media_player(hass_hue, hue_client):
mp_result_json = await mp_result.json()
assert mp_result.status == HTTP_OK
assert mp_result.status == HTTPStatus.OK
assert len(mp_result_json) == 2
walkman = hass_hue.states.get("media_player.walkman")
@ -917,7 +919,7 @@ async def test_open_cover_without_position(hass_hue, hue_client):
# Go through the API to turn it on
cover_result = await perform_put_light_state(hass_hue, hue_client, cover_id, True)
assert cover_result.status == HTTP_OK
assert cover_result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in cover_result.headers["content-type"]
for _ in range(11):
@ -937,7 +939,7 @@ async def test_open_cover_without_position(hass_hue, hue_client):
# Go through the API to turn it off
cover_result = await perform_put_light_state(hass_hue, hue_client, cover_id, False)
assert cover_result.status == HTTP_OK
assert cover_result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in cover_result.headers["content-type"]
for _ in range(11):
@ -986,7 +988,7 @@ async def test_set_position_cover(hass_hue, hue_client):
hass_hue, hue_client, cover_id, False, brightness
)
assert cover_result.status == HTTP_OK
assert cover_result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in cover_result.headers["content-type"]
cover_result_json = await cover_result.json()
@ -1026,7 +1028,7 @@ async def test_put_light_state_fan(hass_hue, hue_client):
fan_result_json = await fan_result.json()
assert fan_result.status == HTTP_OK
assert fan_result.status == HTTPStatus.OK
assert len(fan_result_json) == 2
living_room_fan = hass_hue.states.get("fan.living_room_fan")
@ -1056,7 +1058,7 @@ async def test_put_light_state_fan(hass_hue, hue_client):
with patch.object(hue_api, "STATE_CACHED_TIMEOUT", 0.000001):
await asyncio.sleep(0.000001)
fan_json = await perform_get_light_state(
hue_client, "fan.living_room_fan", HTTP_OK
hue_client, "fan.living_room_fan", HTTPStatus.OK
)
assert round(fan_json["state"][HUE_API_STATE_BRI] * 100 / 254) == 33
@ -1074,7 +1076,7 @@ async def test_put_light_state_fan(hass_hue, hue_client):
with patch.object(hue_api, "STATE_CACHED_TIMEOUT", 0.000001):
await asyncio.sleep(0.000001)
fan_json = await perform_get_light_state(
hue_client, "fan.living_room_fan", HTTP_OK
hue_client, "fan.living_room_fan", HTTPStatus.OK
)
assert (
round(fan_json["state"][HUE_API_STATE_BRI] * 100 / 254) == 67
@ -1094,7 +1096,7 @@ async def test_put_light_state_fan(hass_hue, hue_client):
with patch.object(hue_api, "STATE_CACHED_TIMEOUT", 0.000001):
await asyncio.sleep(0.000001)
fan_json = await perform_get_light_state(
hue_client, "fan.living_room_fan", HTTP_OK
hue_client, "fan.living_room_fan", HTTPStatus.OK
)
assert round(fan_json["state"][HUE_API_STATE_BRI] * 100 / 254) == 100
@ -1116,18 +1118,18 @@ async def test_put_with_form_urlencoded_content_type(hass_hue, hue_client):
data=data,
)
assert result.status == 400
assert result.status == HTTPStatus.BAD_REQUEST
async def test_entity_not_found(hue_client):
"""Test for entity which are not found."""
result = await hue_client.get("/api/username/lights/98")
assert result.status == HTTP_NOT_FOUND
assert result.status == HTTPStatus.NOT_FOUND
result = await hue_client.put("/api/username/lights/98/state")
assert result.status == HTTP_NOT_FOUND
assert result.status == HTTPStatus.NOT_FOUND
async def test_allowed_methods(hue_client):
@ -1136,17 +1138,17 @@ async def test_allowed_methods(hue_client):
"/api/username/lights/ENTITY_NUMBERS_BY_ID[light.ceiling_lights]/state"
)
assert result.status == 405
assert result.status == HTTPStatus.METHOD_NOT_ALLOWED
result = await hue_client.put(
"/api/username/lights/ENTITY_NUMBERS_BY_ID[light.ceiling_lights]"
)
assert result.status == 405
assert result.status == HTTPStatus.METHOD_NOT_ALLOWED
result = await hue_client.put("/api/username/lights")
assert result.status == 405
assert result.status == HTTPStatus.METHOD_NOT_ALLOWED
async def test_proper_put_state_request(hue_client):
@ -1159,7 +1161,7 @@ async def test_proper_put_state_request(hue_client):
data=json.dumps({HUE_API_STATE_ON: 1234}),
)
assert result.status == 400
assert result.status == HTTPStatus.BAD_REQUEST
# Test proper brightness value parsing
result = await hue_client.put(
@ -1169,7 +1171,7 @@ async def test_proper_put_state_request(hue_client):
data=json.dumps({HUE_API_STATE_ON: True, HUE_API_STATE_BRI: "Hello world!"}),
)
assert result.status == 400
assert result.status == HTTPStatus.BAD_REQUEST
async def test_get_empty_groups_state(hue_client):
@ -1177,7 +1179,7 @@ async def test_get_empty_groups_state(hue_client):
# Test proper on value parsing
result = await hue_client.get("/api/username/groups")
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
result_json = await result.json()
@ -1205,7 +1207,7 @@ async def perform_put_test_on_ceiling_lights(
hass_hue, hue_client, "light.ceiling_lights", True, 56, content_type
)
assert office_result.status == HTTP_OK
assert office_result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in office_result.headers["content-type"]
office_result_json = await office_result.json()
@ -1224,7 +1226,7 @@ async def perform_get_light_state_by_number(client, entity_number, expected_stat
assert result.status == expected_status
if expected_status == HTTP_OK:
if expected_status == HTTPStatus.OK:
assert CONTENT_TYPE_JSON in result.headers["content-type"]
return await result.json()
@ -1305,15 +1307,15 @@ async def test_external_ip_blocked(hue_client):
):
for getUrl in getUrls:
result = await hue_client.get(getUrl)
assert result.status == HTTP_UNAUTHORIZED
assert result.status == HTTPStatus.UNAUTHORIZED
for postUrl in postUrls:
result = await hue_client.post(postUrl)
assert result.status == HTTP_UNAUTHORIZED
assert result.status == HTTPStatus.UNAUTHORIZED
for putUrl in putUrls:
result = await hue_client.put(putUrl)
assert result.status == HTTP_UNAUTHORIZED
assert result.status == HTTPStatus.UNAUTHORIZED
async def test_unauthorized_user_blocked(hue_client):
@ -1323,7 +1325,7 @@ async def test_unauthorized_user_blocked(hue_client):
]
for getUrl in getUrls:
result = await hue_client.get(getUrl)
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
result_json = await result.json()
assert result_json[0]["error"]["description"] == "unauthorized user"
@ -1371,7 +1373,7 @@ async def test_put_then_get_cached_properly(hass, hass_hue, hue_client):
# go through api to get the state back, the value returned should match those set in the last PUT request.
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
assert ceiling_json["state"][HUE_API_STATE_HUE] == 4369
@ -1388,7 +1390,7 @@ async def test_put_then_get_cached_properly(hass, hass_hue, hue_client):
# go through api to get the state back
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
# Now it should be the real value as the state of the entity has changed to OFF.
@ -1430,7 +1432,7 @@ async def test_put_then_get_cached_properly(hass, hass_hue, hue_client):
# go through api to get the state back, the value returned should match those set in the last PUT request.
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
# With no wait, we must be reading what we set via the PUT call.
@ -1443,7 +1445,7 @@ async def test_put_then_get_cached_properly(hass, hass_hue, hue_client):
# go through api to get the state back, the value returned should now match the actual values.
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
# Once we're after the cached duration, we should see the real value.
@ -1496,7 +1498,7 @@ async def test_put_than_get_when_service_call_fails(hass, hass_hue, hue_client):
# go through api to get the state back, the value returned should NOT match those set in the last PUT request
# as the waiting to check the state change timed out
ceiling_json = await perform_get_light_state(
hue_client, "light.ceiling_lights", HTTP_OK
hue_client, "light.ceiling_lights", HTTPStatus.OK
)
assert ceiling_json["state"][HUE_API_STATE_ON] is False
@ -1506,7 +1508,7 @@ async def test_get_invalid_entity(hass, hass_hue, hue_client):
"""Test the setting of light states and an immediate readback reads the same values."""
# Check that we get an error with an invalid entity number.
await perform_get_light_state_by_number(hue_client, 999, HTTP_NOT_FOUND)
await perform_get_light_state_by_number(hue_client, 999, HTTPStatus.NOT_FOUND)
async def test_put_light_state_scene(hass, hass_hue, hue_client):
@ -1524,7 +1526,7 @@ async def test_put_light_state_scene(hass, hass_hue, hue_client):
)
scene_result_json = await scene_result.json()
assert scene_result.status == HTTP_OK
assert scene_result.status == HTTPStatus.OK
assert len(scene_result_json) == 1
assert hass_hue.states.get("light.kitchen_lights").state == STATE_ON

View file

@ -1,4 +1,5 @@
"""The tests for the emulated Hue component."""
from http import HTTPStatus
import json
import unittest
@ -9,7 +10,7 @@ import pytest
from homeassistant import setup
from homeassistant.components import emulated_hue
from homeassistant.components.emulated_hue import upnp
from homeassistant.const import CONTENT_TYPE_JSON, HTTP_OK
from homeassistant.const import CONTENT_TYPE_JSON
from tests.common import get_test_instance_port
@ -149,7 +150,7 @@ async def test_description_xml(hass, hue_client):
client = await hue_client()
result = await client.get("/description.xml", timeout=5)
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
assert "text/xml" in result.headers["content-type"]
try:
@ -168,7 +169,7 @@ async def test_create_username(hass, hue_client):
result = await client.post("/api", data=json.dumps(request_json), timeout=5)
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
resp_json = await result.json()
@ -188,7 +189,7 @@ async def test_unauthorized_view(hass, hue_client):
"/api/unauthorized", data=json.dumps(request_json), timeout=5
)
assert result.status == HTTP_OK
assert result.status == HTTPStatus.OK
assert CONTENT_TYPE_JSON in result.headers["content-type"]
resp_json = await result.json()
@ -212,4 +213,4 @@ async def test_valid_username_request(hass, hue_client):
result = await client.post("/api", data=json.dumps(request_json), timeout=5)
assert result.status == 400
assert result.status == HTTPStatus.BAD_REQUEST