Ejemplo n.º 1
0
    def test_command_parameters(self):
        """
        Tests all requests base action parameters
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        session.login_hash = "1"
        installation = Installation(session, 10)
        _self = self

        def assert_command(action):
            """
            asserts calls to a specific action

            :param action action being performed
            """

            responses.reset()
            responses.add(
                responses.GET,
                BASE_URL,
                status=200,
                body='<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><HASH>11111111111</HASH></PET>'
            )

            _self.assertEqual({"RES": "OK", "HASH": "11111111111"}, installation.execute_command(action))
            _self.assertEqual(len(responses.calls), 1)
            _self.assertTrue(action in responses.calls[0].request.params["request"])

        assert_command("ACT_V2")
        assert_command("SRV")
        assert_command("MYINSTALLATION")
        assert_command("INS")
Ejemplo n.º 2
0
    def test_re_login(self):
        """
        Tests an valid close attempt
        """

        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body=
            '<?xml version="1.0" encoding="UTF-8"?><PET><RES>ERROR</RES><ERR>60067</ERR></PET>'
        )

        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body=
            '<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><HASH>11111111111</HASH></PET>'
        )

        session = Session("u1", "p1", "i1", "c1", "l1")
        session.login_hash = "2"
        session.get({"ACTION": "REQ"})
        self.assertEqual("11111111111", session.login_hash)
Ejemplo n.º 3
0
    def test_get_or_create_session(self):
        """
        Tests creating a session and getting the created one
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        self.assertEqual(session.get_or_create_session(),
                         session.get_or_create_session())
Ejemplo n.º 4
0
 def __init__(self, domain_config):
     """Initialize the Securitas hub."""
     self.overview = {}
     self.config = domain_config
     self.session = Session(domain_config[CONF_USERNAME],
                            domain_config[CONF_PASSWORD],
                            domain_config[CONF_INSTALLATION],
                            domain_config[CONF_COUNTRY].upper(),
                            domain_config[CONF_LANG].lower())
     self.installation = Installation(self.session)
     self.alarm = Alarm(self.session)
     self.installation_num = domain_config[CONF_INSTALLATION]
     self.installation_alias = None
Ejemplo n.º 5
0
    def test_invalid_connect_no_hash(self):
        """
        Tests an invalid connection attempt without hash value in the response
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body=
            '<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES></PET>')

        with pytest.raises(ConnectionException):
            session.connect()
Ejemplo n.º 6
0
    def test_basic_configuration(self):
        """
        Tests session basic configurations
        """

        session = Session("u1", "p1", "i1", "c1", "l1", "s1")
        self.assertEqual("u1", session.username)
        self.assertEqual("p1", session.password)
        self.assertEqual("i1", session.installation)
        self.assertEqual("C1", session.country)
        self.assertEqual("l1", session.lang)
        self.assertEqual("s1", session.sensor)
        self.assertEqual(30, session.timeout)
        session.set_timeout(60)
        self.assertEqual(60, session.timeout)
Ejemplo n.º 7
0
    def test_invalid_close(self):
        """
        Tests an invalid close attempt
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body=
            '<?xml version="1.0" encoding="UTF-8"?><PET><RES>ERROR</RES></PET>'
        )

        with pytest.raises(ConnectionException):
            session.close()
Ejemplo n.º 8
0
    def test_get_installation_alias(self):
        """
        Tests getting the installation alias
        """

        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body='<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><INSTALATION><ALIAS>alias</ALIAS></INSTALATION></PET>'
        )

        session = Session("u1", "p1", "i1", "c1", "l1")
        session.login_hash = "1"
        installation = Installation(session, 10)
        self.assertEqual("alias", installation.get_alias())
Ejemplo n.º 9
0
    def test_async_valid_request(self):
        """
        Tests a valid request
        """

        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body='<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><HASH>11111111111</HASH></PET>'
        )

        session = Session("u1", "p1", "i1", "c1", "l1")
        session.login_hash = "1"
        alarm = Installation(session, 1)
        self.assertEqual({"RES": "OK", "HASH": "11111111111"}, alarm.async_request("DUMMY"))
        self.assertGreaterEqual(len(responses.calls), 2)
Ejemplo n.º 10
0
    def test_inf_request_timeout(self):
        """
        Tests a request timeout
        """

        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body='<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><LIST><REG signaltype="1"></REG><REG signaltype="1"></REG></LIST></PET>'
        )

        session = Session("u1", "p1", "i1", "c1", "l1")
        session.login_hash = "1"
        alarm = Installation(session, 1)
        self.assertIsNone(alarm.get_inf())
        self.assertGreaterEqual(len(responses.calls), 1)
Ejemplo n.º 11
0
    def test_async_request_timeout(self):
        """
        Tests a request timeout
        """

        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body='<?xml version="1.0" encoding="UTF-8"?><PET><RES>WAIT</RES></PET>'
        )

        session = Session("u1", "p1", "i1", "c1", "l1")
        session.login_hash = "1"
        installation = Installation(session, 1)
        self.assertIsNone(installation.async_request("DUMMY"))
        self.assertGreaterEqual(len(responses.calls), 2)
Ejemplo n.º 12
0
    def test_valid_connect(self):
        """
        Tests an valid connection attempt
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body=
            '<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><HASH>11111111111</HASH></PET>'
        )

        session.connect()
        self.assertTrue(session.is_connected())
        self.assertEqual("11111111111", session.login_hash)
Ejemplo n.º 13
0
    def test_build_payload(self):
        """
        Tests building payload for performing actions against the api
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        self.assertEqual(
            {
                "Country": session.country,
                "user": session.username,
                "pwd": session.password,
                "lang": session.lang,
                "hash": None,
                "callby": "AND_61",
                "numinst": "i1",
                "panel": "SDVFAST",
                "cust1": "v1"
            }, session.build_payload(cust1="v1"))
Ejemplo n.º 14
0
class SecuritasHub:
    """A Securitas hub wrapper class."""
    def __init__(self, domain_config):
        """Initialize the Securitas hub."""
        self.overview = {}
        self.config = domain_config
        self.session = Session(domain_config[CONF_USERNAME],
                               domain_config[CONF_PASSWORD],
                               domain_config[CONF_INSTALLATION],
                               domain_config[CONF_COUNTRY].upper(),
                               domain_config[CONF_LANG].lower())
        self.installation = Installation(self.session)
        self.alarm = Alarm(self.session)
        self.installation_num = domain_config[CONF_INSTALLATION]
        self.installation_alias = None

    def login(self):
        """Login to Securitas."""
        self.session.connect()
        self.installation_alias = self.installation.get_alias()

        return self.session.is_connected()

    def logout(self):
        """Logout from Securitas."""
        self.session.close()

        return True

    def update_overview(self):
        """Update the overview."""

        filter = ('1', '2', '31', '32', '46', '202', '311', '13', '24')
        res = self.installation.get_activity_log()
        _LOGGER.debug(res)
        try:
            regs = res['LIST']['REG']
            for reg in regs:
                if filter is None or reg['@type'] in filter:
                    self.overview = reg

                    return
        except (KeyError, TypeError):
            pass
Ejemplo n.º 15
0
    def test_command_parameters(self):
        """
        Tests all requests base action parameters
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        session.login_hash = "1"
        alarm = Alarm(session, 10)
        _self = self

        def assert_command(action):
            """
            asserts calls to a specific action

            :param action action being performed
            """

            responses.reset()
            responses.add(
                responses.GET,
                BASE_URL,
                status=200,
                body=
                '<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><HASH>11111111111</HASH></PET>'
            )

            _self.assertEqual({
                "RES": "OK",
                "HASH": "11111111111"
            }, alarm.execute_command(action))
            _self.assertGreaterEqual(len(responses.calls), 2)
            _self.assertTrue(
                action + "1" in responses.calls[0].request.params["request"])
            _self.assertTrue(
                action + "2" in responses.calls[1].request.params["request"])

        assert_command("ARM")
        assert_command("ARMDAY")
        assert_command("ARMNIGHT")
        assert_command("PERI")
        assert_command("DARM")
        assert_command("ARMANNEX")
        assert_command("DARMANNEX")
        assert_command("EST")
Ejemplo n.º 16
0
    def test_valid_close(self):
        """
        Tests an valid close attempt
        """

        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body=
            '<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><HASH>11111111111</HASH></PET>'
        )

        with Session("u1", "p1", "i1", "c1", "l1") as session:
            pass
Ejemplo n.º 17
0
    def run(self):
        """
        Runs a the command provided on cli arguments

        :return: the result from the operation
        """

        command = self.args.command

        with Session(self.args.username, self.args.password, self.args.installation, self.args.country,
                     self.args.language, self.args.sensor) as session:
            if command in alarm_commands():
                self.result = Alarm(session).execute_command(command)
            elif command in installation_commands():
                self.result = Installation(session).execute_command(command)
            elif command in camera_commands():
                self.result = Camera(session).execute_command(command)
Ejemplo n.º 18
0
    def test_validate_connection(self):
        """
        Tests that a connection was established
        """

        session = Session("u1", "p1", "i1", "c1", "l1")
        responses.add(
            responses.GET,
            BASE_URL,
            status=200,
            body=
            '<?xml version="1.0" encoding="UTF-8"?><PET><RES>OK</RES><HASH>11111111111</HASH></PET>'
        )

        self.assertFalse(session.is_connected())
        with pytest.raises(ConnectionException):
            session.validate_connection()

        session.connect()
        self.assertEqual("11111111111", session.login_hash)