mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Use websocket fixture in deCONZ binary sensor tests (#47820)
Localize test data Improve asserts
This commit is contained in:
parent
5cdd945f44
commit
333f5da036
@ -1,5 +1,6 @@
|
|||||||
"""deCONZ binary sensor platform tests."""
|
"""deCONZ binary sensor platform tests."""
|
||||||
from copy import deepcopy
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
DEVICE_CLASS_MOTION,
|
DEVICE_CLASS_MOTION,
|
||||||
@ -11,7 +12,6 @@ from homeassistant.components.deconz.const import (
|
|||||||
CONF_MASTER_GATEWAY,
|
CONF_MASTER_GATEWAY,
|
||||||
DOMAIN as DECONZ_DOMAIN,
|
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.components.deconz.services import SERVICE_DEVICE_REFRESH
|
||||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
@ -23,9 +23,18 @@ from .test_gateway import (
|
|||||||
setup_deconz_integration,
|
setup_deconz_integration,
|
||||||
)
|
)
|
||||||
|
|
||||||
SENSORS = {
|
|
||||||
|
async def test_no_binary_sensors(hass, aioclient_mock):
|
||||||
|
"""Test that no sensors in deconz results in no sensor entities."""
|
||||||
|
await setup_deconz_integration(hass, aioclient_mock)
|
||||||
|
assert len(hass.states.async_all()) == 0
|
||||||
|
|
||||||
|
|
||||||
|
async def test_binary_sensors(hass, aioclient_mock, mock_deconz_websocket):
|
||||||
|
"""Test successful creation of binary sensor entities."""
|
||||||
|
data = {
|
||||||
|
"sensors": {
|
||||||
"1": {
|
"1": {
|
||||||
"id": "Presence sensor id",
|
|
||||||
"name": "Presence sensor",
|
"name": "Presence sensor",
|
||||||
"type": "ZHAPresence",
|
"type": "ZHAPresence",
|
||||||
"state": {"dark": False, "presence": False},
|
"state": {"dark": False, "presence": False},
|
||||||
@ -33,7 +42,6 @@ SENSORS = {
|
|||||||
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
||||||
},
|
},
|
||||||
"2": {
|
"2": {
|
||||||
"id": "Temperature sensor id",
|
|
||||||
"name": "Temperature sensor",
|
"name": "Temperature sensor",
|
||||||
"type": "ZHATemperature",
|
"type": "ZHATemperature",
|
||||||
"state": {"temperature": False},
|
"state": {"temperature": False},
|
||||||
@ -41,7 +49,6 @@ SENSORS = {
|
|||||||
"uniqueid": "00:00:00:00:00:00:00:01-00",
|
"uniqueid": "00:00:00:00:00:00:00:01-00",
|
||||||
},
|
},
|
||||||
"3": {
|
"3": {
|
||||||
"id": "CLIP presence sensor id",
|
|
||||||
"name": "CLIP presence sensor",
|
"name": "CLIP presence sensor",
|
||||||
"type": "CLIPPresence",
|
"type": "CLIPPresence",
|
||||||
"state": {"presence": False},
|
"state": {"presence": False},
|
||||||
@ -49,7 +56,6 @@ SENSORS = {
|
|||||||
"uniqueid": "00:00:00:00:00:00:00:02-00",
|
"uniqueid": "00:00:00:00:00:00:00:02-00",
|
||||||
},
|
},
|
||||||
"4": {
|
"4": {
|
||||||
"id": "Vibration sensor id",
|
|
||||||
"name": "Vibration sensor",
|
"name": "Vibration sensor",
|
||||||
"type": "ZHAVibration",
|
"type": "ZHAVibration",
|
||||||
"state": {
|
"state": {
|
||||||
@ -62,22 +68,9 @@ SENSORS = {
|
|||||||
"uniqueid": "00:00:00:00:00:00:00:03-00",
|
"uniqueid": "00:00:00:00:00:00:00:03-00",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||||
async def test_no_binary_sensors(hass, aioclient_mock):
|
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
||||||
"""Test that no sensors in deconz results in no sensor entities."""
|
|
||||||
await setup_deconz_integration(hass, aioclient_mock)
|
|
||||||
assert len(hass.states.async_all()) == 0
|
|
||||||
|
|
||||||
|
|
||||||
async def test_binary_sensors(hass, aioclient_mock):
|
|
||||||
"""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)
|
|
||||||
|
|
||||||
assert len(hass.states.async_all()) == 3
|
assert len(hass.states.async_all()) == 3
|
||||||
presence_sensor = hass.states.get("binary_sensor.presence_sensor")
|
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.state == STATE_ON
|
||||||
assert vibration_sensor.attributes["device_class"] == DEVICE_CLASS_VIBRATION
|
assert vibration_sensor.attributes["device_class"] == DEVICE_CLASS_VIBRATION
|
||||||
|
|
||||||
state_changed_event = {
|
event_changed_sensor = {
|
||||||
"t": "event",
|
"t": "event",
|
||||||
"e": "changed",
|
"e": "changed",
|
||||||
"r": "sensors",
|
"r": "sensors",
|
||||||
"id": "1",
|
"id": "1",
|
||||||
"state": {"presence": True},
|
"state": {"presence": True},
|
||||||
}
|
}
|
||||||
gateway.api.event_handler(state_changed_event)
|
await mock_deconz_websocket(data=event_changed_sensor)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert hass.states.get("binary_sensor.presence_sensor").state == STATE_ON
|
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.config_entries.async_remove(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert len(hass.states.async_all()) == 0
|
assert len(hass.states.async_all()) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_allow_clip_sensor(hass, aioclient_mock):
|
async def test_allow_clip_sensor(hass, aioclient_mock):
|
||||||
"""Test that CLIP sensors can be allowed."""
|
"""Test that CLIP sensors can be allowed."""
|
||||||
data = deepcopy(DECONZ_WEB_REQUEST)
|
data = {
|
||||||
data["sensors"] = deepcopy(SENSORS)
|
"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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
with patch.dict(DECONZ_WEB_REQUEST, data):
|
||||||
config_entry = await setup_deconz_integration(
|
config_entry = await setup_deconz_integration(
|
||||||
hass,
|
hass, aioclient_mock, options={CONF_ALLOW_CLIP_SENSOR: True}
|
||||||
aioclient_mock,
|
|
||||||
options={CONF_ALLOW_CLIP_SENSOR: True},
|
|
||||||
get_state_response=data,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(hass.states.async_all()) == 4
|
assert len(hass.states.async_all()) == 2
|
||||||
assert hass.states.get("binary_sensor.presence_sensor").state == 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 == STATE_OFF
|
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
|
# Disallow clip sensors
|
||||||
|
|
||||||
@ -134,8 +141,8 @@ async def test_allow_clip_sensor(hass, aioclient_mock):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert len(hass.states.async_all()) == 3
|
assert len(hass.states.async_all()) == 1
|
||||||
assert hass.states.get("binary_sensor.clip_presence_sensor") is None
|
assert not hass.states.get("binary_sensor.clip_presence_sensor")
|
||||||
|
|
||||||
# Allow clip sensors
|
# Allow clip sensors
|
||||||
|
|
||||||
@ -144,48 +151,65 @@ async def test_allow_clip_sensor(hass, aioclient_mock):
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
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
|
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."""
|
"""Test that adding a new binary sensor works."""
|
||||||
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
event_added_sensor = {
|
||||||
gateway = get_gateway_from_config_entry(hass, config_entry)
|
|
||||||
assert len(hass.states.async_all()) == 0
|
|
||||||
|
|
||||||
state_added_event = {
|
|
||||||
"t": "event",
|
"t": "event",
|
||||||
"e": "added",
|
"e": "added",
|
||||||
"r": "sensors",
|
"r": "sensors",
|
||||||
"id": "1",
|
"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()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert len(hass.states.async_all()) == 1
|
assert len(hass.states.async_all()) == 1
|
||||||
assert hass.states.get("binary_sensor.presence_sensor").state == STATE_OFF
|
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."""
|
"""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(
|
config_entry = await setup_deconz_integration(
|
||||||
hass,
|
hass,
|
||||||
aioclient_mock,
|
aioclient_mock,
|
||||||
options={CONF_MASTER_GATEWAY: True, CONF_ALLOW_NEW_DEVICES: False},
|
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
|
assert len(hass.states.async_all()) == 0
|
||||||
|
|
||||||
state_added_event = {
|
await mock_deconz_websocket(data=event_added_sensor)
|
||||||
"t": "event",
|
|
||||||
"e": "added",
|
|
||||||
"r": "sensors",
|
|
||||||
"id": "1",
|
|
||||||
"sensor": deepcopy(SENSORS["1"]),
|
|
||||||
}
|
|
||||||
gateway.api.event_handler(state_added_event)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert len(hass.states.async_all()) == 0
|
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()
|
aioclient_mock.clear_requests()
|
||||||
data = {
|
data = {"groups": {}, "lights": {}, "sensors": {"1": sensor}}
|
||||||
"groups": {},
|
|
||||||
"lights": {},
|
|
||||||
"sensors": {"1": deepcopy(SENSORS["1"])},
|
|
||||||
}
|
|
||||||
mock_deconz_request(aioclient_mock, config_entry.data, data)
|
mock_deconz_request(aioclient_mock, config_entry.data, data)
|
||||||
|
|
||||||
await hass.services.async_call(DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH)
|
await hass.services.async_call(DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user