diff --git a/tests/components/deconz/test_binary_sensor.py b/tests/components/deconz/test_binary_sensor.py index 39809ecc231..9d4c86ead6c 100644 --- a/tests/components/deconz/test_binary_sensor.py +++ b/tests/components/deconz/test_binary_sensor.py @@ -1,5 +1,6 @@ """deCONZ binary sensor platform tests.""" -from copy import deepcopy + +from unittest.mock import patch from homeassistant.components.binary_sensor import ( DEVICE_CLASS_MOTION, @@ -11,7 +12,6 @@ from homeassistant.components.deconz.const import ( CONF_MASTER_GATEWAY, DOMAIN as DECONZ_DOMAIN, ) -from homeassistant.components.deconz.gateway import get_gateway_from_config_entry from homeassistant.components.deconz.services import SERVICE_DEVICE_REFRESH from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.helpers import entity_registry as er @@ -23,46 +23,6 @@ from .test_gateway import ( setup_deconz_integration, ) -SENSORS = { - "1": { - "id": "Presence sensor id", - "name": "Presence sensor", - "type": "ZHAPresence", - "state": {"dark": False, "presence": False}, - "config": {"on": True, "reachable": True, "temperature": 10}, - "uniqueid": "00:00:00:00:00:00:00:00-00", - }, - "2": { - "id": "Temperature sensor id", - "name": "Temperature sensor", - "type": "ZHATemperature", - "state": {"temperature": False}, - "config": {}, - "uniqueid": "00:00:00:00:00:00:00:01-00", - }, - "3": { - "id": "CLIP presence sensor id", - "name": "CLIP presence sensor", - "type": "CLIPPresence", - "state": {"presence": False}, - "config": {}, - "uniqueid": "00:00:00:00:00:00:00:02-00", - }, - "4": { - "id": "Vibration sensor id", - "name": "Vibration sensor", - "type": "ZHAVibration", - "state": { - "orientation": [1, 2, 3], - "tiltangle": 36, - "vibration": True, - "vibrationstrength": 10, - }, - "config": {"on": True, "reachable": True, "temperature": 10}, - "uniqueid": "00:00:00:00:00:00:00:03-00", - }, -} - async def test_no_binary_sensors(hass, aioclient_mock): """Test that no sensors in deconz results in no sensor entities.""" @@ -70,14 +30,47 @@ async def test_no_binary_sensors(hass, aioclient_mock): assert len(hass.states.async_all()) == 0 -async def test_binary_sensors(hass, aioclient_mock): +async def test_binary_sensors(hass, aioclient_mock, mock_deconz_websocket): """Test successful creation of binary sensor entities.""" - data = deepcopy(DECONZ_WEB_REQUEST) - data["sensors"] = deepcopy(SENSORS) - config_entry = await setup_deconz_integration( - hass, aioclient_mock, get_state_response=data - ) - gateway = get_gateway_from_config_entry(hass, config_entry) + data = { + "sensors": { + "1": { + "name": "Presence sensor", + "type": "ZHAPresence", + "state": {"dark": False, "presence": False}, + "config": {"on": True, "reachable": True, "temperature": 10}, + "uniqueid": "00:00:00:00:00:00:00:00-00", + }, + "2": { + "name": "Temperature sensor", + "type": "ZHATemperature", + "state": {"temperature": False}, + "config": {}, + "uniqueid": "00:00:00:00:00:00:00:01-00", + }, + "3": { + "name": "CLIP presence sensor", + "type": "CLIPPresence", + "state": {"presence": False}, + "config": {}, + "uniqueid": "00:00:00:00:00:00:00:02-00", + }, + "4": { + "name": "Vibration sensor", + "type": "ZHAVibration", + "state": { + "orientation": [1, 2, 3], + "tiltangle": 36, + "vibration": True, + "vibrationstrength": 10, + }, + "config": {"on": True, "reachable": True, "temperature": 10}, + "uniqueid": "00:00:00:00:00:00:00:03-00", + }, + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) assert len(hass.states.async_all()) == 3 presence_sensor = hass.states.get("binary_sensor.presence_sensor") @@ -89,14 +82,14 @@ async def test_binary_sensors(hass, aioclient_mock): assert vibration_sensor.state == STATE_ON assert vibration_sensor.attributes["device_class"] == DEVICE_CLASS_VIBRATION - state_changed_event = { + event_changed_sensor = { "t": "event", "e": "changed", "r": "sensors", "id": "1", "state": {"presence": True}, } - gateway.api.event_handler(state_changed_event) + await mock_deconz_websocket(data=event_changed_sensor) await hass.async_block_till_done() assert hass.states.get("binary_sensor.presence_sensor").state == STATE_ON @@ -107,25 +100,39 @@ async def test_binary_sensors(hass, aioclient_mock): await hass.config_entries.async_remove(config_entry.entry_id) await hass.async_block_till_done() + assert len(hass.states.async_all()) == 0 async def test_allow_clip_sensor(hass, aioclient_mock): """Test that CLIP sensors can be allowed.""" - data = deepcopy(DECONZ_WEB_REQUEST) - data["sensors"] = deepcopy(SENSORS) - config_entry = await setup_deconz_integration( - hass, - aioclient_mock, - options={CONF_ALLOW_CLIP_SENSOR: True}, - get_state_response=data, - ) + data = { + "sensors": { + "1": { + "name": "Presence sensor", + "type": "ZHAPresence", + "state": {"presence": False}, + "config": {"on": True, "reachable": True}, + "uniqueid": "00:00:00:00:00:00:00:00-00", + }, + "2": { + "name": "CLIP presence sensor", + "type": "CLIPPresence", + "state": {"presence": False}, + "config": {}, + "uniqueid": "00:00:00:00:00:00:00:02-00", + }, + } + } - assert len(hass.states.async_all()) == 4 + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration( + hass, aioclient_mock, options={CONF_ALLOW_CLIP_SENSOR: True} + ) + + assert len(hass.states.async_all()) == 2 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 == STATE_OFF - assert hass.states.get("binary_sensor.vibration_sensor").state == STATE_ON # Disallow clip sensors @@ -134,8 +141,8 @@ async def test_allow_clip_sensor(hass, aioclient_mock): ) await hass.async_block_till_done() - assert len(hass.states.async_all()) == 3 - assert hass.states.get("binary_sensor.clip_presence_sensor") is None + assert len(hass.states.async_all()) == 1 + assert not hass.states.get("binary_sensor.clip_presence_sensor") # Allow clip sensors @@ -144,48 +151,65 @@ async def test_allow_clip_sensor(hass, aioclient_mock): ) await hass.async_block_till_done() - assert len(hass.states.async_all()) == 4 + assert len(hass.states.async_all()) == 2 assert hass.states.get("binary_sensor.clip_presence_sensor").state == STATE_OFF -async def test_add_new_binary_sensor(hass, aioclient_mock): +async def test_add_new_binary_sensor(hass, aioclient_mock, mock_deconz_websocket): """Test that adding a new binary sensor works.""" - config_entry = await setup_deconz_integration(hass, aioclient_mock) - gateway = get_gateway_from_config_entry(hass, config_entry) - assert len(hass.states.async_all()) == 0 - - state_added_event = { + event_added_sensor = { "t": "event", "e": "added", "r": "sensors", "id": "1", - "sensor": deepcopy(SENSORS["1"]), + "sensor": { + "id": "Presence sensor id", + "name": "Presence sensor", + "type": "ZHAPresence", + "state": {"presence": False}, + "config": {"on": True, "reachable": True}, + "uniqueid": "00:00:00:00:00:00:00:00-00", + }, } - gateway.api.event_handler(state_added_event) + + await setup_deconz_integration(hass, aioclient_mock) + assert len(hass.states.async_all()) == 0 + + await mock_deconz_websocket(data=event_added_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 assert hass.states.get("binary_sensor.presence_sensor").state == STATE_OFF -async def test_add_new_binary_sensor_ignored(hass, aioclient_mock): +async def test_add_new_binary_sensor_ignored( + hass, aioclient_mock, mock_deconz_websocket +): """Test that adding a new binary sensor is not allowed.""" + sensor = { + "name": "Presence sensor", + "type": "ZHAPresence", + "state": {"presence": False}, + "config": {"on": True, "reachable": True}, + "uniqueid": "00:00:00:00:00:00:00:00-00", + } + event_added_sensor = { + "t": "event", + "e": "added", + "r": "sensors", + "id": "1", + "sensor": sensor, + } + config_entry = await setup_deconz_integration( hass, aioclient_mock, options={CONF_MASTER_GATEWAY: True, CONF_ALLOW_NEW_DEVICES: False}, ) - gateway = get_gateway_from_config_entry(hass, config_entry) + assert len(hass.states.async_all()) == 0 - state_added_event = { - "t": "event", - "e": "added", - "r": "sensors", - "id": "1", - "sensor": deepcopy(SENSORS["1"]), - } - gateway.api.event_handler(state_added_event) + await mock_deconz_websocket(data=event_added_sensor) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 @@ -197,11 +221,7 @@ async def test_add_new_binary_sensor_ignored(hass, aioclient_mock): ) aioclient_mock.clear_requests() - data = { - "groups": {}, - "lights": {}, - "sensors": {"1": deepcopy(SENSORS["1"])}, - } + data = {"groups": {}, "lights": {}, "sensors": {"1": sensor}} mock_deconz_request(aioclient_mock, config_entry.data, data) await hass.services.async_call(DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH)