Change deCONZ to embedded platforms (#20113)
Move all platforms into components/deconz
This commit is contained in:
1 parent
336b6adc88
commit
a3f0d55737
12 files changed
+19
-14
No files matched your search
@@ -0,0 +1,134 @@
|
||||
"""deCONZ binary sensor platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.binary_sensor as binary_sensor
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
SENSOR = {
|
||||
"1": {
|
||||
"id": "Sensor 1 id",
|
||||
"name": "Sensor 1 name",
|
||||
"type": "ZHAPresence",
|
||||
"state": {"presence": False},
|
||||
"config": {},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Sensor 2 id",
|
||||
"name": "Sensor 2 name",
|
||||
"type": "ZHATemperature",
|
||||
"state": {"temperature": False},
|
||||
"config": {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data, allow_clip_sensor=True):
|
||||
"""Load the deCONZ binary sensor platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
|
||||
ENTRY_CONFIG[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, 'binary_sensor')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_binary_sensors(hass):
|
||||
"""Test that no sensors in deconz results in no sensor entities."""
|
||||
data = {}
|
||||
await setup_gateway(hass, data)
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_binary_sensors(hass):
|
||||
"""Test successful creation of binary sensor entities."""
|
||||
data = {"sensors": SENSOR}
|
||||
await setup_gateway(hass, data)
|
||||
assert "binary_sensor.sensor_1_name" in \
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "binary_sensor.sensor_2_name" not in \
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
hass.data[deconz.DOMAIN].api.sensors['1'].async_update(
|
||||
{'state': {'on': False}})
|
||||
|
||||
|
||||
async def test_add_new_sensor(hass):
|
||||
"""Test successful creation of sensor entities."""
|
||||
data = {}
|
||||
await setup_gateway(hass, data)
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'ZHAPresence'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert "binary_sensor.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_do_not_allow_clip_sensor(hass):
|
||||
"""Test that clip sensors can be ignored."""
|
||||
data = {}
|
||||
await setup_gateway(hass, data, allow_clip_sensor=False)
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'CLIPPresence'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
|
||||
|
||||
async def test_unload_switch(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
data = {"sensors": SENSOR}
|
||||
await setup_gateway(hass, data)
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
@@ -0,0 +1,147 @@
|
||||
"""deCONZ cover platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.components.deconz.const import COVER_TYPES
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.cover as cover
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
SUPPORTED_COVERS = {
|
||||
"1": {
|
||||
"id": "Cover 1 id",
|
||||
"name": "Cover 1 name",
|
||||
"type": "Level controllable output",
|
||||
"state": {"bri": 255, "reachable": True},
|
||||
"modelid": "Not zigbee spec",
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Cover 2 id",
|
||||
"name": "Cover 2 name",
|
||||
"type": "Window covering device",
|
||||
"state": {"bri": 255, "reachable": True},
|
||||
"modelid": "lumi.curtain"
|
||||
}
|
||||
}
|
||||
|
||||
UNSUPPORTED_COVER = {
|
||||
"1": {
|
||||
"id": "Cover id",
|
||||
"name": "Unsupported switch",
|
||||
"type": "Not a cover",
|
||||
"state": {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data):
|
||||
"""Load the deCONZ cover platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'cover')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_covers(hass):
|
||||
"""Test that no cover entities are created."""
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_cover(hass):
|
||||
"""Test that all supported cover entities are created."""
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_COVERS})
|
||||
assert "cover.cover_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(SUPPORTED_COVERS) == len(COVER_TYPES)
|
||||
assert len(hass.states.async_all()) == 3
|
||||
|
||||
cover_1 = hass.states.get('cover.cover_1_name')
|
||||
assert cover_1 is not None
|
||||
assert cover_1.state == 'closed'
|
||||
|
||||
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
|
||||
|
||||
await hass.services.async_call('cover', 'open_cover', {
|
||||
'entity_id': 'cover.cover_1_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('cover', 'close_cover', {
|
||||
'entity_id': 'cover.cover_1_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('cover', 'stop_cover', {
|
||||
'entity_id': 'cover.cover_1_name'
|
||||
}, blocking=True)
|
||||
|
||||
await hass.services.async_call('cover', 'close_cover', {
|
||||
'entity_id': 'cover.cover_2_name'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_add_new_cover(hass):
|
||||
"""Test successful creation of cover entity."""
|
||||
data = {}
|
||||
await setup_gateway(hass, data)
|
||||
cover = Mock()
|
||||
cover.name = 'name'
|
||||
cover.type = "Level controllable output"
|
||||
cover.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_light', [cover])
|
||||
await hass.async_block_till_done()
|
||||
assert "cover.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_unsupported_cover(hass):
|
||||
"""Test that unsupported covers are not created."""
|
||||
await setup_gateway(hass, {"lights": UNSUPPORTED_COVER})
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_unload_cover(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_COVERS})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
@@ -0,0 +1,213 @@
|
||||
"""deCONZ light platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.light as light
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
LIGHT = {
|
||||
"1": {
|
||||
"id": "Light 1 id",
|
||||
"name": "Light 1 name",
|
||||
"state": {
|
||||
"on": True, "bri": 255, "colormode": "xy", "xy": (500, 500),
|
||||
"reachable": True
|
||||
},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Light 2 id",
|
||||
"name": "Light 2 name",
|
||||
"state": {
|
||||
"on": True, "colormode": "ct", "ct": 2500, "reachable": True
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GROUP = {
|
||||
"1": {
|
||||
"id": "Group 1 id",
|
||||
"name": "Group 1 name",
|
||||
"type": "LightGroup",
|
||||
"state": {},
|
||||
"action": {},
|
||||
"scenes": [],
|
||||
"lights": [
|
||||
"1",
|
||||
"2"
|
||||
]
|
||||
},
|
||||
"2": {
|
||||
"id": "Group 2 id",
|
||||
"name": "Group 2 name",
|
||||
"state": {},
|
||||
"action": {},
|
||||
"scenes": []
|
||||
},
|
||||
}
|
||||
|
||||
SWITCH = {
|
||||
"1": {
|
||||
"id": "Switch 1 id",
|
||||
"name": "Switch 1 name",
|
||||
"type": "On/Off plug-in unit",
|
||||
"state": {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data, allow_deconz_groups=True):
|
||||
"""Load the deCONZ light platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
|
||||
ENTRY_CONFIG[deconz.const.CONF_ALLOW_DECONZ_GROUPS] = allow_deconz_groups
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'light')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_lights_or_groups(hass):
|
||||
"""Test that no lights or groups entities are created."""
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_lights_and_groups(hass):
|
||||
"""Test that lights or groups entities are created."""
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"lights": LIGHT, "groups": GROUP})
|
||||
assert "light.light_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "light.light_2_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "light.group_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "light.group_2_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 4
|
||||
|
||||
lamp_1 = hass.states.get('light.light_1_name')
|
||||
assert lamp_1 is not None
|
||||
assert lamp_1.state == 'on'
|
||||
assert lamp_1.attributes['brightness'] == 255
|
||||
assert lamp_1.attributes['hs_color'] == (224.235, 100.0)
|
||||
|
||||
light_2 = hass.states.get('light.light_2_name')
|
||||
assert light_2 is not None
|
||||
assert light_2.state == 'on'
|
||||
assert light_2.attributes['color_temp'] == 2500
|
||||
|
||||
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
|
||||
|
||||
await hass.services.async_call('light', 'turn_on', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'color_temp': 2500,
|
||||
'brightness': 200,
|
||||
'transition': 5,
|
||||
'flash': 'short',
|
||||
'effect': 'colorloop'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('light', 'turn_on', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'hs_color': (20, 30),
|
||||
'flash': 'long',
|
||||
'effect': 'None'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('light', 'turn_off', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'transition': 5,
|
||||
'flash': 'short'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('light', 'turn_off', {
|
||||
'entity_id': 'light.light_1_name',
|
||||
'flash': 'long'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_add_new_light(hass):
|
||||
"""Test successful creation of light entities."""
|
||||
await setup_gateway(hass, {})
|
||||
light = Mock()
|
||||
light.name = 'name'
|
||||
light.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_light', [light])
|
||||
await hass.async_block_till_done()
|
||||
assert "light.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_add_new_group(hass):
|
||||
"""Test successful creation of group entities."""
|
||||
await setup_gateway(hass, {})
|
||||
group = Mock()
|
||||
group.name = 'name'
|
||||
group.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_group', [group])
|
||||
await hass.async_block_till_done()
|
||||
assert "light.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_do_not_add_deconz_groups(hass):
|
||||
"""Test that clip sensors can be ignored."""
|
||||
await setup_gateway(hass, {}, allow_deconz_groups=False)
|
||||
group = Mock()
|
||||
group.name = 'name'
|
||||
group.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_group', [group])
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
|
||||
|
||||
async def test_no_switch(hass):
|
||||
"""Test that a switch doesn't get created as a light entity."""
|
||||
await setup_gateway(hass, {"lights": SWITCH})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_unload_light(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
await setup_gateway(hass, {"lights": LIGHT, "groups": GROUP})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
# Group.all_lights will not be removed
|
||||
assert len(hass.states.async_all()) == 1
|
||||
@@ -0,0 +1,96 @@
|
||||
"""deCONZ scene platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.scene as scene
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
GROUP = {
|
||||
"1": {
|
||||
"id": "Group 1 id",
|
||||
"name": "Group 1 name",
|
||||
"state": {},
|
||||
"action": {},
|
||||
"scenes": [{
|
||||
"id": "1",
|
||||
"name": "Scene 1"
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data):
|
||||
"""Load the deCONZ scene platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'scene')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_scenes(hass):
|
||||
"""Test that scenes can be loaded without scenes being available."""
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_scenes(hass):
|
||||
"""Test that scenes works."""
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"groups": GROUP})
|
||||
assert "scene.group_1_name_scene_1" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
await hass.services.async_call('scene', 'turn_on', {
|
||||
'entity_id': 'scene.group_1_name_scene_1'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_unload_scene(hass):
|
||||
"""Test that it works to unload scene entities."""
|
||||
await setup_gateway(hass, {"groups": GROUP})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
@@ -0,0 +1,172 @@
|
||||
"""deCONZ sensor platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.sensor as sensor
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
SENSOR = {
|
||||
"1": {
|
||||
"id": "Sensor 1 id",
|
||||
"name": "Sensor 1 name",
|
||||
"type": "ZHALightLevel",
|
||||
"state": {"lightlevel": 30000, "dark": False},
|
||||
"config": {"reachable": True},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Sensor 2 id",
|
||||
"name": "Sensor 2 name",
|
||||
"type": "ZHAPresence",
|
||||
"state": {"presence": False},
|
||||
"config": {}
|
||||
},
|
||||
"3": {
|
||||
"id": "Sensor 3 id",
|
||||
"name": "Sensor 3 name",
|
||||
"type": "ZHASwitch",
|
||||
"state": {"buttonevent": 1000},
|
||||
"config": {}
|
||||
},
|
||||
"4": {
|
||||
"id": "Sensor 4 id",
|
||||
"name": "Sensor 4 name",
|
||||
"type": "ZHASwitch",
|
||||
"state": {"buttonevent": 1000},
|
||||
"config": {"battery": 100},
|
||||
"uniqueid": "00:00:00:00:00:00:00:01-00"
|
||||
},
|
||||
"5": {
|
||||
"id": "Sensor 5 id",
|
||||
"name": "Sensor 5 name",
|
||||
"type": "ZHASwitch",
|
||||
"state": {"buttonevent": 1000},
|
||||
"config": {"battery": 100},
|
||||
"uniqueid": "00:00:00:00:00:00:00:02:00-00"
|
||||
},
|
||||
"6": {
|
||||
"id": "Sensor 6 id",
|
||||
"name": "Sensor 6 name",
|
||||
"type": "Daylight",
|
||||
"state": {"daylight": True},
|
||||
"config": {}
|
||||
},
|
||||
"7": {
|
||||
"id": "Sensor 7 id",
|
||||
"name": "Sensor 7 name",
|
||||
"type": "ZHAPower",
|
||||
"state": {"current": 2, "power": 6, "voltage": 3},
|
||||
"config": {"reachable": True}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data, allow_clip_sensor=True):
|
||||
"""Load the deCONZ sensor platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
|
||||
ENTRY_CONFIG[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, 'sensor')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_sensors(hass):
|
||||
"""Test that no sensors in deconz results in no sensor entities."""
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_sensors(hass):
|
||||
"""Test successful creation of sensor entities."""
|
||||
await setup_gateway(hass, {"sensors": SENSOR})
|
||||
assert "sensor.sensor_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_2_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_3_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_3_name_battery_level" not in \
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_4_name" not in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "sensor.sensor_4_name_battery_level" in \
|
||||
hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(hass.states.async_all()) == 5
|
||||
|
||||
hass.data[deconz.DOMAIN].api.sensors['1'].async_update(
|
||||
{'state': {'on': False}})
|
||||
hass.data[deconz.DOMAIN].api.sensors['4'].async_update(
|
||||
{'config': {'battery': 75}})
|
||||
|
||||
|
||||
async def test_add_new_sensor(hass):
|
||||
"""Test successful creation of sensor entities."""
|
||||
await setup_gateway(hass, {})
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'ZHATemperature'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert "sensor.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_do_not_allow_clipsensor(hass):
|
||||
"""Test that clip sensors can be ignored."""
|
||||
await setup_gateway(hass, {}, allow_clip_sensor=False)
|
||||
sensor = Mock()
|
||||
sensor.name = 'name'
|
||||
sensor.type = 'CLIPTemperature'
|
||||
sensor.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_sensor', [sensor])
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
|
||||
|
||||
async def test_unload_sensor(hass):
|
||||
"""Test that it works to unload sensor entities."""
|
||||
await setup_gateway(hass, {"sensors": SENSOR})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 0
|
||||
@@ -0,0 +1,155 @@
|
||||
"""deCONZ switch platform tests."""
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.components.deconz.const import SWITCH_TYPES
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
import homeassistant.components.switch as switch
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
SUPPORTED_SWITCHES = {
|
||||
"1": {
|
||||
"id": "Switch 1 id",
|
||||
"name": "Switch 1 name",
|
||||
"type": "On/Off plug-in unit",
|
||||
"state": {"on": True, "reachable": True},
|
||||
"uniqueid": "00:00:00:00:00:00:00:00-00"
|
||||
},
|
||||
"2": {
|
||||
"id": "Switch 2 id",
|
||||
"name": "Switch 2 name",
|
||||
"type": "Smart plug",
|
||||
"state": {"on": True, "reachable": True}
|
||||
},
|
||||
"3": {
|
||||
"id": "Switch 3 id",
|
||||
"name": "Switch 3 name",
|
||||
"type": "Warning device",
|
||||
"state": {"alert": "lselect", "reachable": True}
|
||||
}
|
||||
}
|
||||
|
||||
UNSUPPORTED_SWITCH = {
|
||||
"1": {
|
||||
"id": "Switch id",
|
||||
"name": "Unsupported switch",
|
||||
"type": "Not a smart plug",
|
||||
"state": {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ENTRY_CONFIG = {
|
||||
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
||||
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
||||
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
||||
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
||||
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
||||
deconz.config_flow.CONF_PORT: 80
|
||||
}
|
||||
|
||||
|
||||
async def setup_gateway(hass, data):
|
||||
"""Load the deCONZ switch platform."""
|
||||
from pydeconz import DeconzSession
|
||||
loop = Mock()
|
||||
session = Mock()
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
|
||||
config_entries.CONN_CLASS_LOCAL_PUSH)
|
||||
gateway = deconz.DeconzGateway(hass, config_entry)
|
||||
gateway.api = DeconzSession(loop, session, **config_entry.data)
|
||||
gateway.api.config = Mock()
|
||||
hass.data[deconz.DOMAIN] = gateway
|
||||
|
||||
with patch('pydeconz.DeconzSession.async_get_state',
|
||||
return_value=mock_coro(data)):
|
||||
await gateway.api.async_load_parameters()
|
||||
|
||||
await hass.config_entries.async_forward_entry_setup(config_entry, 'switch')
|
||||
# To flush out the service call to update the group
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}) is True
|
||||
assert deconz.DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_switches(hass):
|
||||
"""Test that no switch entities are created."""
|
||||
await setup_gateway(hass, {})
|
||||
assert len(hass.data[deconz.DOMAIN].deconz_ids) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_switches(hass):
|
||||
"""Test that all supported switch entities are created."""
|
||||
with patch('pydeconz.DeconzSession.async_put_state',
|
||||
return_value=mock_coro(True)):
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
|
||||
assert "switch.switch_1_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "switch.switch_2_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert "switch.switch_3_name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
assert len(SUPPORTED_SWITCHES) == len(SWITCH_TYPES)
|
||||
assert len(hass.states.async_all()) == 4
|
||||
|
||||
switch_1 = hass.states.get('switch.switch_1_name')
|
||||
assert switch_1 is not None
|
||||
assert switch_1.state == 'on'
|
||||
switch_3 = hass.states.get('switch.switch_3_name')
|
||||
assert switch_3 is not None
|
||||
assert switch_3.state == 'on'
|
||||
|
||||
hass.data[deconz.DOMAIN].api.lights['1'].async_update({})
|
||||
|
||||
await hass.services.async_call('switch', 'turn_on', {
|
||||
'entity_id': 'switch.switch_1_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('switch', 'turn_off', {
|
||||
'entity_id': 'switch.switch_1_name'
|
||||
}, blocking=True)
|
||||
|
||||
await hass.services.async_call('switch', 'turn_on', {
|
||||
'entity_id': 'switch.switch_3_name'
|
||||
}, blocking=True)
|
||||
await hass.services.async_call('switch', 'turn_off', {
|
||||
'entity_id': 'switch.switch_3_name'
|
||||
}, blocking=True)
|
||||
|
||||
|
||||
async def test_add_new_switch(hass):
|
||||
"""Test successful creation of switch entity."""
|
||||
await setup_gateway(hass, {})
|
||||
switch = Mock()
|
||||
switch.name = 'name'
|
||||
switch.type = "Smart plug"
|
||||
switch.register_async_callback = Mock()
|
||||
async_dispatcher_send(hass, 'deconz_new_light', [switch])
|
||||
await hass.async_block_till_done()
|
||||
assert "switch.name" in hass.data[deconz.DOMAIN].deconz_ids
|
||||
|
||||
|
||||
async def test_unsupported_switch(hass):
|
||||
"""Test that unsupported switches are not created."""
|
||||
await setup_gateway(hass, {"lights": UNSUPPORTED_SWITCH})
|
||||
assert len(hass.states.async_all()) == 0
|
||||
|
||||
|
||||
async def test_unload_switch(hass):
|
||||
"""Test that it works to unload switch entities."""
|
||||
await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
|
||||
|
||||
await hass.data[deconz.DOMAIN].async_reset()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
Reference in new issue
Block a user