core/homeassistant/components/starlingbank/sensor.py

103 lines
3.2 KiB
Python
Raw Normal View History

"""Support for balance data via the Starling Bank API."""
import logging
import requests
from starlingbank import StarlingAccount
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_NAME
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
BALANCE_TYPES = ["cleared_balance", "effective_balance"]
2019-07-31 19:25:30 +00:00
CONF_ACCOUNTS = "accounts"
CONF_BALANCE_TYPES = "balance_types"
CONF_SANDBOX = "sandbox"
DEFAULT_SANDBOX = False
2019-07-31 19:25:30 +00:00
DEFAULT_ACCOUNT_NAME = "Starling"
2019-07-31 19:25:30 +00:00
ICON = "mdi:currency-gbp"
2019-07-31 19:25:30 +00:00
ACCOUNT_SCHEMA = vol.Schema(
{
vol.Required(CONF_ACCESS_TOKEN): cv.string,
vol.Optional(CONF_BALANCE_TYPES, default=BALANCE_TYPES): vol.All(
cv.ensure_list, [vol.In(BALANCE_TYPES)]
),
vol.Optional(CONF_NAME, default=DEFAULT_ACCOUNT_NAME): cv.string,
vol.Optional(CONF_SANDBOX, default=DEFAULT_SANDBOX): cv.boolean,
}
)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_ACCOUNTS): vol.Schema([ACCOUNT_SCHEMA])}
)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Sterling Bank sensor platform."""
sensors = []
for account in config[CONF_ACCOUNTS]:
try:
starling_account = StarlingAccount(
2019-07-31 19:25:30 +00:00
account[CONF_ACCESS_TOKEN], sandbox=account[CONF_SANDBOX]
)
for balance_type in account[CONF_BALANCE_TYPES]:
2019-07-31 19:25:30 +00:00
sensors.append(
StarlingBalanceSensor(
starling_account, account[CONF_NAME], balance_type
)
)
except requests.exceptions.HTTPError as error:
_LOGGER.error(
2019-07-31 19:25:30 +00:00
"Unable to set up Starling account '%s': %s", account[CONF_NAME], error
)
add_devices(sensors, True)
class StarlingBalanceSensor(SensorEntity):
"""Representation of a Starling balance sensor."""
def __init__(self, starling_account, account_name, balance_data_type):
"""Initialize the sensor."""
self._starling_account = starling_account
self._balance_data_type = balance_data_type
self._state = None
self._account_name = account_name
@property
def name(self):
"""Return the name of the sensor."""
return "{} {}".format(
2019-07-31 19:25:30 +00:00
self._account_name, self._balance_data_type.replace("_", " ").capitalize()
)
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return self._starling_account.currency
@property
def icon(self):
"""Return the entity icon."""
return ICON
def update(self):
"""Fetch new state data for the sensor."""
self._starling_account.update_balance_data()
2019-07-31 19:25:30 +00:00
if self._balance_data_type == "cleared_balance":
self._state = self._starling_account.cleared_balance / 100
2019-07-31 19:25:30 +00:00
elif self._balance_data_type == "effective_balance":
self._state = self._starling_account.effective_balance / 100