This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+59 -52
View File
@@ -14,33 +14,37 @@ _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(hours=4)
ICON = 'mdi:currency-eur'
ICON = "mdi:currency-eur"
BankCredentials = namedtuple('BankCredentials', 'blz login pin url')
BankCredentials = namedtuple("BankCredentials", "blz login pin url")
CONF_BIN = 'bank_identification_number'
CONF_ACCOUNTS = 'accounts'
CONF_HOLDINGS = 'holdings'
CONF_ACCOUNT = 'account'
CONF_BIN = "bank_identification_number"
CONF_ACCOUNTS = "accounts"
CONF_HOLDINGS = "holdings"
CONF_ACCOUNT = "account"
ATTR_ACCOUNT = CONF_ACCOUNT
ATTR_BANK = 'bank'
ATTR_ACCOUNT_TYPE = 'account_type'
ATTR_BANK = "bank"
ATTR_ACCOUNT_TYPE = "account_type"
SCHEMA_ACCOUNTS = vol.Schema({
vol.Required(CONF_ACCOUNT): cv.string,
vol.Optional(CONF_NAME, default=None): vol.Any(None, cv.string),
})
SCHEMA_ACCOUNTS = vol.Schema(
{
vol.Required(CONF_ACCOUNT): cv.string,
vol.Optional(CONF_NAME, default=None): vol.Any(None, cv.string),
}
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_BIN): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PIN): cv.string,
vol.Required(CONF_URL): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_ACCOUNTS, default=[]): cv.ensure_list(SCHEMA_ACCOUNTS),
vol.Optional(CONF_HOLDINGS, default=[]): cv.ensure_list(SCHEMA_ACCOUNTS),
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_BIN): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PIN): cv.string,
vol.Required(CONF_URL): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_ACCOUNTS, default=[]): cv.ensure_list(SCHEMA_ACCOUNTS),
vol.Optional(CONF_HOLDINGS, default=[]): cv.ensure_list(SCHEMA_ACCOUNTS),
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
@@ -49,15 +53,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
Login to the bank and get a list of existing accounts. Create a
sensor for each account.
"""
credentials = BankCredentials(config[CONF_BIN], config[CONF_USERNAME],
config[CONF_PIN], config[CONF_URL])
credentials = BankCredentials(
config[CONF_BIN], config[CONF_USERNAME], config[CONF_PIN], config[CONF_URL]
)
fints_name = config.get(CONF_NAME, config[CONF_BIN])
account_config = {acc[CONF_ACCOUNT]: acc[CONF_NAME]
for acc in config[CONF_ACCOUNTS]}
account_config = {
acc[CONF_ACCOUNT]: acc[CONF_NAME] for acc in config[CONF_ACCOUNTS]
}
holdings_config = {acc[CONF_ACCOUNT]: acc[CONF_NAME]
for acc in config[CONF_HOLDINGS]}
holdings_config = {
acc[CONF_ACCOUNT]: acc[CONF_NAME] for acc in config[CONF_HOLDINGS]
}
client = FinTsClient(credentials, fints_name)
balance_accounts, holdings_accounts = client.detect_accounts()
@@ -65,31 +72,29 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
for account in balance_accounts:
if config[CONF_ACCOUNTS] and account.iban not in account_config:
_LOGGER.info('skipping account %s for bank %s',
account.iban, fints_name)
_LOGGER.info("skipping account %s for bank %s", account.iban, fints_name)
continue
account_name = account_config.get(account.iban)
if not account_name:
account_name = '{} - {}'.format(fints_name, account.iban)
account_name = "{} - {}".format(fints_name, account.iban)
accounts.append(FinTsAccount(client, account, account_name))
_LOGGER.debug('Creating account %s for bank %s',
account.iban, fints_name)
_LOGGER.debug("Creating account %s for bank %s", account.iban, fints_name)
for account in holdings_accounts:
if config[CONF_HOLDINGS] and \
account.accountnumber not in holdings_config:
_LOGGER.info('skipping holdings %s for bank %s',
account.accountnumber, fints_name)
if config[CONF_HOLDINGS] and account.accountnumber not in holdings_config:
_LOGGER.info(
"skipping holdings %s for bank %s", account.accountnumber, fints_name
)
continue
account_name = holdings_config.get(account.accountnumber)
if not account_name:
account_name = '{} - {}'.format(
fints_name, account.accountnumber)
account_name = "{} - {}".format(fints_name, account.accountnumber)
accounts.append(FinTsHoldingsAccount(client, account, account_name))
_LOGGER.debug('Creating holdings %s for bank %s',
account.accountnumber, fints_name)
_LOGGER.debug(
"Creating holdings %s for bank %s", account.accountnumber, fints_name
)
add_entities(accounts, True)
@@ -114,13 +119,18 @@ class FinTsClient:
object and also think about potential concurrency problems.
"""
from fints.client import FinTS3PinTanClient
return FinTS3PinTanClient(
self._credentials.blz, self._credentials.login,
self._credentials.pin, self._credentials.url)
self._credentials.blz,
self._credentials.login,
self._credentials.pin,
self._credentials.url,
)
def detect_accounts(self):
"""Identify the accounts of the bank."""
from fints.dialog import FinTSDialogError
balance_accounts = []
holdings_accounts = []
for account in self.client.get_sepa_accounts():
@@ -155,7 +165,7 @@ class FinTsAccount(Entity):
self._client = client # type: FinTsClient
self._account = account
self._name = name # type: str
self._balance = None # type: float
self._balance = None # type: float
self._currency = None # type: str
@property
@@ -172,7 +182,7 @@ class FinTsAccount(Entity):
balance = bank.get_balance(self._account)
self._balance = balance.amount.amount
self._currency = balance.amount.currency
_LOGGER.debug('updated balance of account %s', self.name)
_LOGGER.debug("updated balance of account %s", self.name)
@property
def name(self) -> str:
@@ -192,10 +202,7 @@ class FinTsAccount(Entity):
@property
def device_state_attributes(self) -> dict:
"""Additional attributes of the sensor."""
attributes = {
ATTR_ACCOUNT: self._account.iban,
ATTR_ACCOUNT_TYPE: 'balance',
}
attributes = {ATTR_ACCOUNT: self._account.iban, ATTR_ACCOUNT_TYPE: "balance"}
if self._client.name:
attributes[ATTR_BANK] = self._client.name
return attributes
@@ -253,16 +260,16 @@ class FinTsHoldingsAccount(Entity):
"""
attributes = {
ATTR_ACCOUNT: self._account.accountnumber,
ATTR_ACCOUNT_TYPE: 'holdings',
ATTR_ACCOUNT_TYPE: "holdings",
}
if self._client.name:
attributes[ATTR_BANK] = self._client.name
for holding in self._holdings:
total_name = '{} total'.format(holding.name)
total_name = "{} total".format(holding.name)
attributes[total_name] = holding.total_value
pieces_name = '{} pieces'.format(holding.name)
pieces_name = "{} pieces".format(holding.name)
attributes[pieces_name] = holding.pieces
price_name = '{} price'.format(holding.name)
price_name = "{} price".format(holding.name)
attributes[price_name] = holding.market_value
return attributes