def test_partitions_callable_prop(mocker): alarm = Paradox() alarm.panel = mocker.MagicMock() alarm.panel.property_map = { "arm": dict(level=EventLevel.INFO, message={ "True": "{Type} {label} is armed", "False": "{Type} {label} is disarmed" }), } alarm._on_labels_load(data=dict( partition={1: dict(id=1, label='Partition 1', key='Partition_1')})) sendChange = mocker.patch("paradox.data.memory_storage.ps.sendChange") alarm._on_status_update(status=dict(partition={1: dict(arm=False)})) sendChange.assert_any_call( Change('partition', 'Partition_1', 'arm', False, initial=True)) sendChange.reset_mock() alarm.storage.update_container_object("partition", "Partition_1", dict(arm=lambda old: not old)) sendChange.assert_any_call(Change('partition', 'Partition_1', 'arm', True))
def test_partitions(mocker): alarm = Paradox() alarm.panel = mocker.MagicMock() alarm.panel.property_map = { "arm": dict(level=EventLevel.INFO, message={"True": "{Type} {label} is armed", "False": "{Type} {label} is disarmed"}), } event = mocker.MagicMock() mocker.patch("paradox.lib.ps.sendChange") mocker.patch("paradox.lib.ps.sendEvent") mocker.patch('paradox.event.ChangeEvent', return_value=event) ps.sendMessage("labels_loaded", data=dict( partition={ 1: dict( id=1, label='Partition 1', key='Partition_1' ) } )) assert isinstance(alarm.panel, mocker.MagicMock) alarm.storage.update_container_object("partition", "Partition_1", dict(arm=True)) ps.sendChange.assert_called_once_with(Change('partition', 'Partition_1', 'arm', True, initial=True)) ps.sendChange.reset_mock() assert isinstance(alarm.panel, mocker.MagicMock) ps.sendMessage("status_update", status=dict( partition={ 1: dict( arm=False ) } )) assert isinstance(alarm.panel, mocker.MagicMock) ps.sendChange.assert_any_call(Change('partition', 'Partition_1', 'current_state', 'disarmed', initial=True)) ps.sendChange.assert_any_call(Change('partition', 'Partition_1', 'target_state', 'disarmed', initial=True)) ps.sendChange.assert_any_call(Change('partition', 'Partition_1', 'arm', False, initial=False)) assert ps.sendChange.call_count == 3 assert ps.sendEvent.call_count == 0
def test_property(property, type, value, partition, label): change = Change(property=property, new_value=value, old_value=None, type=type, key=label) evt = ChangeEvent(change_object=change, property_map=Panel_EVO192.property_map) assert evt assert len(evt.message) > 0 print(evt.message)
def test_partition_arm_message(): change = Change(property="arm", new_value=True, type="partition", key="Fridge") evt = ChangeEvent(change_object=change, property_map=Panel.property_map) assert evt assert evt.message == "Partition Fridge is armed"
def test_partitions(mocker): alarm = Paradox() alarm.panel = mocker.MagicMock() alarm.panel.property_map = { "arm": dict( level=EventLevel.INFO, message={ "True": "{Type} {label} is armed", "False": "{Type} {label} is disarmed", }, ), } alarm._on_labels_load(data=dict( partition={1: dict(id=1, label="Partition 1", key="Partition_1")})) sendChange = mocker.patch("paradox.data.memory_storage.ps.sendChange") alarm.storage.update_container_object("partition", "Partition_1", dict(arm=True)) sendChange.assert_called_once_with( Change("partition", "Partition_1", "arm", True, initial=True)) sendChange.reset_mock() assert isinstance(alarm.panel, mocker.MagicMock) alarm._on_status_update(status=dict(partition={1: dict(arm=False)})) assert isinstance(alarm.panel, mocker.MagicMock) sendChange.assert_any_call( Change("partition", "Partition_1", "current_state", "disarmed", initial=True)) sendChange.assert_any_call( Change("partition", "Partition_1", "target_state", "disarmed", initial=True)) sendChange.assert_any_call( Change("partition", "Partition_1", "arm", False, initial=False))
def test_property_map_bad(): change = Change(property='does_not_exist', new_value=True, old_value=None, type='system', key='alarm_in_memory') with pytest.raises(AssertionError): ChangeEvent(change_object=change, property_map=Panel_EVO192.property_map)
def test_property_map_value(): change = Change(property='arm', new_value=True, old_value=None, type='partition', key='Fridge') evt = ChangeEvent(change_object=change, property_map=Panel_EVO192.property_map) assert evt assert evt.message == "Partition Fridge is armed"
def update_container_object(self, container_name: str, key: str, changes: dict): assert container_name is not None assert key is not None assert isinstance(changes, dict) if not changes: # Has at least one element return # logger.debug('update_properties %s/%s=%s', container_name, key, change) element = self.get_container_object(container_name, key, create_if_missing=True) object_key = element["key"] # Publish changes and update state for property_name, property_value in changes.items(): if not isinstance(property_name, str): logger.debug( "Invalid property name ({}/{}/{}) type: {}".format( container_name, object_key, property_name, type(property_name))) continue if property_name.startswith("_"): # skip private properties continue old = element.get(property_name) if isinstance( property_value, Callable): # function to make new value from the old one try: property_value = property_value(old) except: logger.exception( 'Exception caught during property "%s" convert. Ignoring', property_name, ) continue # Standard processing of changes if property_name in element: if old == property_value and not cfg.PUSH_UPDATE_WITHOUT_CHANGE: continue element[property_name] = property_value else: element[ property_name] = property_value # Initial value, do not notify # suppress = 'trouble' not in property_name change_object = Change(container_name, object_key, property_name, property_value, old) logger.debug(change_object) ps.sendChange(change_object)
def test_partitions_callable_prop(mocker): alarm = Paradox() alarm.panel = mocker.MagicMock() alarm.panel.property_map = { "arm": dict(level=EventLevel.INFO, message={"True": "{Type} {label} is armed", "False": "{Type} {label} is disarmed"}), } event = mocker.MagicMock() mocker.patch.object(ps, "sendChange") mocker.patch.object(ps, "sendEvent") mocker.patch('paradox.event.ChangeEvent', return_value=event) ps.sendMessage("labels_loaded", data=dict( partition={ 1: dict( id=1, label='Partition 1', key='Partition_1' ) } )) ps.sendMessage("status_update", status=dict( partition={ 1: dict( arm=False ) } )) ps.sendChange.assert_any_call(Change('partition', 'Partition_1', 'arm', False, initial=True)) ps.sendChange.reset_mock() alarm.storage.update_container_object("partition", "Partition_1", dict(arm=lambda old: not old)) ps.sendChange.assert_any_call(Change('partition', 'Partition_1', 'arm', True)) ps.sendEvent.call_count = 0
def test_property_map_bad(): change = Change(property="dcd", new_value=3.33, type="system", key="power") with pytest.raises(AssertionError): ChangeEvent(change_object=change, property_map=Panel.property_map)
def test_property_map_value(): change = Change(property="dc", new_value=3.33, type="system", key="power") evt = ChangeEvent(change_object=change, property_map=Panel.property_map) assert evt assert evt.message == "DC voltage is 3.33V"
def test_property_map_bad(): change = Change(property='dcd', new_value=3.33, type='system', key='power') with pytest.raises(AssertionError): ChangeEvent(change_object=change, property_map=Panel.property_map)
def test_property_map_value(): change = Change(property='dc', new_value=3.33, type='system', key='power') evt = ChangeEvent(change_object=change, property_map=Panel.property_map) assert evt assert evt.message == "DC voltage is 3.33V"