Ejemplo n.º 1
0
 def serialize(cls, share_detail: ShareDetail) -> Dict:
     return {
         cls._FIELD_PAYMENT:
         converter.serialize(share_detail._payment_field_for_request),
         cls._FIELD_READ_ONLY:
         converter.serialize(share_detail._read_only_field_for_request),
         cls._FIELD_DRAFT_PAYMENT:
         converter.serialize(share_detail._draft_payment),
     }
Ejemplo n.º 2
0
 def serialize(cls, installation: Installation) -> List:
     return [
         {
             cls._FIELD_ID: converter.serialize(installation.id_)
         },
         {
             cls._FIELD_TOKEN: converter.serialize(installation.token)
         },
         {
             cls._FIELD_SERVER_PUBLIC_KEY:
             converter.serialize(installation.server_public_key)
         },
     ]
Ejemplo n.º 3
0
    def serialize(cls, share_detail):
        """
        :type share_detail: object_.ShareDetail

        :rtype: dict
        """

        return {
            cls._FIELD_PAYMENT:
            converter.serialize(share_detail.payment),
            cls._FIELD_READ_ONLY:
            converter.serialize(share_detail.read_only),
            cls._FIELD_DRAFT_PAYMENT:
            converter.serialize(share_detail.draft_payment),
        }
Ejemplo n.º 4
0
    async def async_update(self, *_):
        """Update the data from bunq."""
        from bunq.sdk.exception import BunqException, ApiException
        from bunq.sdk.model.generated import endpoint
        from bunq.sdk.json import converter

        try:
            # get the account list which includes the balance
            acc_resp = await self.hass.async_add_executor_job(
                endpoint.MonetaryAccount.list)
            accounts = converter.serialize(acc_resp.value)
            # create a dict with the id and the balance value
            self.data = {
                a['MonetaryAccountBank']['id']:
                float(a['MonetaryAccountBank']['balance']['value'])
                for a in accounts}
            # update the individual sensors
            await self.update_devices()
        except KeyError:
            # if the key (the account id) is not found warn
            # can happen when a account is deleted in Bunq
            # but still present in HA
            _LOGGER.warning('Count not find account in API results.')
        except (BunqException, ApiException) as err:
            # if the Bunq sdk errors out there is
            # something wrong with the user setup
            # or the api call
            # log the error and raise HA error
            _LOGGER.error(err)
Ejemplo n.º 5
0
    def serialize(cls, monetary_account_reference):
        """
        :type monetary_account_reference: object_.MonetaryAccountReference

        :rtype: dict
        """

        return converter.serialize(monetary_account_reference.pointer)
Ejemplo n.º 6
0
async def async_setup_platform(hass, config, async_add_entities,
                               discovery_info=None):
    """Set up the Bunq Bank sensor platform."""
    from bunq.sdk.context import (
        ApiContext, ApiEnvironmentType,
        BunqContext)
    from bunq.sdk.exception import BunqException, ApiException
    from bunq.sdk.model.generated import endpoint
    from bunq.sdk.json import converter

    # set environment type
    api_environment = ApiEnvironmentType.SANDBOX \
        if config.get(CONF_SANDBOX) else ApiEnvironmentType.PRODUCTION

    accs = []
    try:
        # create the api context variable
        bunq_context = ApiContext(
            api_environment,
            config.get(CONF_API_KEY),
            'Home Assistant'
        )
        # ensure the key is active, or activate
        bunq_context.ensure_session_active()
        # load user context from api context (not IO)
        # checks if the user has active accounts
        # raises BunqException otherwise

        BunqContext.load_api_context(bunq_context)
        # call the account list endpoint
        accounts = converter.serialize(
            endpoint.MonetaryAccount.list().value)
        # create and add the devices to the list
        accs = [
            BunqAccountSensor(
                acc['MonetaryAccountBank']['description'],
                acc['MonetaryAccountBank']['id'],
                acc['MonetaryAccountBank']['currency'],
                float(acc['MonetaryAccountBank']['balance']['value']))
            for acc in accounts
            ]
        async_add_entities(accs)
        # create the refresh object
        data = BunqData(hass, bunq_context, accs)
        # schedule the first update
        await data.schedule_update(INTERVAL_TO_NEXT_REFRESH)
    except ApiException as err:
        # if there is something wrong with the user setup
        # such as a incorrect key or invalid IP address
        # log the error and raise HA error
        # nothing to setup further until the key is changed
        _LOGGER.error(err)
    except BunqException as err:
        # if the Bunq sdk errors out there is
        # such as API rate limit throtteling
        # log the error and raise PlatformNotReady to retry
        _LOGGER.error(err)
        raise PlatformNotReady
Ejemplo n.º 7
0
    def serialize(cls, installation):
        """
        :type installation: core.Installation

        :rtype: list
        """

        return [
            {
                cls._FIELD_ID: converter.serialize(installation.id_)
            },
            {
                cls._FIELD_TOKEN: converter.serialize(installation.token)
            },
            {
                cls._FIELD_SERVER_PUBLIC_KEY:
                converter.serialize(installation.server_public_key),
            },
        ]
    def test_api_scenario_payment_listing_with_pagination(self):
        self._ensure_enough_payments()
        payments_expected = self._payments_required()
        pagination = client.Pagination()
        pagination.count = self._PAYMENT_LISTING_PAGE_SIZE

        response_latest = self._list_payments(pagination.url_params_count_only)
        pagination_latest = response_latest.pagination
        response_previous = self._list_payments(
            pagination_latest.url_params_previous_page)
        pagination_previous = response_previous.pagination
        response_previous_next = self._list_payments(
            pagination_previous.url_params_next_page)
        payments_previous = response_previous.value
        payments_previous_next = response_previous_next.value
        payments_actual = payments_previous_next + payments_previous
        payments_expected_serialized = converter.serialize(payments_expected)
        payments_actual_serialized = converter.serialize(payments_actual)

        self.assertEqual(payments_expected_serialized,
                         payments_actual_serialized)
Ejemplo n.º 9
0
 def serialize(cls, session_server: SessionServer) -> List:
     return [
         {
             cls._FIELD_ID: converter.serialize(session_server.id_)
         },
         {
             cls._FIELD_TOKEN: converter.serialize(session_server.token)
         },
         {
             cls._FIELD_USER_COMPANY:
             converter.serialize(session_server.user_company),
         },
         {
             cls._FIELD_USER_PERSON:
             converter.serialize(session_server.user_person),
         },
         {
             cls._FIELD_USER_API_KEY:
             converter.serialize(session_server.user_api_key),
         },
         {
             cls._FIELD_USER_PAYMENT_SERVER_PROVIDER:
             converter.serialize(
                 session_server.user_payment_service_provider),
         },
     ]
Ejemplo n.º 10
0
    def serialize(cls, session_server):
        """
        :type session_server: core.SessionServer

        :rtype: list
        """

        return [
            {
                cls._FIELD_ID: converter.serialize(session_server.id_)
            },
            {
                cls._FIELD_TOKEN: converter.serialize(session_server.token)
            },
            {
                cls._FIELD_USER_COMPANY:
                converter.serialize(session_server.user_company),
            },
            {
                cls._FIELD_USER_PERSON:
                converter.serialize(session_server.user_person),
            },
        ]
 def serialize(
         cls, monetary_account_reference: MonetaryAccountReference) -> Dict:
     return converter.serialize(monetary_account_reference.pointer)