This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+171 -168
View File
@@ -12,14 +12,14 @@ from homeassistant.components.zwave import DATA_NETWORK, const
from tests.mock.zwave import MockNode, MockValue, MockEntityValues
VIEW_NAME = 'api:config:zwave:device_config'
VIEW_NAME = "api:config:zwave:device_config"
@pytest.fixture
def client(loop, hass, hass_client):
"""Client to communicate with Z-Wave config views."""
with patch.object(config, 'SECTIONS', ['zwave']):
loop.run_until_complete(async_setup_component(hass, 'config', {}))
with patch.object(config, "SECTIONS", ["zwave"]):
loop.run_until_complete(async_setup_component(hass, "config", {}))
return loop.run_until_complete(hass_client())
@@ -27,37 +27,26 @@ def client(loop, hass, hass_client):
@asyncio.coroutine
def test_get_device_config(client):
"""Test getting device config."""
def mock_read(path):
"""Mock reading data."""
return {
'hello.beer': {
'free': 'beer',
},
'other.entity': {
'do': 'something',
},
}
return {"hello.beer": {"free": "beer"}, "other.entity": {"do": "something"}}
with patch('homeassistant.components.config._read', mock_read):
resp = yield from client.get(
'/api/config/zwave/device_config/hello.beer')
with patch("homeassistant.components.config._read", mock_read):
resp = yield from client.get("/api/config/zwave/device_config/hello.beer")
assert resp.status == 200
result = yield from resp.json()
assert result == {'free': 'beer'}
assert result == {"free": "beer"}
@asyncio.coroutine
def test_update_device_config(client):
"""Test updating device config."""
orig_data = {
'hello.beer': {
'ignored': True,
},
'other.entity': {
'polling_intensity': 2,
},
"hello.beer": {"ignored": True},
"other.entity": {"polling_intensity": 2},
}
def mock_read(path):
@@ -70,18 +59,19 @@ def test_update_device_config(client):
"""Mock writing data."""
written.append(data)
with patch('homeassistant.components.config._read', mock_read), \
patch('homeassistant.components.config._write', mock_write):
with patch("homeassistant.components.config._read", mock_read), patch(
"homeassistant.components.config._write", mock_write
):
resp = yield from client.post(
'/api/config/zwave/device_config/hello.beer', data=json.dumps({
'polling_intensity': 2
}))
"/api/config/zwave/device_config/hello.beer",
data=json.dumps({"polling_intensity": 2}),
)
assert resp.status == 200
result = yield from resp.json()
assert result == {'result': 'ok'}
assert result == {"result": "ok"}
orig_data['hello.beer']['polling_intensity'] = 2
orig_data["hello.beer"]["polling_intensity"] = 2
assert written[0] == orig_data
@@ -90,9 +80,9 @@ def test_update_device_config(client):
def test_update_device_config_invalid_key(client):
"""Test updating device config."""
resp = yield from client.post(
'/api/config/zwave/device_config/invalid_entity', data=json.dumps({
'polling_intensity': 2
}))
"/api/config/zwave/device_config/invalid_entity",
data=json.dumps({"polling_intensity": 2}),
)
assert resp.status == 400
@@ -101,9 +91,9 @@ def test_update_device_config_invalid_key(client):
def test_update_device_config_invalid_data(client):
"""Test updating device config."""
resp = yield from client.post(
'/api/config/zwave/device_config/hello.beer', data=json.dumps({
'invalid_option': 2
}))
"/api/config/zwave/device_config/hello.beer",
data=json.dumps({"invalid_option": 2}),
)
assert resp.status == 400
@@ -112,7 +102,8 @@ def test_update_device_config_invalid_data(client):
def test_update_device_config_invalid_json(client):
"""Test updating device config."""
resp = yield from client.post(
'/api/config/zwave/device_config/hello.beer', data='not json')
"/api/config/zwave/device_config/hello.beer", data="not json"
)
assert resp.status == 400
@@ -121,25 +112,31 @@ def test_update_device_config_invalid_json(client):
def test_get_values(hass, client):
"""Test getting values on node."""
node = MockNode(node_id=1)
value = MockValue(value_id=123456, node=node, label='Test Label',
instance=1, index=2, poll_intensity=4)
value = MockValue(
value_id=123456,
node=node,
label="Test Label",
instance=1,
index=2,
poll_intensity=4,
)
values = MockEntityValues(primary=value)
node2 = MockNode(node_id=2)
value2 = MockValue(value_id=234567, node=node2, label='Test Label 2')
value2 = MockValue(value_id=234567, node=node2, label="Test Label 2")
values2 = MockEntityValues(primary=value2)
hass.data[const.DATA_ENTITY_VALUES] = [values, values2]
resp = yield from client.get('/api/zwave/values/1')
resp = yield from client.get("/api/zwave/values/1")
assert resp.status == 200
result = yield from resp.json()
assert result == {
'123456': {
'label': 'Test Label',
'instance': 1,
'index': 2,
'poll_intensity': 4,
"123456": {
"label": "Test Label",
"instance": 1,
"index": 2,
"poll_intensity": 4,
}
}
@@ -149,24 +146,24 @@ def test_get_groups(hass, client):
"""Test getting groupdata on node."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=2)
node.groups.associations = 'assoc'
node.groups.associations_instances = 'inst'
node.groups.label = 'the label'
node.groups.max_associations = 'max'
node.groups.associations = "assoc"
node.groups.associations_instances = "inst"
node.groups.label = "the label"
node.groups.max_associations = "max"
node.groups = {1: node.groups}
network.nodes = {2: node}
resp = yield from client.get('/api/zwave/groups/2')
resp = yield from client.get("/api/zwave/groups/2")
assert resp.status == 200
result = yield from resp.json()
assert result == {
'1': {
'association_instances': 'inst',
'associations': 'assoc',
'label': 'the label',
'max_associations': 'max'
"1": {
"association_instances": "inst",
"associations": "assoc",
"label": "the label",
"max_associations": "max",
}
}
@@ -179,7 +176,7 @@ def test_get_groups_nogroups(hass, client):
network.nodes = {2: node}
resp = yield from client.get('/api/zwave/groups/2')
resp = yield from client.get("/api/zwave/groups/2")
assert resp.status == 200
result = yield from resp.json()
@@ -193,12 +190,12 @@ def test_get_groups_nonode(hass, client):
network = hass.data[DATA_NETWORK] = MagicMock()
network.nodes = {1: 1, 5: 5}
resp = yield from client.get('/api/zwave/groups/2')
resp = yield from client.get("/api/zwave/groups/2")
assert resp.status == 404
result = yield from resp.json()
assert result == {'message': 'Node not found'}
assert result == {"message": "Node not found"}
@asyncio.coroutine
@@ -206,32 +203,34 @@ def test_get_config(hass, client):
"""Test getting config on node."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=2)
value = MockValue(
index=12,
command_class=const.COMMAND_CLASS_CONFIGURATION)
value.label = 'label'
value.help = 'help'
value.type = 'type'
value.data = 'data'
value.data_items = ['item1', 'item2']
value.max = 'max'
value.min = 'min'
value = MockValue(index=12, command_class=const.COMMAND_CLASS_CONFIGURATION)
value.label = "label"
value.help = "help"
value.type = "type"
value.data = "data"
value.data_items = ["item1", "item2"]
value.max = "max"
value.min = "min"
node.values = {12: value}
network.nodes = {2: node}
node.get_values.return_value = node.values
resp = yield from client.get('/api/zwave/config/2')
resp = yield from client.get("/api/zwave/config/2")
assert resp.status == 200
result = yield from resp.json()
assert result == {'12': {'data': 'data',
'data_items': ['item1', 'item2'],
'help': 'help',
'label': 'label',
'max': 'max',
'min': 'min',
'type': 'type'}}
assert result == {
"12": {
"data": "data",
"data_items": ["item1", "item2"],
"help": "help",
"label": "label",
"max": "max",
"min": "min",
"type": "type",
}
}
@asyncio.coroutine
@@ -243,7 +242,7 @@ def test_get_config_noconfig_node(hass, client):
network.nodes = {2: node}
node.get_values.return_value = node.values
resp = yield from client.get('/api/zwave/config/2')
resp = yield from client.get("/api/zwave/config/2")
assert resp.status == 200
result = yield from resp.json()
@@ -257,12 +256,12 @@ def test_get_config_nonode(hass, client):
network = hass.data[DATA_NETWORK] = MagicMock()
network.nodes = {1: 1, 5: 5}
resp = yield from client.get('/api/zwave/config/2')
resp = yield from client.get("/api/zwave/config/2")
assert resp.status == 404
result = yield from resp.json()
assert result == {'message': 'Node not found'}
assert result == {"message": "Node not found"}
@asyncio.coroutine
@@ -271,38 +270,33 @@ def test_get_usercodes_nonode(hass, client):
network = hass.data[DATA_NETWORK] = MagicMock()
network.nodes = {1: 1, 5: 5}
resp = yield from client.get('/api/zwave/usercodes/2')
resp = yield from client.get("/api/zwave/usercodes/2")
assert resp.status == 404
result = yield from resp.json()
assert result == {'message': 'Node not found'}
assert result == {"message": "Node not found"}
@asyncio.coroutine
def test_get_usercodes(hass, client):
"""Test getting usercodes on node."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=18,
command_classes=[const.COMMAND_CLASS_USER_CODE])
value = MockValue(
index=0,
command_class=const.COMMAND_CLASS_USER_CODE)
node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_USER_CODE])
value = MockValue(index=0, command_class=const.COMMAND_CLASS_USER_CODE)
value.genre = const.GENRE_USER
value.label = 'label'
value.data = '1234'
value.label = "label"
value.data = "1234"
node.values = {0: value}
network.nodes = {18: node}
node.get_values.return_value = node.values
resp = yield from client.get('/api/zwave/usercodes/18')
resp = yield from client.get("/api/zwave/usercodes/18")
assert resp.status == 200
result = yield from resp.json()
assert result == {'0': {'code': '1234',
'label': 'label',
'length': 4}}
assert result == {"0": {"code": "1234", "label": "label", "length": 4}}
@asyncio.coroutine
@@ -314,7 +308,7 @@ def test_get_usercode_nousercode_node(hass, client):
network.nodes = {18: node}
node.get_values.return_value = node.values
resp = yield from client.get('/api/zwave/usercodes/18')
resp = yield from client.get("/api/zwave/usercodes/18")
assert resp.status == 200
result = yield from resp.json()
@@ -326,19 +320,16 @@ def test_get_usercode_nousercode_node(hass, client):
def test_get_usercodes_no_genreuser(hass, client):
"""Test getting usercodes on node missing genre user."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=18,
command_classes=[const.COMMAND_CLASS_USER_CODE])
value = MockValue(
index=0,
command_class=const.COMMAND_CLASS_USER_CODE)
node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_USER_CODE])
value = MockValue(index=0, command_class=const.COMMAND_CLASS_USER_CODE)
value.genre = const.GENRE_SYSTEM
value.label = 'label'
value.data = '1234'
value.label = "label"
value.data = "1234"
node.values = {0: value}
network.nodes = {18: node}
node.get_values.return_value = node.values
resp = yield from client.get('/api/zwave/usercodes/18')
resp = yield from client.get("/api/zwave/usercodes/18")
assert resp.status == 200
result = yield from resp.json()
@@ -349,11 +340,11 @@ def test_get_usercodes_no_genreuser(hass, client):
@asyncio.coroutine
def test_save_config_no_network(hass, client):
"""Test saving configuration without network data."""
resp = yield from client.post('/api/zwave/saveconfig')
resp = yield from client.post("/api/zwave/saveconfig")
assert resp.status == 404
result = yield from resp.json()
assert result == {'message': 'No Z-Wave network data found'}
assert result == {"message": "No Z-Wave network data found"}
@asyncio.coroutine
@@ -361,36 +352,39 @@ def test_save_config(hass, client):
"""Test saving configuration."""
network = hass.data[DATA_NETWORK] = MagicMock()
resp = yield from client.post('/api/zwave/saveconfig')
resp = yield from client.post("/api/zwave/saveconfig")
assert resp.status == 200
result = yield from resp.json()
assert network.write_config.called
assert result == {'message': 'Z-Wave configuration saved to file.'}
assert result == {"message": "Z-Wave configuration saved to file."}
async def test_get_protection_values(hass, client):
"""Test getting protection values on node."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=18,
command_classes=[const.COMMAND_CLASS_PROTECTION])
node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION])
value = MockValue(
value_id=123456,
index=0,
instance=1,
command_class=const.COMMAND_CLASS_PROTECTION)
value.label = 'Protection Test'
value.data_items = ['Unprotected', 'Protection by Sequence',
'No Operation Possible']
value.data = 'Unprotected'
command_class=const.COMMAND_CLASS_PROTECTION,
)
value.label = "Protection Test"
value.data_items = [
"Unprotected",
"Protection by Sequence",
"No Operation Possible",
]
value.data = "Unprotected"
network.nodes = {18: node}
node.value = value
node.get_protection_item.return_value = "Unprotected"
node.get_protection_items.return_value = value.data_items
node.get_protections.return_value = {value.value_id: 'Object'}
node.get_protections.return_value = {value.value_id: "Object"}
resp = await client.get('/api/zwave/protection/18')
resp = await client.get("/api/zwave/protection/18")
assert resp.status == 200
result = await resp.json()
@@ -398,52 +392,51 @@ async def test_get_protection_values(hass, client):
assert node.get_protection_item.called
assert node.get_protection_items.called
assert result == {
'value_id': '123456',
'selected': 'Unprotected',
'options': ['Unprotected', 'Protection by Sequence',
'No Operation Possible']
"value_id": "123456",
"selected": "Unprotected",
"options": ["Unprotected", "Protection by Sequence", "No Operation Possible"],
}
async def test_get_protection_values_nonexisting_node(hass, client):
"""Test getting protection values on node with wrong nodeid."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=18,
command_classes=[const.COMMAND_CLASS_PROTECTION])
node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION])
value = MockValue(
value_id=123456,
index=0,
instance=1,
command_class=const.COMMAND_CLASS_PROTECTION)
value.label = 'Protection Test'
value.data_items = ['Unprotected', 'Protection by Sequence',
'No Operation Possible']
value.data = 'Unprotected'
command_class=const.COMMAND_CLASS_PROTECTION,
)
value.label = "Protection Test"
value.data_items = [
"Unprotected",
"Protection by Sequence",
"No Operation Possible",
]
value.data = "Unprotected"
network.nodes = {17: node}
node.value = value
resp = await client.get('/api/zwave/protection/18')
resp = await client.get("/api/zwave/protection/18")
assert resp.status == 404
result = await resp.json()
assert not node.get_protections.called
assert not node.get_protection_item.called
assert not node.get_protection_items.called
assert result == {'message': 'Node not found'}
assert result == {"message": "Node not found"}
async def test_get_protection_values_without_protectionclass(hass, client):
"""Test getting protection values on node without protectionclass."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=18)
value = MockValue(
value_id=123456,
index=0,
instance=1)
value = MockValue(value_id=123456, index=0, instance=1)
network.nodes = {18: node}
node.value = value
resp = await client.get('/api/zwave/protection/18')
resp = await client.get("/api/zwave/protection/18")
assert resp.status == 200
result = await resp.json()
@@ -456,103 +449,113 @@ async def test_get_protection_values_without_protectionclass(hass, client):
async def test_set_protection_value(hass, client):
"""Test setting protection value on node."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=18,
command_classes=[const.COMMAND_CLASS_PROTECTION])
node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION])
value = MockValue(
value_id=123456,
index=0,
instance=1,
command_class=const.COMMAND_CLASS_PROTECTION)
value.label = 'Protection Test'
value.data_items = ['Unprotected', 'Protection by Sequence',
'No Operation Possible']
value.data = 'Unprotected'
command_class=const.COMMAND_CLASS_PROTECTION,
)
value.label = "Protection Test"
value.data_items = [
"Unprotected",
"Protection by Sequence",
"No Operation Possible",
]
value.data = "Unprotected"
network.nodes = {18: node}
node.value = value
resp = await client.post(
'/api/zwave/protection/18', data=json.dumps({
'value_id': '123456', 'selection': 'Protection by Sequence'}))
"/api/zwave/protection/18",
data=json.dumps({"value_id": "123456", "selection": "Protection by Sequence"}),
)
assert resp.status == 200
result = await resp.json()
assert node.set_protection.called
assert result == {'message': 'Protection setting succsessfully set'}
assert result == {"message": "Protection setting succsessfully set"}
async def test_set_protection_value_failed(hass, client):
"""Test setting protection value failed on node."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=18,
command_classes=[const.COMMAND_CLASS_PROTECTION])
node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION])
value = MockValue(
value_id=123456,
index=0,
instance=1,
command_class=const.COMMAND_CLASS_PROTECTION)
value.label = 'Protection Test'
value.data_items = ['Unprotected', 'Protection by Sequence',
'No Operation Possible']
value.data = 'Unprotected'
command_class=const.COMMAND_CLASS_PROTECTION,
)
value.label = "Protection Test"
value.data_items = [
"Unprotected",
"Protection by Sequence",
"No Operation Possible",
]
value.data = "Unprotected"
network.nodes = {18: node}
node.value = value
node.set_protection.return_value = False
resp = await client.post(
'/api/zwave/protection/18', data=json.dumps({
'value_id': '123456', 'selection': 'Protecton by Seuence'}))
"/api/zwave/protection/18",
data=json.dumps({"value_id": "123456", "selection": "Protecton by Seuence"}),
)
assert resp.status == 202
result = await resp.json()
assert node.set_protection.called
assert result == {'message': 'Protection setting did not complete'}
assert result == {"message": "Protection setting did not complete"}
async def test_set_protection_value_nonexisting_node(hass, client):
"""Test setting protection value on nonexisting node."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=17,
command_classes=[const.COMMAND_CLASS_PROTECTION])
node = MockNode(node_id=17, command_classes=[const.COMMAND_CLASS_PROTECTION])
value = MockValue(
value_id=123456,
index=0,
instance=1,
command_class=const.COMMAND_CLASS_PROTECTION)
value.label = 'Protection Test'
value.data_items = ['Unprotected', 'Protection by Sequence',
'No Operation Possible']
value.data = 'Unprotected'
command_class=const.COMMAND_CLASS_PROTECTION,
)
value.label = "Protection Test"
value.data_items = [
"Unprotected",
"Protection by Sequence",
"No Operation Possible",
]
value.data = "Unprotected"
network.nodes = {17: node}
node.value = value
node.set_protection.return_value = False
resp = await client.post(
'/api/zwave/protection/18', data=json.dumps({
'value_id': '123456', 'selection': 'Protecton by Seuence'}))
"/api/zwave/protection/18",
data=json.dumps({"value_id": "123456", "selection": "Protecton by Seuence"}),
)
assert resp.status == 404
result = await resp.json()
assert not node.set_protection.called
assert result == {'message': 'Node not found'}
assert result == {"message": "Node not found"}
async def test_set_protection_value_missing_class(hass, client):
"""Test setting protection value on node without protectionclass."""
network = hass.data[DATA_NETWORK] = MagicMock()
node = MockNode(node_id=17)
value = MockValue(
value_id=123456,
index=0,
instance=1)
value = MockValue(value_id=123456, index=0, instance=1)
network.nodes = {17: node}
node.value = value
node.set_protection.return_value = False
resp = await client.post(
'/api/zwave/protection/17', data=json.dumps({
'value_id': '123456', 'selection': 'Protecton by Seuence'}))
"/api/zwave/protection/17",
data=json.dumps({"value_id": "123456", "selection": "Protecton by Seuence"}),
)
assert resp.status == 404
result = await resp.json()
assert not node.set_protection.called
assert result == {'message': 'No protection commandclass on this node'}
assert result == {"message": "No protection commandclass on this node"}