Skip to content

Commit

Permalink
Improved Sequence Handling Within Areas (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc authored Jan 25, 2021
1 parent 7c81aed commit 1ab0784
Show file tree
Hide file tree
Showing 14 changed files with 422 additions and 41 deletions.
10 changes: 5 additions & 5 deletions tests/test_comnav_0_106.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_comnav_0_106_communication(mock_post):
assert len(uobj.areas) == 1
assert uobj.areas[0]['name'] == 'Area 1'
assert uobj.areas[0]['bank'] == 0
assert uobj.areas[0]['sequence'] == 244
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

assert isinstance(uobj.zones, dict)
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_comnav_0_106_communication(mock_post):
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.xml'
assert uobj.areas[0]['sequence'] == 244
assert uobj.areas[0]['sequence'] == 1

# Update our sequence file so that it reflects a change
# This update will report an area update and 2 zones (bank 0 and 4)
Expand Down Expand Up @@ -206,8 +206,8 @@ def test_comnav_0_106_communication(mock_post):
assert len(uobj.areas) == 1
assert uobj.areas[0]['name'] == 'Area 1'
assert uobj.areas[0]['bank'] == 0
# Our sequence got bumped
assert uobj.areas[0]['sequence'] == 245
# Our sequence got bumped because of a status change
assert uobj.areas[0]['sequence'] == 2
assert uobj.areas[0]['status'] == 'Not Ready'

# Reset our mock object
Expand All @@ -222,4 +222,4 @@ def test_comnav_0_106_communication(mock_post):
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.xml'
assert uobj.areas[0]['sequence'] == 245
assert uobj.areas[0]['sequence'] == 2
191 changes: 191 additions & 0 deletions tests/test_comnav_0_108-zone-check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Chris Caron <[email protected]>
# All rights reserved.
#
# This code is licensed under the MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files(the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions :
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import mock
import requests
from os.path import join
from os.path import dirname

from ultrasync import UltraSync
from ultrasync.common import NX595EVendor

# Disable logging for a cleaner testing output
import logging
logging.disable(logging.CRITICAL)

# Reference Directory
ULTRASYNC_TEST_VAR_DIR = \
join(dirname(__file__), 'var', NX595EVendor.COMNAV, '0.108-zone-test')


@mock.patch('requests.Session.post')
def test_comnav_0_108_zone_filter(mock_post):
"""
Test ComNav v0.108 Zone Filtering
"""

# A area response object
arobj = mock.Mock()

# Simulate a valid login return
with open(join(ULTRASYNC_TEST_VAR_DIR, 'area.htm'), 'rb') as f:
arobj.content = f.read()
arobj.status_code = requests.codes.ok

# A zone response object
zrobj = mock.Mock()

# Simulate initial zone configuration
with open(join(ULTRASYNC_TEST_VAR_DIR, 'zones.htm'), 'rb') as f:
zrobj.content = f.read()
zrobj.status_code = requests.codes.ok

# A sequence response object
seq_obj = mock.Mock()

# Simulate initial sequence configuration
with open(join(ULTRASYNC_TEST_VAR_DIR, 'seq.xml'), 'rb') as f:
seq_obj.content = f.read()
seq_obj.status_code = requests.codes.ok

# A zone state response object
zst_obj = mock.Mock()

# Simulate initial zone fetch configuration
with open(join(ULTRASYNC_TEST_VAR_DIR, 'zstate.xml'), 'rb') as f:
zst_obj.content = f.read()
zst_obj.status_code = requests.codes.ok

# An area state response object
ast_obj = mock.Mock()

# Simulate initial area status fetch configuration
with open(join(ULTRASYNC_TEST_VAR_DIR, 'status.xml'), 'rb') as f:
ast_obj.content = f.read()
ast_obj.status_code = requests.codes.ok

# Assign our response object to our mocked instance of requests
mock_post.side_effect = (arobj, zrobj)

uobj = UltraSync()

# Perform a login which under the hood queries both area.htm and zones.htm
# (in that order)
assert uobj.login()
assert uobj.vendor is NX595EVendor.COMNAV
assert uobj.version == '0.108'
assert uobj.release == 'O'

assert isinstance(uobj.areas, dict)
# 2 Areas defined
assert len(uobj.areas) == 2
assert uobj.areas[0]['name'] == 'AREA 1'
assert uobj.areas[0]['bank'] == 0
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

assert uobj.areas[1]['name'] == 'AREA 2'
assert uobj.areas[1]['bank'] == 1
assert uobj.areas[1]['sequence'] == 1
assert uobj.areas[1]['status'] == 'Ready'

assert isinstance(uobj.zones, dict)
# our total zones defined (we want to be sure we don't
# include the '%2D' or '-')
assert len(uobj.zones) == 17

# A call to login.cgi (which fetches area.html) and then zones.htm
assert mock_post.call_count == 2
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/login.cgi'
assert mock_post.call_args_list[1][0][0] == \
'http://zerowire/user/zones.htm'

# Reset our mock object
mock_post.reset_mock()

# Update our side effects
mock_post.side_effect = (seq_obj, ast_obj, zst_obj)

# Perform Updated Query
uobj.update(max_age_sec=0)

# Only 1 query made because seq.xml file unchanged
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.xml'
assert uobj.areas[0]['sequence'] == 1

# Update our sequence file so that it reflects a change
with open(join(ULTRASYNC_TEST_VAR_DIR, 'seq.w.update.xml'), 'rb') as f:
seq_obj.content = f.read()

# Reset our mock object
mock_post.reset_mock()

# Update our side effects
mock_post.side_effect = (seq_obj, ast_obj, zst_obj)

# Perform Updated Query
uobj.update(max_age_sec=0)

assert mock_post.call_count == 3
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.xml'
assert mock_post.call_args_list[1][0][0] == \
'http://zerowire/user/zstate.xml'
assert mock_post.call_args_list[2][0][0] == \
'http://zerowire/user/status.xml'

assert isinstance(uobj.areas, dict)
assert len(uobj.areas) == 2
assert uobj.areas[0]['name'] == 'AREA 1'
assert uobj.areas[0]['bank'] == 0
# Our sequence does not change because our status did not
# change from one state to another
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

assert uobj.areas[1]['name'] == 'AREA 2'
assert uobj.areas[1]['bank'] == 1
# Our sequence does not change because our status did not
# change from one state to another
assert uobj.areas[1]['sequence'] == 1
assert uobj.areas[1]['status'] == 'Ready'

# Reset our mock object
mock_post.reset_mock()

# Update our side effects
mock_post.side_effect = seq_obj

uobj.details(max_age_sec=0)

# Only 1 query made because seq.xml file unchanged
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.xml'
assert uobj.areas[0]['sequence'] == 1
8 changes: 4 additions & 4 deletions tests/test_comnav_0_108.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_comnav_0_108_communication(mock_post):
assert len(uobj.areas) == 1
assert uobj.areas[0]['name'] == 'Home'
assert uobj.areas[0]['bank'] == 0
assert uobj.areas[0]['sequence'] == 178
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

assert isinstance(uobj.zones, dict)
Expand Down Expand Up @@ -157,7 +157,7 @@ def test_comnav_0_108_communication(mock_post):
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.xml'
assert uobj.areas[0]['sequence'] == 178
assert uobj.areas[0]['sequence'] == 1

# Update our sequence file so that it reflects a change
with open(join(ULTRASYNC_TEST_VAR_DIR, 'seq.w.update.xml'), 'rb') as f:
Expand Down Expand Up @@ -185,7 +185,7 @@ def test_comnav_0_108_communication(mock_post):
assert uobj.areas[0]['name'] == 'Home'
assert uobj.areas[0]['bank'] == 0
# Our sequence got bumped
assert uobj.areas[0]['sequence'] == 179
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

# Reset our mock object
Expand All @@ -200,4 +200,4 @@ def test_comnav_0_108_communication(mock_post):
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.xml'
assert uobj.areas[0]['sequence'] == 179
assert uobj.areas[0]['sequence'] == 1
2 changes: 1 addition & 1 deletion tests/test_comnav_0_108_burglar_alarm_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_comnav_0_108_active_burglary(mock_post):
assert len(uobj.areas) == 1
assert uobj.areas[0]['name'] == 'Home'
assert uobj.areas[0]['bank'] == 0
assert uobj.areas[0]['sequence'] == 208
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Burglar Alarm'

assert isinstance(uobj.zones, dict)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_xgen_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ def test_xgen_general_communication(mock_post):
assert len(uobj.areas) == 2
assert uobj.areas[0]['name'] == 'Area 1'
assert uobj.areas[0]['bank'] == 0
assert uobj.areas[0]['sequence'] == 126
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

assert uobj.areas[1]['name'] == 'Area 2'
assert uobj.areas[1]['bank'] == 1
assert uobj.areas[1]['sequence'] == 0
assert uobj.areas[1]['sequence'] == 1
assert uobj.areas[1]['status'] == 'Ready'

assert isinstance(uobj.zones, dict)
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_xgen_general_communication(mock_post):
assert uobj.areas[1]['name'] == 'Area 2'
assert uobj.areas[1]['bank'] == 1
# Our sequence got bumped
assert uobj.areas[0]['sequence'] == 127
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

# Reset our mock object
Expand All @@ -220,7 +220,7 @@ def test_xgen_general_communication(mock_post):
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.json'
assert uobj.areas[0]['sequence'] == 127
assert uobj.areas[0]['sequence'] == 1

# Reset our mock object
mock_post.reset_mock()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_zerowire_armed.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_zerowire_armed_communication(mock_post):
assert len(uobj.areas) == 1
assert uobj.areas[0]['name'] == 'Area 1'
assert uobj.areas[0]['bank'] == 0
assert uobj.areas[0]['sequence'] == 203
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Exit Delay 1'

assert isinstance(uobj.zones, dict)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_zerowire_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_zerowire_general_communication(mock_post):
assert len(uobj.areas) == 1
assert uobj.areas[0]['name'] == 'Area 1'
assert uobj.areas[0]['bank'] == 0
assert uobj.areas[0]['sequence'] == 149
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

assert isinstance(uobj.zones, dict)
Expand Down Expand Up @@ -176,7 +176,7 @@ def test_zerowire_general_communication(mock_post):
assert uobj.areas[0]['name'] == 'Area 1'
assert uobj.areas[0]['bank'] == 0
# Our sequence got bumped
assert uobj.areas[0]['sequence'] == 150
assert uobj.areas[0]['sequence'] == 1
assert uobj.areas[0]['status'] == 'Ready'

# Reset our mock object
Expand All @@ -191,4 +191,4 @@ def test_zerowire_general_communication(mock_post):
assert mock_post.call_count == 1
assert mock_post.call_args_list[0][0][0] == \
'http://zerowire/user/seq.json'
assert uobj.areas[0]['sequence'] == 150
assert uobj.areas[0]['sequence'] == 1
Loading

0 comments on commit 1ab0784

Please sign in to comment.