def list_accounts(): """ List all shared accounts (id, name, balance) """ # Ensure the whole account is included print("\nLoading Bunq API environment...") env = ApiEnvironmentType.PRODUCTION # Authenticate session & load context print("Authenticating and loading context...") api_context = ApiContext(ApiEnvironmentType.PRODUCTION, API_KEY, socket.gethostname()) api_context.ensure_session_active() BunqContext.load_api_context(api_context) # Get user info print("Loading user info...\n") user = endpoint.User.get().value.get_referenced_object() # Fetch account detials accounts = endpoint.MonetaryAccountJoint.list().value for account in accounts: print( f"[{account.id_}] {account.description} (\N{euro sign}{account.balance.value})" )
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
def fetch_transactions(days): # Ensure the whole account is included print("\nLoading Bunq API environment...") env = ApiEnvironmentType.PRODUCTION # Authenticate session & load context print("Authenticating and loading context...") api_context = ApiContext(ApiEnvironmentType.PRODUCTION, API_KEY, socket.gethostname()) api_context.ensure_session_active() BunqContext.load_api_context(api_context) # Get user info print("Loading user info...") user = endpoint.User.get().value.get_referenced_object() # Fetch account detials account = endpoint.MonetaryAccountJoint.get(ACCOUNT_ID).value description = account.description balance = account.balance.value print( f"\nLoaded account '{description}' with current balance of \N{euro sign}{balance}" ) start_date = datetime.now() - timedelta(days) transactions = [] for transaction in iterate_transactions(ACCOUNT_ID): transaction_dict = { "timestamp": transaction.created, "amount": transaction.amount.value, "description": transaction.description.replace("\r", "").replace("\n", " "), "counterparty": transaction.counterparty_alias.label_monetary_account.display_name, } # Still in range if dateparse.parse(transaction.created) >= start_date: transactions.append(transaction_dict) else: break print(transactions)
def setup_context(self, api_key): api_context = ApiContext(ApiEnvironmentType.SANDBOX, api_key, socket.gethostname()) api_context.ensure_session_active() BunqContext.load_api_context(api_context)