From 7eb532ddd0dde768fa33711eb5a3dcc10dd98857 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 17 Oct 2020 18:20:06 +0200 Subject: [PATCH] Improve imports in deCONZ tests (#41976) * Improve imports in deconz tests * Add spacing --- tests/components/deconz/test_binary_sensor.py | 40 +++-- tests/components/deconz/test_climate.py | 92 ++++++---- tests/components/deconz/test_config_flow.py | 159 +++++++++--------- tests/components/deconz/test_cover.py | 47 +++--- tests/components/deconz/test_deconz_event.py | 1 + .../components/deconz/test_device_trigger.py | 84 +++++---- tests/components/deconz/test_fan.py | 69 ++++---- tests/components/deconz/test_gateway.py | 93 +++++----- tests/components/deconz/test_init.py | 46 ++--- tests/components/deconz/test_light.py | 129 ++++++++------ tests/components/deconz/test_lock.py | 27 +-- tests/components/deconz/test_scene.py | 15 +- tests/components/deconz/test_sensor.py | 18 +- tests/components/deconz/test_services.py | 75 +++++---- tests/components/deconz/test_switch.py | 50 +++--- 15 files changed, 547 insertions(+), 398 deletions(-) diff --git a/tests/components/deconz/test_binary_sensor.py b/tests/components/deconz/test_binary_sensor.py index f3a5cede08e..5872aee1bf1 100644 --- a/tests/components/deconz/test_binary_sensor.py +++ b/tests/components/deconz/test_binary_sensor.py @@ -1,13 +1,19 @@ """deCONZ binary sensor platform tests.""" + from copy import deepcopy -from homeassistant.components import deconz -import homeassistant.components.binary_sensor as binary_sensor from homeassistant.components.binary_sensor import ( DEVICE_CLASS_MOTION, DEVICE_CLASS_VIBRATION, + DOMAIN as BINARY_SENSOR_DOMAIN, +) +from homeassistant.components.deconz.const import ( + CONF_ALLOW_CLIP_SENSOR, + CONF_ALLOW_NEW_DEVICES, + DOMAIN as DECONZ_DOMAIN, ) from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.helpers.entity_registry import async_entries_for_config_entry from homeassistant.setup import async_setup_component @@ -58,11 +64,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, binary_sensor.DOMAIN, {"binary_sensor": {"platform": deconz.DOMAIN}} + hass, BINARY_SENSOR_DOMAIN, {"binary_sensor": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_binary_sensors(hass): @@ -80,12 +86,12 @@ async def test_binary_sensors(hass): assert len(hass.states.async_all()) == 3 presence_sensor = hass.states.get("binary_sensor.presence_sensor") - assert presence_sensor.state == "off" + assert presence_sensor.state == STATE_OFF assert presence_sensor.attributes["device_class"] == DEVICE_CLASS_MOTION assert hass.states.get("binary_sensor.temperature_sensor") is None assert hass.states.get("binary_sensor.clip_presence_sensor") is None vibration_sensor = hass.states.get("binary_sensor.vibration_sensor") - assert vibration_sensor.state == "on" + assert vibration_sensor.state == STATE_ON assert vibration_sensor.attributes["device_class"] == DEVICE_CLASS_VIBRATION state_changed_event = { @@ -98,7 +104,7 @@ async def test_binary_sensors(hass): gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() - assert hass.states.get("binary_sensor.presence_sensor").state == "on" + assert hass.states.get("binary_sensor.presence_sensor").state == STATE_ON await hass.config_entries.async_unload(config_entry.entry_id) @@ -111,20 +117,20 @@ async def test_allow_clip_sensor(hass): data["sensors"] = deepcopy(SENSORS) config_entry = await setup_deconz_integration( hass, - options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}, + options={CONF_ALLOW_CLIP_SENSOR: True}, get_state_response=data, ) assert len(hass.states.async_all()) == 4 - assert hass.states.get("binary_sensor.presence_sensor").state == "off" + assert hass.states.get("binary_sensor.presence_sensor").state == STATE_OFF assert hass.states.get("binary_sensor.temperature_sensor") is None - assert hass.states.get("binary_sensor.clip_presence_sensor").state == "off" - assert hass.states.get("binary_sensor.vibration_sensor").state == "on" + assert hass.states.get("binary_sensor.clip_presence_sensor").state == STATE_OFF + assert hass.states.get("binary_sensor.vibration_sensor").state == STATE_ON # Disallow clip sensors hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} + config_entry, options={CONF_ALLOW_CLIP_SENSOR: False} ) await hass.async_block_till_done() @@ -134,18 +140,18 @@ async def test_allow_clip_sensor(hass): # Allow clip sensors hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} + config_entry, options={CONF_ALLOW_CLIP_SENSOR: True} ) await hass.async_block_till_done() assert len(hass.states.async_all()) == 4 - assert hass.states.get("binary_sensor.clip_presence_sensor").state == "off" + assert hass.states.get("binary_sensor.clip_presence_sensor").state == STATE_OFF async def test_add_new_binary_sensor(hass): """Test that adding a new binary sensor works.""" config_entry = await setup_deconz_integration(hass) - gateway = deconz.gateway.get_gateway_from_config_entry(hass, config_entry) + gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { @@ -159,14 +165,14 @@ async def test_add_new_binary_sensor(hass): await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 - assert hass.states.get("binary_sensor.presence_sensor").state == "off" + assert hass.states.get("binary_sensor.presence_sensor").state == STATE_OFF async def test_add_new_binary_sensor_ignored(hass): """Test that adding a new binary sensor is not allowed.""" config_entry = await setup_deconz_integration( hass, - options={deconz.gateway.CONF_ALLOW_NEW_DEVICES: False}, + options={CONF_ALLOW_NEW_DEVICES: False}, ) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 diff --git a/tests/components/deconz/test_climate.py b/tests/components/deconz/test_climate.py index 7e49dd3c907..5d2e6d614a3 100644 --- a/tests/components/deconz/test_climate.py +++ b/tests/components/deconz/test_climate.py @@ -1,11 +1,29 @@ """deCONZ climate platform tests.""" + from copy import deepcopy import pytest -from homeassistant.components import deconz -import homeassistant.components.climate as climate +from homeassistant.components.climate import ( + DOMAIN as CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + SERVICE_SET_TEMPERATURE, +) +from homeassistant.components.climate.const import ( + ATTR_HVAC_MODE, + ATTR_TARGET_TEMP_HIGH, + ATTR_TARGET_TEMP_LOW, + HVAC_MODE_AUTO, + HVAC_MODE_COOL, + HVAC_MODE_HEAT, + HVAC_MODE_OFF, +) +from homeassistant.components.deconz.const import ( + CONF_ALLOW_CLIP_SENSOR, + DOMAIN as DECONZ_DOMAIN, +) from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.const import ATTR_ENTITY_ID, ATTR_TEMPERATURE, STATE_OFF from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -42,11 +60,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, climate.DOMAIN, {"climate": {"platform": deconz.DOMAIN}} + hass, CLIMATE_DOMAIN, {"climate": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_sensors(hass): @@ -63,7 +81,7 @@ async def test_climate_devices(hass): gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 2 - assert hass.states.get("climate.thermostat").state == "auto" + assert hass.states.get("climate.thermostat").state == HVAC_MODE_AUTO assert hass.states.get("sensor.thermostat") is None assert hass.states.get("sensor.thermostat_battery_level").state == "100" assert hass.states.get("climate.presence_sensor") is None @@ -81,7 +99,7 @@ async def test_climate_devices(hass): gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() - assert hass.states.get("climate.thermostat").state == "off" + assert hass.states.get("climate.thermostat").state == STATE_OFF # Event signals thermostat state on @@ -96,7 +114,7 @@ async def test_climate_devices(hass): gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() - assert hass.states.get("climate.thermostat").state == "heat" + assert hass.states.get("climate.thermostat").state == HVAC_MODE_HEAT # Event signals thermostat state off @@ -110,7 +128,7 @@ async def test_climate_devices(hass): gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() - assert hass.states.get("climate.thermostat").state == "off" + assert hass.states.get("climate.thermostat").state == STATE_OFF # Verify service calls @@ -120,9 +138,9 @@ async def test_climate_devices(hass): with patch.object(thermostat_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - climate.DOMAIN, - climate.SERVICE_SET_HVAC_MODE, - {"entity_id": "climate.thermostat", "hvac_mode": "auto"}, + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + {ATTR_ENTITY_ID: "climate.thermostat", ATTR_HVAC_MODE: HVAC_MODE_AUTO}, blocking=True, ) await hass.async_block_till_done() @@ -134,9 +152,9 @@ async def test_climate_devices(hass): with patch.object(thermostat_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - climate.DOMAIN, - climate.SERVICE_SET_HVAC_MODE, - {"entity_id": "climate.thermostat", "hvac_mode": "heat"}, + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + {ATTR_ENTITY_ID: "climate.thermostat", ATTR_HVAC_MODE: HVAC_MODE_HEAT}, blocking=True, ) await hass.async_block_till_done() @@ -148,9 +166,9 @@ async def test_climate_devices(hass): with patch.object(thermostat_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - climate.DOMAIN, - climate.SERVICE_SET_HVAC_MODE, - {"entity_id": "climate.thermostat", "hvac_mode": "off"}, + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + {ATTR_ENTITY_ID: "climate.thermostat", ATTR_HVAC_MODE: HVAC_MODE_OFF}, blocking=True, ) set_callback.assert_called_with( @@ -163,9 +181,9 @@ async def test_climate_devices(hass): thermostat_device, "_request", return_value=True ) as set_callback, pytest.raises(ValueError): await hass.services.async_call( - climate.DOMAIN, - climate.SERVICE_SET_HVAC_MODE, - {"entity_id": "climate.thermostat", "hvac_mode": "cool"}, + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + {ATTR_ENTITY_ID: "climate.thermostat", ATTR_HVAC_MODE: HVAC_MODE_COOL}, blocking=True, ) @@ -173,9 +191,9 @@ async def test_climate_devices(hass): with patch.object(thermostat_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - climate.DOMAIN, - climate.SERVICE_SET_TEMPERATURE, - {"entity_id": "climate.thermostat", "temperature": 20}, + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + {ATTR_ENTITY_ID: "climate.thermostat", ATTR_TEMPERATURE: 20}, blocking=True, ) set_callback.assert_called_with( @@ -188,12 +206,12 @@ async def test_climate_devices(hass): thermostat_device, "_request", return_value=True ) as set_callback, pytest.raises(ValueError): await hass.services.async_call( - climate.DOMAIN, - climate.SERVICE_SET_TEMPERATURE, + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, { - "entity_id": "climate.thermostat", - "target_temp_high": 30, - "target_temp_low": 10, + ATTR_ENTITY_ID: "climate.thermostat", + ATTR_TARGET_TEMP_HIGH: 30, + ATTR_TARGET_TEMP_LOW: 10, }, blocking=True, ) @@ -209,20 +227,20 @@ async def test_clip_climate_device(hass): data["sensors"] = deepcopy(SENSORS) config_entry = await setup_deconz_integration( hass, - options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}, + options={CONF_ALLOW_CLIP_SENSOR: True}, get_state_response=data, ) assert len(hass.states.async_all()) == 3 - assert hass.states.get("climate.thermostat").state == "auto" + assert hass.states.get("climate.thermostat").state == HVAC_MODE_AUTO assert hass.states.get("sensor.thermostat") is None assert hass.states.get("sensor.thermostat_battery_level").state == "100" - assert hass.states.get("climate.clip_thermostat").state == "heat" + assert hass.states.get("climate.clip_thermostat").state == HVAC_MODE_HEAT # Disallow clip sensors hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} + config_entry, options={CONF_ALLOW_CLIP_SENSOR: False} ) await hass.async_block_till_done() @@ -232,12 +250,12 @@ async def test_clip_climate_device(hass): # Allow clip sensors hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} + config_entry, options={CONF_ALLOW_CLIP_SENSOR: True} ) await hass.async_block_till_done() assert len(hass.states.async_all()) == 3 - assert hass.states.get("climate.clip_thermostat").state == "heat" + assert hass.states.get("climate.clip_thermostat").state == HVAC_MODE_HEAT async def test_verify_state_update(hass): @@ -247,7 +265,7 @@ async def test_verify_state_update(hass): config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) - assert hass.states.get("climate.thermostat").state == "auto" + assert hass.states.get("climate.thermostat").state == HVAC_MODE_AUTO state_changed_event = { "t": "event", @@ -259,7 +277,7 @@ async def test_verify_state_update(hass): gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() - assert hass.states.get("climate.thermostat").state == "auto" + assert hass.states.get("climate.thermostat").state == HVAC_MODE_AUTO assert gateway.api.sensors["1"].changed_keys == {"state", "r", "t", "on", "e", "id"} @@ -280,5 +298,5 @@ async def test_add_new_climate_device(hass): await hass.async_block_till_done() assert len(hass.states.async_all()) == 2 - assert hass.states.get("climate.thermostat").state == "auto" + assert hass.states.get("climate.thermostat").state == HVAC_MODE_AUTO assert hass.states.get("sensor.thermostat_battery_level").state == "100" diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index 70894e8926a..dd477e76e7f 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -1,11 +1,9 @@ """Tests for deCONZ config flow.""" + import asyncio import pydeconz -from homeassistant import data_entry_flow -from homeassistant.components import ssdp -from homeassistant.components.deconz import config_flow from homeassistant.components.deconz.config_flow import ( CONF_MANUAL_INPUT, CONF_SERIAL, @@ -18,7 +16,18 @@ from homeassistant.components.deconz.const import ( CONF_MASTER_GATEWAY, DOMAIN, ) +from homeassistant.components.ssdp import ( + ATTR_SSDP_LOCATION, + ATTR_UPNP_MANUFACTURER_URL, + ATTR_UPNP_SERIAL, +) +from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_SSDP, SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON +from homeassistant.data_entry_flow import ( + RESULT_TYPE_ABORT, + RESULT_TYPE_CREATE_ENTRY, + RESULT_TYPE_FORM, +) from .test_gateway import API_KEY, BRIDGEID, setup_deconz_integration @@ -39,17 +48,17 @@ async def test_flow_discovered_bridges(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "user" result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_HOST: "1.2.3.4"} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -62,7 +71,7 @@ async def test_flow_discovered_bridges(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { CONF_HOST: "1.2.3.4", @@ -80,14 +89,14 @@ async def test_flow_manual_configuration_decision(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_HOST: CONF_MANUAL_INPUT} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "manual_input" result = await hass.config_entries.flow.async_configure( @@ -95,7 +104,7 @@ async def test_flow_manual_configuration_decision(hass, aioclient_mock): user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -114,7 +123,7 @@ async def test_flow_manual_configuration_decision(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { CONF_HOST: "1.2.3.4", @@ -132,10 +141,10 @@ async def test_flow_manual_configuration(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "manual_input" result = await hass.config_entries.flow.async_configure( @@ -143,7 +152,7 @@ async def test_flow_manual_configuration(hass, aioclient_mock): user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -162,7 +171,7 @@ async def test_flow_manual_configuration(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { CONF_HOST: "1.2.3.4", @@ -176,23 +185,23 @@ async def test_manual_configuration_after_discovery_timeout(hass, aioclient_mock aioclient_mock.get(pydeconz.utils.URL_DISCOVER, exc=asyncio.TimeoutError) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "manual_input" assert not hass.config_entries.flow._progress[result["flow_id"]].bridges async def test_manual_configuration_after_discovery_ResponseError(hass, aioclient_mock): """Test failed discovery fallbacks to manual configuration.""" - aioclient_mock.get(pydeconz.utils.URL_DISCOVER, exc=config_flow.ResponseError) + aioclient_mock.get(pydeconz.utils.URL_DISCOVER, exc=pydeconz.errors.ResponseError) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "manual_input" assert not hass.config_entries.flow._progress[result["flow_id"]].bridges @@ -208,10 +217,10 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "manual_input" result = await hass.config_entries.flow.async_configure( @@ -219,7 +228,7 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock): user_input={CONF_HOST: "2.3.4.5", CONF_PORT: 80}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -238,7 +247,7 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert config_entry.data[CONF_HOST] == "2.3.4.5" @@ -254,10 +263,10 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "manual_input" result = await hass.config_entries.flow.async_configure( @@ -265,7 +274,7 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -284,7 +293,7 @@ async def test_manual_configuration_dont_update_configuration(hass, aioclient_mo result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" @@ -297,10 +306,10 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "manual_input" result = await hass.config_entries.flow.async_configure( @@ -308,7 +317,7 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock): user_input={CONF_HOST: "1.2.3.4", CONF_PORT: 80}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -325,7 +334,7 @@ async def test_manual_configuration_timeout_get_bridge(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "no_bridges" @@ -338,14 +347,14 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock): ) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": "user"} + DOMAIN, context={"source": SOURCE_USER} ) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_HOST: "1.2.3.4"} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post("http://1.2.3.4:80/api", exc=pydeconz.errors.ResponseError) @@ -354,7 +363,7 @@ async def test_link_get_api_key_ResponseError(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" assert result["errors"] == {"base": "no_key"} @@ -364,14 +373,14 @@ async def test_flow_ssdp_discovery(hass, aioclient_mock): result = await hass.config_entries.flow.async_init( DOMAIN, data={ - ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, - ssdp.ATTR_UPNP_SERIAL: BRIDGEID, + ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", + ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, + ATTR_UPNP_SERIAL: BRIDGEID, }, - context={"source": "ssdp"}, + context={"source": SOURCE_SSDP}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -384,7 +393,7 @@ async def test_flow_ssdp_discovery(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == BRIDGEID assert result["data"] == { CONF_HOST: "1.2.3.4", @@ -398,14 +407,14 @@ async def test_flow_ssdp_discovery_bad_bridge_id_aborts(hass, aioclient_mock): result = await hass.config_entries.flow.async_init( DOMAIN, data={ - ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, - ssdp.ATTR_UPNP_SERIAL: BAD_BRIDGEID, + ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", + ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, + ATTR_UPNP_SERIAL: BAD_BRIDGEID, }, - context={"source": "ssdp"}, + context={"source": SOURCE_SSDP}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "link" aioclient_mock.post( @@ -418,7 +427,7 @@ async def test_flow_ssdp_discovery_bad_bridge_id_aborts(hass, aioclient_mock): result["flow_id"], user_input={} ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "no_hardware_available" @@ -426,11 +435,11 @@ async def test_ssdp_discovery_not_deconz_bridge(hass): """Test a non deconz bridge being discovered over ssdp.""" result = await hass.config_entries.flow.async_init( DOMAIN, - data={ssdp.ATTR_UPNP_MANUFACTURER_URL: "not deconz bridge"}, - context={"source": "ssdp"}, + data={ATTR_UPNP_MANUFACTURER_URL: "not deconz bridge"}, + context={"source": SOURCE_SSDP}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "not_deconz_bridge" @@ -445,15 +454,15 @@ async def test_ssdp_discovery_update_configuration(hass): result = await hass.config_entries.flow.async_init( DOMAIN, data={ - ssdp.ATTR_SSDP_LOCATION: "http://2.3.4.5:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, - ssdp.ATTR_UPNP_SERIAL: BRIDGEID, + ATTR_SSDP_LOCATION: "http://2.3.4.5:80/", + ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, + ATTR_UPNP_SERIAL: BRIDGEID, }, - context={"source": "ssdp"}, + context={"source": SOURCE_SSDP}, ) await hass.async_block_till_done() - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert config_entry.data[CONF_HOST] == "2.3.4.5" assert len(mock_setup_entry.mock_calls) == 1 @@ -466,33 +475,33 @@ async def test_ssdp_discovery_dont_update_configuration(hass): result = await hass.config_entries.flow.async_init( DOMAIN, data={ - ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, - ssdp.ATTR_UPNP_SERIAL: BRIDGEID, + ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", + ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, + ATTR_UPNP_SERIAL: BRIDGEID, }, - context={"source": "ssdp"}, + context={"source": SOURCE_SSDP}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert config_entry.data[CONF_HOST] == "1.2.3.4" async def test_ssdp_discovery_dont_update_existing_hassio_configuration(hass): """Test to ensure the SSDP discovery does not update an Hass.io entry.""" - config_entry = await setup_deconz_integration(hass, source="hassio") + config_entry = await setup_deconz_integration(hass, source=SOURCE_HASSIO) result = await hass.config_entries.flow.async_init( DOMAIN, data={ - ssdp.ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, - ssdp.ATTR_UPNP_SERIAL: BRIDGEID, + ATTR_SSDP_LOCATION: "http://1.2.3.4:80/", + ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, + ATTR_UPNP_SERIAL: BRIDGEID, }, - context={"source": "ssdp"}, + context={"source": SOURCE_SSDP}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert config_entry.data[CONF_HOST] == "1.2.3.4" @@ -508,9 +517,9 @@ async def test_flow_hassio_discovery(hass): CONF_SERIAL: BRIDGEID, CONF_API_KEY: API_KEY, }, - context={"source": "hassio"}, + context={"source": SOURCE_HASSIO}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "hassio_confirm" assert result["description_placeholders"] == {"addon": "Mock Addon"} @@ -525,7 +534,7 @@ async def test_flow_hassio_discovery(hass): ) await hass.async_block_till_done() - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["result"].data == { CONF_HOST: "mock-deconz", CONF_PORT: 80, @@ -551,11 +560,11 @@ async def test_hassio_discovery_update_configuration(hass): CONF_API_KEY: "updated", CONF_SERIAL: BRIDGEID, }, - context={"source": "hassio"}, + context={"source": SOURCE_HASSIO}, ) await hass.async_block_till_done() - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" assert config_entry.data[CONF_HOST] == "2.3.4.5" assert config_entry.data[CONF_PORT] == 8080 @@ -575,10 +584,10 @@ async def test_hassio_discovery_dont_update_configuration(hass): CONF_API_KEY: API_KEY, CONF_SERIAL: BRIDGEID, }, - context={"source": "hassio"}, + context={"source": SOURCE_HASSIO}, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "already_configured" @@ -588,7 +597,7 @@ async def test_option_flow(hass): result = await hass.config_entries.options.async_init(config_entry.entry_id) - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM + assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "deconz_devices" result = await hass.config_entries.options.async_configure( @@ -600,7 +609,7 @@ async def test_option_flow(hass): }, ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["data"] == { CONF_ALLOW_CLIP_SENSOR: False, CONF_ALLOW_DECONZ_GROUPS: False, diff --git a/tests/components/deconz/test_cover.py b/tests/components/deconz/test_cover.py index 799917b26fd..485ae4239be 100644 --- a/tests/components/deconz/test_cover.py +++ b/tests/components/deconz/test_cover.py @@ -1,9 +1,16 @@ """deCONZ cover platform tests.""" + from copy import deepcopy -from homeassistant.components import deconz -import homeassistant.components.cover as cover +from homeassistant.components.cover import ( + DOMAIN as COVER_DOMAIN, + SERVICE_CLOSE_COVER, + SERVICE_OPEN_COVER, + SERVICE_STOP_COVER, +) +from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.const import ATTR_ENTITY_ID, STATE_CLOSED, STATE_OPEN from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -57,11 +64,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, cover.DOMAIN, {"cover": {"platform": deconz.DOMAIN}} + hass, COVER_DOMAIN, {"cover": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_covers(hass): @@ -78,11 +85,11 @@ async def test_cover(hass): gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 5 - assert hass.states.get("cover.level_controllable_cover").state == "open" - assert hass.states.get("cover.window_covering_device").state == "closed" + assert hass.states.get("cover.level_controllable_cover").state == STATE_OPEN + assert hass.states.get("cover.window_covering_device").state == STATE_CLOSED assert hass.states.get("cover.unsupported_cover") is None - assert hass.states.get("cover.deconz_old_brightness_cover").state == "open" - assert hass.states.get("cover.window_covering_controller").state == "closed" + assert hass.states.get("cover.deconz_old_brightness_cover").state == STATE_OPEN + assert hass.states.get("cover.window_covering_controller").state == STATE_CLOSED # Event signals cover is closed @@ -96,7 +103,7 @@ async def test_cover(hass): gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() - assert hass.states.get("cover.level_controllable_cover").state == "closed" + assert hass.states.get("cover.level_controllable_cover").state == STATE_CLOSED # Verify service calls @@ -108,9 +115,9 @@ async def test_cover(hass): level_controllable_cover_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - cover.DOMAIN, - cover.SERVICE_OPEN_COVER, - {"entity_id": "cover.level_controllable_cover"}, + COVER_DOMAIN, + SERVICE_OPEN_COVER, + {ATTR_ENTITY_ID: "cover.level_controllable_cover"}, blocking=True, ) await hass.async_block_till_done() @@ -122,9 +129,9 @@ async def test_cover(hass): level_controllable_cover_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - cover.DOMAIN, - cover.SERVICE_CLOSE_COVER, - {"entity_id": "cover.level_controllable_cover"}, + COVER_DOMAIN, + SERVICE_CLOSE_COVER, + {ATTR_ENTITY_ID: "cover.level_controllable_cover"}, blocking=True, ) await hass.async_block_till_done() @@ -138,16 +145,16 @@ async def test_cover(hass): level_controllable_cover_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - cover.DOMAIN, - cover.SERVICE_STOP_COVER, - {"entity_id": "cover.level_controllable_cover"}, + COVER_DOMAIN, + SERVICE_STOP_COVER, + {ATTR_ENTITY_ID: "cover.level_controllable_cover"}, blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with("put", "/lights/1/state", json={"bri_inc": 0}) # Test that a reported cover position of 255 (deconz-rest-api < 2.05.73) is interpreted correctly. - assert hass.states.get("cover.deconz_old_brightness_cover").state == "open" + assert hass.states.get("cover.deconz_old_brightness_cover").state == STATE_OPEN state_changed_event = { "t": "event", @@ -160,7 +167,7 @@ async def test_cover(hass): await hass.async_block_till_done() deconz_old_brightness_cover = hass.states.get("cover.deconz_old_brightness_cover") - assert deconz_old_brightness_cover.state == "closed" + assert deconz_old_brightness_cover.state == STATE_CLOSED assert deconz_old_brightness_cover.attributes["current_position"] == 0 await hass.config_entries.async_unload(config_entry.entry_id) diff --git a/tests/components/deconz/test_deconz_event.py b/tests/components/deconz/test_deconz_event.py index 252fd8c3062..e1492fb0fcf 100644 --- a/tests/components/deconz/test_deconz_event.py +++ b/tests/components/deconz/test_deconz_event.py @@ -1,4 +1,5 @@ """Test deCONZ remote events.""" + from copy import deepcopy from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT diff --git a/tests/components/deconz/test_device_trigger.py b/tests/components/deconz/test_device_trigger.py index c83b0710805..69c9ae94727 100644 --- a/tests/components/deconz/test_device_trigger.py +++ b/tests/components/deconz/test_device_trigger.py @@ -1,8 +1,20 @@ """deCONZ device automation tests.""" + from copy import deepcopy from homeassistant.components.deconz import device_trigger +from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN +from homeassistant.components.deconz.device_trigger import CONF_SUBTYPE from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.const import ( + ATTR_BATTERY_LEVEL, + ATTR_ENTITY_ID, + CONF_DEVICE_ID, + CONF_DOMAIN, + CONF_PLATFORM, + CONF_TYPE, +) from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -25,7 +37,7 @@ SENSORS = { "name": "TRÅDFRI on/off switch ", "state": {"buttonevent": 2002, "lastupdated": "2019-09-07T07:39:39"}, "swversion": "1.4.018", - "type": "ZHASwitch", + CONF_TYPE: "ZHASwitch", "uniqueid": "d0:cf:5e:ff:fe:71:a4:3a-01-1000", } } @@ -42,53 +54,53 @@ async def test_get_triggers(hass): expected_triggers = [ { - "device_id": device_id, - "domain": "deconz", - "platform": "device", - "type": device_trigger.CONF_SHORT_PRESS, - "subtype": device_trigger.CONF_TURN_ON, + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DECONZ_DOMAIN, + CONF_PLATFORM: "device", + CONF_TYPE: device_trigger.CONF_SHORT_PRESS, + CONF_SUBTYPE: device_trigger.CONF_TURN_ON, }, { - "device_id": device_id, - "domain": "deconz", - "platform": "device", - "type": device_trigger.CONF_LONG_PRESS, - "subtype": device_trigger.CONF_TURN_ON, + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DECONZ_DOMAIN, + CONF_PLATFORM: "device", + CONF_TYPE: device_trigger.CONF_LONG_PRESS, + CONF_SUBTYPE: device_trigger.CONF_TURN_ON, }, { - "device_id": device_id, - "domain": "deconz", - "platform": "device", - "type": device_trigger.CONF_LONG_RELEASE, - "subtype": device_trigger.CONF_TURN_ON, + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DECONZ_DOMAIN, + CONF_PLATFORM: "device", + CONF_TYPE: device_trigger.CONF_LONG_RELEASE, + CONF_SUBTYPE: device_trigger.CONF_TURN_ON, }, { - "device_id": device_id, - "domain": "deconz", - "platform": "device", - "type": device_trigger.CONF_SHORT_PRESS, - "subtype": device_trigger.CONF_TURN_OFF, + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DECONZ_DOMAIN, + CONF_PLATFORM: "device", + CONF_TYPE: device_trigger.CONF_SHORT_PRESS, + CONF_SUBTYPE: device_trigger.CONF_TURN_OFF, }, { - "device_id": device_id, - "domain": "deconz", - "platform": "device", - "type": device_trigger.CONF_LONG_PRESS, - "subtype": device_trigger.CONF_TURN_OFF, + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DECONZ_DOMAIN, + CONF_PLATFORM: "device", + CONF_TYPE: device_trigger.CONF_LONG_PRESS, + CONF_SUBTYPE: device_trigger.CONF_TURN_OFF, }, { - "device_id": device_id, - "domain": "deconz", - "platform": "device", - "type": device_trigger.CONF_LONG_RELEASE, - "subtype": device_trigger.CONF_TURN_OFF, + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DECONZ_DOMAIN, + CONF_PLATFORM: "device", + CONF_TYPE: device_trigger.CONF_LONG_RELEASE, + CONF_SUBTYPE: device_trigger.CONF_TURN_OFF, }, { - "device_id": device_id, - "domain": "sensor", - "entity_id": "sensor.tradfri_on_off_switch_battery_level", - "platform": "device", - "type": "battery_level", + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: SENSOR_DOMAIN, + ATTR_ENTITY_ID: "sensor.tradfri_on_off_switch_battery_level", + CONF_PLATFORM: "device", + CONF_TYPE: ATTR_BATTERY_LEVEL, }, ] diff --git a/tests/components/deconz/test_fan.py b/tests/components/deconz/test_fan.py index cabafeb481d..6806071dd75 100644 --- a/tests/components/deconz/test_fan.py +++ b/tests/components/deconz/test_fan.py @@ -1,12 +1,23 @@ """deCONZ fan platform tests.""" + from copy import deepcopy import pytest -from homeassistant.components import deconz +from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN from homeassistant.components.deconz.gateway import get_gateway_from_config_entry -import homeassistant.components.fan as fan -from homeassistant.const import STATE_OFF, STATE_ON +from homeassistant.components.fan import ( + ATTR_SPEED, + DOMAIN as FAN_DOMAIN, + SERVICE_SET_SPEED, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + SPEED_HIGH, + SPEED_LOW, + SPEED_MEDIUM, + SPEED_OFF, +) +from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -38,11 +49,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, fan.DOMAIN, {"fan": {"platform": deconz.DOMAIN}} + hass, FAN_DOMAIN, {"fan": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_fans(hass): @@ -64,7 +75,7 @@ async def test_fans(hass): # Test states assert hass.states.get("fan.ceiling_fan").state == STATE_ON - assert hass.states.get("fan.ceiling_fan").attributes["speed"] == fan.SPEED_HIGH + assert hass.states.get("fan.ceiling_fan").attributes["speed"] == SPEED_HIGH state_changed_event = { "t": "event", @@ -77,7 +88,7 @@ async def test_fans(hass): await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_OFF - assert hass.states.get("fan.ceiling_fan").attributes["speed"] == fan.SPEED_OFF + assert hass.states.get("fan.ceiling_fan").attributes["speed"] == SPEED_OFF # Test service calls @@ -89,9 +100,9 @@ async def test_fans(hass): ceiling_fan_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - fan.DOMAIN, - fan.SERVICE_TURN_ON, - {"entity_id": "fan.ceiling_fan"}, + FAN_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "fan.ceiling_fan"}, blocking=True, ) await hass.async_block_till_done() @@ -103,9 +114,9 @@ async def test_fans(hass): ceiling_fan_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - fan.DOMAIN, - fan.SERVICE_TURN_OFF, - {"entity_id": "fan.ceiling_fan"}, + FAN_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "fan.ceiling_fan"}, blocking=True, ) await hass.async_block_till_done() @@ -117,9 +128,9 @@ async def test_fans(hass): ceiling_fan_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - fan.DOMAIN, - fan.SERVICE_SET_SPEED, - {"entity_id": "fan.ceiling_fan", fan.ATTR_SPEED: fan.SPEED_LOW}, + FAN_DOMAIN, + SERVICE_SET_SPEED, + {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_SPEED: SPEED_LOW}, blocking=True, ) await hass.async_block_till_done() @@ -131,9 +142,9 @@ async def test_fans(hass): ceiling_fan_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - fan.DOMAIN, - fan.SERVICE_SET_SPEED, - {"entity_id": "fan.ceiling_fan", fan.ATTR_SPEED: fan.SPEED_MEDIUM}, + FAN_DOMAIN, + SERVICE_SET_SPEED, + {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_SPEED: SPEED_MEDIUM}, blocking=True, ) await hass.async_block_till_done() @@ -145,9 +156,9 @@ async def test_fans(hass): ceiling_fan_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - fan.DOMAIN, - fan.SERVICE_SET_SPEED, - {"entity_id": "fan.ceiling_fan", fan.ATTR_SPEED: fan.SPEED_HIGH}, + FAN_DOMAIN, + SERVICE_SET_SPEED, + {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_SPEED: SPEED_HIGH}, blocking=True, ) await hass.async_block_till_done() @@ -159,9 +170,9 @@ async def test_fans(hass): ceiling_fan_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - fan.DOMAIN, - fan.SERVICE_SET_SPEED, - {"entity_id": "fan.ceiling_fan", fan.ATTR_SPEED: fan.SPEED_OFF}, + FAN_DOMAIN, + SERVICE_SET_SPEED, + {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_SPEED: SPEED_OFF}, blocking=True, ) await hass.async_block_till_done() @@ -173,9 +184,9 @@ async def test_fans(hass): ceiling_fan_device, "_request", return_value=True ) as set_callback, pytest.raises(ValueError): await hass.services.async_call( - fan.DOMAIN, - fan.SERVICE_SET_SPEED, - {"entity_id": "fan.ceiling_fan", fan.ATTR_SPEED: "bad value"}, + FAN_DOMAIN, + SERVICE_SET_SPEED, + {ATTR_ENTITY_ID: "fan.ceiling_fan", ATTR_SPEED: "bad value"}, blocking=True, ) await hass.async_block_till_done() @@ -193,7 +204,7 @@ async def test_fans(hass): await hass.async_block_till_done() assert hass.states.get("fan.ceiling_fan").state == STATE_ON - assert hass.states.get("fan.ceiling_fan").attributes["speed"] == fan.SPEED_MEDIUM + assert hass.states.get("fan.ceiling_fan").attributes["speed"] == SPEED_MEDIUM await hass.config_entries.async_unload(config_entry.entry_id) diff --git a/tests/components/deconz/test_gateway.py b/tests/components/deconz/test_gateway.py index 2ac14c136a7..12666cdb692 100644 --- a/tests/components/deconz/test_gateway.py +++ b/tests/components/deconz/test_gateway.py @@ -1,12 +1,34 @@ """Test deCONZ gateway.""" + from copy import deepcopy import pydeconz import pytest -from homeassistant import config_entries -from homeassistant.components import deconz, ssdp -from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN +from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN +from homeassistant.components.cover import DOMAIN as COVER_DOMAIN +from homeassistant.components.deconz.config_flow import DECONZ_MANUFACTURERURL +from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN +from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect +from homeassistant.components.deconz.gateway import ( + get_gateway, + get_gateway_from_config_entry, +) +from homeassistant.components.fan import DOMAIN as FAN_DOMAIN +from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN +from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN +from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.components.ssdp import ( + ATTR_SSDP_LOCATION, + ATTR_UPNP_MANUFACTURER_URL, + ATTR_UPNP_SERIAL, + ATTR_UPNP_UDN, +) +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.config_entries import CONN_CLASS_LOCAL_PUSH, SOURCE_SSDP +from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT from homeassistant.helpers.dispatcher import async_dispatcher_connect from tests.async_mock import Mock, patch @@ -15,11 +37,7 @@ from tests.common import MockConfigEntry API_KEY = "1234567890ABCDEF" BRIDGEID = "01234E56789A" -ENTRY_CONFIG = { - deconz.config_flow.CONF_API_KEY: API_KEY, - deconz.config_flow.CONF_HOST: "1.2.3.4", - deconz.config_flow.CONF_PORT: 80, -} +ENTRY_CONFIG = {CONF_API_KEY: API_KEY, CONF_HOST: "1.2.3.4", CONF_PORT: 80} ENTRY_OPTIONS = {} @@ -52,10 +70,10 @@ async def setup_deconz_integration( ): """Create the deCONZ gateway.""" config_entry = MockConfigEntry( - domain=deconz.DOMAIN, + domain=DECONZ_DOMAIN, source=source, data=deepcopy(config), - connection_class=config_entries.CONN_CLASS_LOCAL_PUSH, + connection_class=CONN_CLASS_LOCAL_PUSH, options=deepcopy(options), entry_id=entry_id, ) @@ -87,25 +105,28 @@ async def test_gateway_setup(hass): assert len(gateway.deconz_ids) == 0 assert len(hass.states.async_all()) == 0 - assert forward_entry_setup.mock_calls[0][1] == (config_entry, "binary_sensor") - assert forward_entry_setup.mock_calls[1][1] == (config_entry, "climate") - assert forward_entry_setup.mock_calls[2][1] == (config_entry, "cover") - assert forward_entry_setup.mock_calls[3][1] == (config_entry, "fan") - assert forward_entry_setup.mock_calls[4][1] == (config_entry, "light") - assert forward_entry_setup.mock_calls[5][1] == (config_entry, "lock") - assert forward_entry_setup.mock_calls[6][1] == (config_entry, "scene") - assert forward_entry_setup.mock_calls[7][1] == (config_entry, "sensor") - assert forward_entry_setup.mock_calls[8][1] == (config_entry, "switch") + assert forward_entry_setup.mock_calls[0][1] == ( + config_entry, + BINARY_SENSOR_DOMAIN, + ) + assert forward_entry_setup.mock_calls[1][1] == (config_entry, CLIMATE_DOMAIN) + assert forward_entry_setup.mock_calls[2][1] == (config_entry, COVER_DOMAIN) + assert forward_entry_setup.mock_calls[3][1] == (config_entry, FAN_DOMAIN) + assert forward_entry_setup.mock_calls[4][1] == (config_entry, LIGHT_DOMAIN) + assert forward_entry_setup.mock_calls[5][1] == (config_entry, LOCK_DOMAIN) + assert forward_entry_setup.mock_calls[6][1] == (config_entry, SCENE_DOMAIN) + assert forward_entry_setup.mock_calls[7][1] == (config_entry, SENSOR_DOMAIN) + assert forward_entry_setup.mock_calls[8][1] == (config_entry, SWITCH_DOMAIN) async def test_gateway_retry(hass): """Retry setup.""" with patch( "homeassistant.components.deconz.gateway.get_gateway", - side_effect=deconz.errors.CannotConnect, + side_effect=CannotConnect, ): await setup_deconz_integration(hass) - assert not hass.data[deconz.DOMAIN] + assert not hass.data[DECONZ_DOMAIN] async def test_gateway_setup_fails(hass): @@ -114,7 +135,7 @@ async def test_gateway_setup_fails(hass): "homeassistant.components.deconz.gateway.get_gateway", side_effect=Exception ): await setup_deconz_integration(hass) - assert not hass.data[deconz.DOMAIN] + assert not hass.data[DECONZ_DOMAIN] async def test_connection_status_signalling(hass): @@ -145,14 +166,14 @@ async def test_update_address(hass): return_value=True, ) as mock_setup_entry: await hass.config_entries.flow.async_init( - deconz.config_flow.DOMAIN, + DECONZ_DOMAIN, data={ - ssdp.ATTR_SSDP_LOCATION: "http://2.3.4.5:80/", - ssdp.ATTR_UPNP_MANUFACTURER_URL: deconz.config_flow.DECONZ_MANUFACTURERURL, - ssdp.ATTR_UPNP_SERIAL: BRIDGEID, - ssdp.ATTR_UPNP_UDN: "uuid:456DEF", + ATTR_SSDP_LOCATION: "http://2.3.4.5:80/", + ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, + ATTR_UPNP_SERIAL: BRIDGEID, + ATTR_UPNP_UDN: "uuid:456DEF", }, - context={"source": "ssdp"}, + context={"source": SOURCE_SSDP}, ) await hass.async_block_till_done() @@ -174,7 +195,7 @@ async def test_reset_after_successful_setup(hass): async def test_get_gateway(hass): """Successful call.""" with patch("pydeconz.DeconzSession.initialize", return_value=True): - assert await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) + assert await get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) async def test_get_gateway_fails_unauthorized(hass): @@ -182,11 +203,8 @@ async def test_get_gateway_fails_unauthorized(hass): with patch( "pydeconz.DeconzSession.initialize", side_effect=pydeconz.errors.Unauthorized, - ), pytest.raises(deconz.errors.AuthenticationRequired): - assert ( - await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) - is False - ) + ), pytest.raises(AuthenticationRequired): + assert await get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) is False async def test_get_gateway_fails_cannot_connect(hass): @@ -194,8 +212,5 @@ async def test_get_gateway_fails_cannot_connect(hass): with patch( "pydeconz.DeconzSession.initialize", side_effect=pydeconz.errors.RequestError, - ), pytest.raises(deconz.errors.CannotConnect): - assert ( - await deconz.gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) - is False - ) + ), pytest.raises(CannotConnect): + assert await get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) is False diff --git a/tests/components/deconz/test_init.py b/tests/components/deconz/test_init.py index 8fd29ec2368..ae7ce5b2a39 100644 --- a/tests/components/deconz/test_init.py +++ b/tests/components/deconz/test_init.py @@ -1,8 +1,14 @@ """Test deCONZ component setup process.""" + import asyncio from copy import deepcopy -from homeassistant.components import deconz +from homeassistant.components.deconz import ( + DeconzGateway, + async_setup_entry, + async_unload_entry, +) +from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -24,26 +30,24 @@ ENTRY2_UUID = "789ACE" async def setup_entry(hass, entry): """Test that setup entry works.""" - with patch.object( - deconz.DeconzGateway, "async_setup", return_value=True - ), patch.object( - deconz.DeconzGateway, "async_update_device_registry", return_value=True + with patch.object(DeconzGateway, "async_setup", return_value=True), patch.object( + DeconzGateway, "async_update_device_registry", return_value=True ): - assert await deconz.async_setup_entry(hass, entry) is True + assert await async_setup_entry(hass, entry) is True async def test_setup_entry_fails(hass): """Test setup entry fails if deCONZ is not available.""" with patch("pydeconz.DeconzSession.initialize", side_effect=Exception): await setup_deconz_integration(hass) - assert not hass.data[deconz.DOMAIN] + assert not hass.data[DECONZ_DOMAIN] async def test_setup_entry_no_available_bridge(hass): """Test setup entry fails if deCONZ is not available.""" with patch("pydeconz.DeconzSession.initialize", side_effect=asyncio.TimeoutError): await setup_deconz_integration(hass) - assert not hass.data[deconz.DOMAIN] + assert not hass.data[DECONZ_DOMAIN] async def test_setup_entry_successful(hass): @@ -51,9 +55,9 @@ async def test_setup_entry_successful(hass): config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) - assert hass.data[deconz.DOMAIN] - assert gateway.bridgeid in hass.data[deconz.DOMAIN] - assert hass.data[deconz.DOMAIN][gateway.bridgeid].master + assert hass.data[DECONZ_DOMAIN] + assert gateway.bridgeid in hass.data[DECONZ_DOMAIN] + assert hass.data[DECONZ_DOMAIN][gateway.bridgeid].master async def test_setup_entry_multiple_gateways(hass): @@ -68,18 +72,18 @@ async def test_setup_entry_multiple_gateways(hass): ) gateway2 = get_gateway_from_config_entry(hass, config_entry2) - assert len(hass.data[deconz.DOMAIN]) == 2 - assert hass.data[deconz.DOMAIN][gateway.bridgeid].master - assert not hass.data[deconz.DOMAIN][gateway2.bridgeid].master + assert len(hass.data[DECONZ_DOMAIN]) == 2 + assert hass.data[DECONZ_DOMAIN][gateway.bridgeid].master + assert not hass.data[DECONZ_DOMAIN][gateway2.bridgeid].master async def test_unload_entry(hass): """Test being able to unload an entry.""" config_entry = await setup_deconz_integration(hass) - assert hass.data[deconz.DOMAIN] + assert hass.data[DECONZ_DOMAIN] - assert await deconz.async_unload_entry(hass, config_entry) - assert not hass.data[deconz.DOMAIN] + assert await async_unload_entry(hass, config_entry) + assert not hass.data[DECONZ_DOMAIN] async def test_unload_entry_multiple_gateways(hass): @@ -93,9 +97,9 @@ async def test_unload_entry_multiple_gateways(hass): ) gateway2 = get_gateway_from_config_entry(hass, config_entry2) - assert len(hass.data[deconz.DOMAIN]) == 2 + assert len(hass.data[DECONZ_DOMAIN]) == 2 - assert await deconz.async_unload_entry(hass, config_entry) + assert await async_unload_entry(hass, config_entry) - assert len(hass.data[deconz.DOMAIN]) == 1 - assert hass.data[deconz.DOMAIN][gateway2.bridgeid].master + assert len(hass.data[DECONZ_DOMAIN]) == 1 + assert hass.data[DECONZ_DOMAIN][gateway2.bridgeid].master diff --git a/tests/components/deconz/test_light.py b/tests/components/deconz/test_light.py index 00a3f49477e..460f81e830c 100644 --- a/tests/components/deconz/test_light.py +++ b/tests/components/deconz/test_light.py @@ -1,9 +1,34 @@ """deCONZ light platform tests.""" + from copy import deepcopy -from homeassistant.components import deconz +from homeassistant.components.deconz.const import ( + CONF_ALLOW_DECONZ_GROUPS, + DOMAIN as DECONZ_DOMAIN, +) from homeassistant.components.deconz.gateway import get_gateway_from_config_entry -import homeassistant.components.light as light +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, + ATTR_COLOR_TEMP, + ATTR_EFFECT, + ATTR_FLASH, + ATTR_HS_COLOR, + ATTR_MAX_MIREDS, + ATTR_MIN_MIREDS, + ATTR_TRANSITION, + DOMAIN as LIGHT_DOMAIN, + EFFECT_COLORLOOP, + FLASH_LONG, + FLASH_SHORT, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) +from homeassistant.const import ( + ATTR_ENTITY_ID, + ATTR_SUPPORTED_FEATURES, + STATE_OFF, + STATE_ON, +) from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -84,11 +109,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, light.DOMAIN, {"light": {"platform": deconz.DOMAIN}} + hass, LIGHT_DOMAIN, {"light": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_lights_or_groups(hass): @@ -108,34 +133,34 @@ async def test_lights_and_groups(hass): assert len(hass.states.async_all()) == 6 rgb_light = hass.states.get("light.rgb_light") - assert rgb_light.state == "on" - assert rgb_light.attributes["brightness"] == 255 - assert rgb_light.attributes["hs_color"] == (224.235, 100.0) + assert rgb_light.state == STATE_ON + assert rgb_light.attributes[ATTR_BRIGHTNESS] == 255 + assert rgb_light.attributes[ATTR_HS_COLOR] == (224.235, 100.0) assert rgb_light.attributes["is_deconz_group"] is False - assert rgb_light.attributes["supported_features"] == 61 + assert rgb_light.attributes[ATTR_SUPPORTED_FEATURES] == 61 tunable_white_light = hass.states.get("light.tunable_white_light") - assert tunable_white_light.state == "on" - assert tunable_white_light.attributes["color_temp"] == 2500 - assert tunable_white_light.attributes["max_mireds"] == 454 - assert tunable_white_light.attributes["min_mireds"] == 155 - assert tunable_white_light.attributes["supported_features"] == 2 + assert tunable_white_light.state == STATE_ON + assert tunable_white_light.attributes[ATTR_COLOR_TEMP] == 2500 + assert tunable_white_light.attributes[ATTR_MAX_MIREDS] == 454 + assert tunable_white_light.attributes[ATTR_MIN_MIREDS] == 155 + assert tunable_white_light.attributes[ATTR_SUPPORTED_FEATURES] == 2 tunable_white_light_bad_maxmin = hass.states.get( "light.tunable_white_light_with_bad_maxmin_values" ) - assert tunable_white_light_bad_maxmin.state == "on" - assert tunable_white_light_bad_maxmin.attributes["color_temp"] == 2500 - assert tunable_white_light_bad_maxmin.attributes["max_mireds"] == 650 - assert tunable_white_light_bad_maxmin.attributes["min_mireds"] == 140 - assert tunable_white_light_bad_maxmin.attributes["supported_features"] == 2 + assert tunable_white_light_bad_maxmin.state == STATE_ON + assert tunable_white_light_bad_maxmin.attributes[ATTR_COLOR_TEMP] == 2500 + assert tunable_white_light_bad_maxmin.attributes[ATTR_MAX_MIREDS] == 650 + assert tunable_white_light_bad_maxmin.attributes[ATTR_MIN_MIREDS] == 140 + assert tunable_white_light_bad_maxmin.attributes[ATTR_SUPPORTED_FEATURES] == 2 on_off_light = hass.states.get("light.on_off_light") - assert on_off_light.state == "on" - assert on_off_light.attributes["supported_features"] == 0 + assert on_off_light.state == STATE_ON + assert on_off_light.attributes[ATTR_SUPPORTED_FEATURES] == 0 light_group = hass.states.get("light.light_group") - assert light_group.state == "on" + assert light_group.state == STATE_ON assert light_group.attributes["all_on"] is False empty_group = hass.states.get("light.empty_group") @@ -152,7 +177,7 @@ async def test_lights_and_groups(hass): await hass.async_block_till_done() rgb_light = hass.states.get("light.rgb_light") - assert rgb_light.state == "off" + assert rgb_light.state == STATE_OFF # Verify service calls @@ -162,15 +187,15 @@ async def test_lights_and_groups(hass): with patch.object(rgb_light_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - light.DOMAIN, - light.SERVICE_TURN_ON, + LIGHT_DOMAIN, + SERVICE_TURN_ON, { - "entity_id": "light.rgb_light", - "color_temp": 2500, - "brightness": 200, - "transition": 5, - "flash": "short", - "effect": "colorloop", + ATTR_ENTITY_ID: "light.rgb_light", + ATTR_COLOR_TEMP: 2500, + ATTR_BRIGHTNESS: 200, + ATTR_TRANSITION: 5, + ATTR_FLASH: FLASH_SHORT, + ATTR_EFFECT: EFFECT_COLORLOOP, }, blocking=True, ) @@ -191,13 +216,13 @@ async def test_lights_and_groups(hass): with patch.object(rgb_light_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - light.DOMAIN, - light.SERVICE_TURN_ON, + LIGHT_DOMAIN, + SERVICE_TURN_ON, { - "entity_id": "light.rgb_light", - "hs_color": (20, 30), - "flash": "long", - "effect": "None", + ATTR_ENTITY_ID: "light.rgb_light", + ATTR_HS_COLOR: (20, 30), + ATTR_FLASH: FLASH_LONG, + ATTR_EFFECT: "None", }, blocking=True, ) @@ -212,9 +237,13 @@ async def test_lights_and_groups(hass): with patch.object(rgb_light_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - light.DOMAIN, - light.SERVICE_TURN_OFF, - {"entity_id": "light.rgb_light", "transition": 5, "flash": "short"}, + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + { + ATTR_ENTITY_ID: "light.rgb_light", + ATTR_TRANSITION: 5, + ATTR_FLASH: FLASH_SHORT, + }, blocking=True, ) await hass.async_block_till_done() @@ -234,9 +263,13 @@ async def test_lights_and_groups(hass): with patch.object(rgb_light_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - light.DOMAIN, - light.SERVICE_TURN_OFF, - {"entity_id": "light.rgb_light", "transition": 5, "flash": "short"}, + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + { + ATTR_ENTITY_ID: "light.rgb_light", + ATTR_TRANSITION: 5, + ATTR_FLASH: FLASH_SHORT, + }, blocking=True, ) await hass.async_block_till_done() @@ -250,9 +283,9 @@ async def test_lights_and_groups(hass): with patch.object(rgb_light_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - light.DOMAIN, - light.SERVICE_TURN_OFF, - {"entity_id": "light.rgb_light", "flash": "long"}, + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "light.rgb_light", ATTR_FLASH: FLASH_LONG}, blocking=True, ) await hass.async_block_till_done() @@ -272,7 +305,7 @@ async def test_disable_light_groups(hass): data["lights"] = deepcopy(LIGHTS) config_entry = await setup_deconz_integration( hass, - options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: False}, + options={CONF_ALLOW_DECONZ_GROUPS: False}, get_state_response=data, ) @@ -283,7 +316,7 @@ async def test_disable_light_groups(hass): assert hass.states.get("light.empty_group") is None hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: True} + config_entry, options={CONF_ALLOW_DECONZ_GROUPS: True} ) await hass.async_block_till_done() @@ -291,7 +324,7 @@ async def test_disable_light_groups(hass): assert hass.states.get("light.light_group") hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: False} + config_entry, options={CONF_ALLOW_DECONZ_GROUPS: False} ) await hass.async_block_till_done() diff --git a/tests/components/deconz/test_lock.py b/tests/components/deconz/test_lock.py index cb7267aaada..2acfb440b13 100644 --- a/tests/components/deconz/test_lock.py +++ b/tests/components/deconz/test_lock.py @@ -1,10 +1,15 @@ """deCONZ lock platform tests.""" + from copy import deepcopy -from homeassistant.components import deconz +from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN from homeassistant.components.deconz.gateway import get_gateway_from_config_entry -import homeassistant.components.lock as lock -from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED +from homeassistant.components.lock import ( + DOMAIN as LOCK_DOMAIN, + SERVICE_LOCK, + SERVICE_UNLOCK, +) +from homeassistant.const import ATTR_ENTITY_ID, STATE_LOCKED, STATE_UNLOCKED from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -32,11 +37,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, lock.DOMAIN, {"lock": {"platform": deconz.DOMAIN}} + hass, LOCK_DOMAIN, {"lock": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_locks(hass): @@ -78,9 +83,9 @@ async def test_locks(hass): with patch.object(door_lock_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - lock.DOMAIN, - lock.SERVICE_LOCK, - {"entity_id": "lock.door_lock"}, + LOCK_DOMAIN, + SERVICE_LOCK, + {ATTR_ENTITY_ID: "lock.door_lock"}, blocking=True, ) await hass.async_block_till_done() @@ -90,9 +95,9 @@ async def test_locks(hass): with patch.object(door_lock_device, "_request", return_value=True) as set_callback: await hass.services.async_call( - lock.DOMAIN, - lock.SERVICE_UNLOCK, - {"entity_id": "lock.door_lock"}, + LOCK_DOMAIN, + SERVICE_UNLOCK, + {ATTR_ENTITY_ID: "lock.door_lock"}, blocking=True, ) await hass.async_block_till_done() diff --git a/tests/components/deconz/test_scene.py b/tests/components/deconz/test_scene.py index 4717ccb0ba8..c4a92538815 100644 --- a/tests/components/deconz/test_scene.py +++ b/tests/components/deconz/test_scene.py @@ -1,9 +1,11 @@ """deCONZ scene platform tests.""" + from copy import deepcopy -from homeassistant.components import deconz +from homeassistant.components.deconz import DOMAIN as DECONZ_DOMAIN from homeassistant.components.deconz.gateway import get_gateway_from_config_entry -import homeassistant.components.scene as scene +from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON +from homeassistant.const import ATTR_ENTITY_ID from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -27,11 +29,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, scene.DOMAIN, {"scene": {"platform": deconz.DOMAIN}} + hass, SCENE_DOMAIN, {"scene": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_scenes(hass): @@ -58,7 +60,10 @@ async def test_scenes(hass): with patch.object(group_scene, "_request", return_value=True) as set_callback: await hass.services.async_call( - "scene", "turn_on", {"entity_id": "scene.light_group_scene"}, blocking=True + SCENE_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "scene.light_group_scene"}, + blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with("put", "/groups/1/scenes/1/recall", json={}) diff --git a/tests/components/deconz/test_sensor.py b/tests/components/deconz/test_sensor.py index 5cfb01ddf9d..8b2f1e4da76 100644 --- a/tests/components/deconz/test_sensor.py +++ b/tests/components/deconz/test_sensor.py @@ -1,9 +1,13 @@ """deCONZ sensor platform tests.""" + from copy import deepcopy -from homeassistant.components import deconz +from homeassistant.components.deconz.const import ( + CONF_ALLOW_CLIP_SENSOR, + DOMAIN as DECONZ_DOMAIN, +) from homeassistant.components.deconz.gateway import get_gateway_from_config_entry -import homeassistant.components.sensor as sensor +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.const import ( DEVICE_CLASS_BATTERY, DEVICE_CLASS_ILLUMINANCE, @@ -85,11 +89,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, sensor.DOMAIN, {"sensor": {"platform": deconz.DOMAIN}} + hass, SENSOR_DOMAIN, {"sensor": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_sensors(hass): @@ -170,7 +174,7 @@ async def test_allow_clip_sensors(hass): data["sensors"] = deepcopy(SENSORS) config_entry = await setup_deconz_integration( hass, - options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}, + options={CONF_ALLOW_CLIP_SENSOR: True}, get_state_response=data, ) @@ -180,7 +184,7 @@ async def test_allow_clip_sensors(hass): # Disallow clip sensors hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: False} + config_entry, options={CONF_ALLOW_CLIP_SENSOR: False} ) await hass.async_block_till_done() @@ -190,7 +194,7 @@ async def test_allow_clip_sensors(hass): # Allow clip sensors hass.config_entries.async_update_entry( - config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True} + config_entry, options={CONF_ALLOW_CLIP_SENSOR: True} ) await hass.async_block_till_done() diff --git a/tests/components/deconz/test_services.py b/tests/components/deconz/test_services.py index e8cd1c23dc9..5b6a0e9520b 100644 --- a/tests/components/deconz/test_services.py +++ b/tests/components/deconz/test_services.py @@ -5,9 +5,22 @@ from copy import deepcopy import pytest import voluptuous as vol -from homeassistant.components import deconz -from homeassistant.components.deconz.const import CONF_BRIDGE_ID +from homeassistant.components.deconz.const import ( + CONF_BRIDGE_ID, + DOMAIN as DECONZ_DOMAIN, +) from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.components.deconz.services import ( + DECONZ_SERVICES, + SERVICE_CONFIGURE_DEVICE, + SERVICE_DATA, + SERVICE_DEVICE_REFRESH, + SERVICE_ENTITY, + SERVICE_FIELD, + SERVICE_REMOVE_ORPHANED_ENTRIES, + async_setup_services, + async_unload_services, +) from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.helpers.entity_registry import async_entries_for_config_entry @@ -62,33 +75,33 @@ SWITCH = { async def test_service_setup(hass): """Verify service setup works.""" - assert deconz.services.DECONZ_SERVICES not in hass.data + assert DECONZ_SERVICES not in hass.data with patch( "homeassistant.core.ServiceRegistry.async_register", return_value=Mock(True) ) as async_register: - await deconz.services.async_setup_services(hass) - assert hass.data[deconz.services.DECONZ_SERVICES] is True + await async_setup_services(hass) + assert hass.data[DECONZ_SERVICES] is True assert async_register.call_count == 3 async def test_service_setup_already_registered(hass): """Make sure that services are only registered once.""" - hass.data[deconz.services.DECONZ_SERVICES] = True + hass.data[DECONZ_SERVICES] = True with patch( "homeassistant.core.ServiceRegistry.async_register", return_value=Mock(True) ) as async_register: - await deconz.services.async_setup_services(hass) + await async_setup_services(hass) async_register.assert_not_called() async def test_service_unload(hass): """Verify service unload works.""" - hass.data[deconz.services.DECONZ_SERVICES] = True + hass.data[DECONZ_SERVICES] = True with patch( "homeassistant.core.ServiceRegistry.async_remove", return_value=Mock(True) ) as async_remove: - await deconz.services.async_unload_services(hass) - assert hass.data[deconz.services.DECONZ_SERVICES] is False + await async_unload_services(hass) + assert hass.data[DECONZ_SERVICES] is False assert async_remove.call_count == 3 @@ -97,8 +110,8 @@ async def test_service_unload_not_registered(hass): with patch( "homeassistant.core.ServiceRegistry.async_remove", return_value=Mock(True) ) as async_remove: - await deconz.services.async_unload_services(hass) - assert deconz.services.DECONZ_SERVICES not in hass.data + await async_unload_services(hass) + assert DECONZ_SERVICES not in hass.data async_remove.assert_not_called() @@ -107,14 +120,14 @@ async def test_configure_service_with_field(hass): await setup_deconz_integration(hass) data = { - deconz.services.SERVICE_FIELD: "/light/2", + SERVICE_FIELD: "/light/2", CONF_BRIDGE_ID: BRIDGEID, - deconz.services.SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20}, + SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20}, } with patch("pydeconz.DeconzSession.request", return_value=Mock(True)) as put_state: await hass.services.async_call( - deconz.DOMAIN, deconz.services.SERVICE_CONFIGURE_DEVICE, service_data=data + DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data ) await hass.async_block_till_done() put_state.assert_called_with( @@ -129,13 +142,13 @@ async def test_configure_service_with_entity(hass): gateway.deconz_ids["light.test"] = "/light/1" data = { - deconz.services.SERVICE_ENTITY: "light.test", - deconz.services.SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20}, + SERVICE_ENTITY: "light.test", + SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20}, } with patch("pydeconz.DeconzSession.request", return_value=Mock(True)) as put_state: await hass.services.async_call( - deconz.DOMAIN, deconz.services.SERVICE_CONFIGURE_DEVICE, service_data=data + DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data ) await hass.async_block_till_done() put_state.assert_called_with( @@ -150,14 +163,14 @@ async def test_configure_service_with_entity_and_field(hass): gateway.deconz_ids["light.test"] = "/light/1" data = { - deconz.services.SERVICE_ENTITY: "light.test", - deconz.services.SERVICE_FIELD: "/state", - deconz.services.SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20}, + SERVICE_ENTITY: "light.test", + SERVICE_FIELD: "/state", + SERVICE_DATA: {"on": True, "attr1": 10, "attr2": 20}, } with patch("pydeconz.DeconzSession.request", return_value=Mock(True)) as put_state: await hass.services.async_call( - deconz.DOMAIN, deconz.services.SERVICE_CONFIGURE_DEVICE, service_data=data + DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data ) await hass.async_block_till_done() put_state.assert_called_with( @@ -169,11 +182,11 @@ async def test_configure_service_with_faulty_field(hass): """Test that service invokes pydeconz with the correct path and data.""" await setup_deconz_integration(hass) - data = {deconz.services.SERVICE_FIELD: "light/2", deconz.services.SERVICE_DATA: {}} + data = {SERVICE_FIELD: "light/2", SERVICE_DATA: {}} with pytest.raises(vol.Invalid): await hass.services.async_call( - deconz.DOMAIN, deconz.services.SERVICE_CONFIGURE_DEVICE, service_data=data + DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data ) await hass.async_block_till_done() @@ -183,13 +196,13 @@ async def test_configure_service_with_faulty_entity(hass): await setup_deconz_integration(hass) data = { - deconz.services.SERVICE_ENTITY: "light.nonexisting", - deconz.services.SERVICE_DATA: {}, + SERVICE_ENTITY: "light.nonexisting", + SERVICE_DATA: {}, } with patch("pydeconz.DeconzSession.request", return_value=Mock(True)) as put_state: await hass.services.async_call( - deconz.DOMAIN, deconz.services.SERVICE_CONFIGURE_DEVICE, service_data=data + DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data ) await hass.async_block_till_done() put_state.assert_not_called() @@ -207,7 +220,7 @@ async def test_service_refresh_devices(hass): return_value={"groups": GROUP, "lights": LIGHT, "sensors": SENSOR}, ): await hass.services.async_call( - deconz.DOMAIN, deconz.services.SERVICE_DEVICE_REFRESH, service_data=data + DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH, service_data=data ) await hass.async_block_till_done() @@ -247,7 +260,7 @@ async def test_remove_orphaned_entries_service(hass): entity_registry = await hass.helpers.entity_registry.async_get_registry() entity_registry.async_get_or_create( SENSOR_DOMAIN, - deconz.DOMAIN, + DECONZ_DOMAIN, "12345", suggested_object_id="Orphaned sensor", config_entry=config_entry, @@ -260,8 +273,8 @@ async def test_remove_orphaned_entries_service(hass): ) await hass.services.async_call( - deconz.DOMAIN, - deconz.services.SERVICE_REMOVE_ORPHANED_ENTRIES, + DECONZ_DOMAIN, + SERVICE_REMOVE_ORPHANED_ENTRIES, service_data=data, ) await hass.async_block_till_done() diff --git a/tests/components/deconz/test_switch.py b/tests/components/deconz/test_switch.py index e4d5dcea590..4f23be3d1e3 100644 --- a/tests/components/deconz/test_switch.py +++ b/tests/components/deconz/test_switch.py @@ -1,9 +1,15 @@ """deCONZ switch platform tests.""" + from copy import deepcopy -from homeassistant.components import deconz +from homeassistant.components.deconz import DOMAIN as DECONZ_DOMAIN from homeassistant.components.deconz.gateway import get_gateway_from_config_entry -import homeassistant.components.switch as switch +from homeassistant.components.switch import ( + DOMAIN as SWITCH_DOMAIN, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) +from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -63,11 +69,11 @@ async def test_platform_manually_configured(hass): """Test that we do not discover anything or try to set up a gateway.""" assert ( await async_setup_component( - hass, switch.DOMAIN, {"switch": {"platform": deconz.DOMAIN}} + hass, SWITCH_DOMAIN, {"switch": {"platform": DECONZ_DOMAIN}} ) is True ) - assert deconz.DOMAIN not in hass.data + assert DECONZ_DOMAIN not in hass.data async def test_no_switches(hass): @@ -84,9 +90,9 @@ async def test_power_plugs(hass): gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 4 - assert hass.states.get("switch.on_off_switch").state == "on" - assert hass.states.get("switch.smart_plug").state == "off" - assert hass.states.get("switch.on_off_relay").state == "on" + assert hass.states.get("switch.on_off_switch").state == STATE_ON + assert hass.states.get("switch.smart_plug").state == STATE_OFF + assert hass.states.get("switch.on_off_relay").state == STATE_ON assert hass.states.get("switch.unsupported_switch") is None state_changed_event = { @@ -98,7 +104,7 @@ async def test_power_plugs(hass): } gateway.api.event_handler(state_changed_event) - assert hass.states.get("switch.on_off_switch").state == "off" + assert hass.states.get("switch.on_off_switch").state == STATE_OFF # Verify service calls @@ -110,9 +116,9 @@ async def test_power_plugs(hass): on_off_switch_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - switch.DOMAIN, - switch.SERVICE_TURN_ON, - {"entity_id": "switch.on_off_switch"}, + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "switch.on_off_switch"}, blocking=True, ) await hass.async_block_till_done() @@ -124,9 +130,9 @@ async def test_power_plugs(hass): on_off_switch_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - switch.DOMAIN, - switch.SERVICE_TURN_OFF, - {"entity_id": "switch.on_off_switch"}, + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "switch.on_off_switch"}, blocking=True, ) await hass.async_block_till_done() @@ -145,7 +151,7 @@ async def test_sirens(hass): gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 2 - assert hass.states.get("switch.warning_device").state == "on" + assert hass.states.get("switch.warning_device").state == STATE_ON assert hass.states.get("switch.unsupported_switch") is None state_changed_event = { @@ -157,7 +163,7 @@ async def test_sirens(hass): } gateway.api.event_handler(state_changed_event) - assert hass.states.get("switch.warning_device").state == "off" + assert hass.states.get("switch.warning_device").state == STATE_OFF # Verify service calls @@ -169,9 +175,9 @@ async def test_sirens(hass): warning_device_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - switch.DOMAIN, - switch.SERVICE_TURN_ON, - {"entity_id": "switch.warning_device"}, + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "switch.warning_device"}, blocking=True, ) await hass.async_block_till_done() @@ -185,9 +191,9 @@ async def test_sirens(hass): warning_device_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( - switch.DOMAIN, - switch.SERVICE_TURN_OFF, - {"entity_id": "switch.warning_device"}, + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "switch.warning_device"}, blocking=True, ) await hass.async_block_till_done()