This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
forked from tormjens/home-assistant-sbanken
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from toringer/update_entities
Force update of accounts after doing a transfer
- Loading branch information
Showing
10 changed files
with
274 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
""" Sbanken entities""" | ||
import logging | ||
from datetime import datetime | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity import DeviceInfo | ||
from homeassistant.components.sensor import ( | ||
SensorDeviceClass, | ||
SensorStateClass, | ||
) | ||
from homeassistant.helpers.entity import Entity | ||
from .sbanken_api import SbankenApi | ||
from .const import ( | ||
ATTR_ACCOUNT_ID, | ||
ATTR_ACCOUNT_LIMIT, | ||
ATTR_ACCOUNT_NUMBER, | ||
ATTR_ACCOUNT_TYPE, | ||
ATTR_AVAILABLE, | ||
ATTR_BALANCE, | ||
ATTR_LAST_UPDATE, | ||
ATTR_NAME, | ||
ATTR_PAYMENTS, | ||
ATTR_STANDING_ORDERS, | ||
ATTR_TRANSACTIONS, | ||
DOMAIN, | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class SbankenAccountSensor(Entity): | ||
"""Representation of a Sensor.""" | ||
|
||
def __init__( | ||
self, | ||
account, | ||
api: SbankenApi, | ||
number_of_transactions: int, | ||
hass: HomeAssistant, | ||
customer_info, | ||
) -> None: | ||
"""Initialize the sensor.""" | ||
self.api = api | ||
self.number_of_transactions = number_of_transactions | ||
self.hass = hass | ||
self.customer_info = customer_info | ||
self._account = account | ||
self._account_id = account["accountId"] | ||
self._transactions = [] | ||
self._payments = [] | ||
self._standing_orders = [] | ||
self._state = float(account["available"]) | ||
self._attr_state_class = SensorStateClass.MEASUREMENT | ||
self._attr_device_class = SensorDeviceClass.MONETARY | ||
self._attr_unique_id = self._account["accountNumber"] | ||
self._attr_name = f"{self._account['name']} ({self._account['accountNumber']})" | ||
self._attr_unit_of_measurement = "NOK" | ||
self._attr_icon = "mdi:cash" | ||
|
||
@property | ||
def device_info(self): | ||
"""Return the device_info of the device.""" | ||
device_info = DeviceInfo( | ||
identifiers={(DOMAIN, self.customer_info["customerId"])}, | ||
name=f"Sbanken: {self.customer_info['firstName']} {self.customer_info['lastName']}", | ||
manufacturer="Sbanken", | ||
configuration_url="https://sbanken.no/", | ||
) | ||
return device_info | ||
|
||
@property | ||
def state(self) -> float: | ||
"""Return the state of the sensor.""" | ||
return self._state | ||
|
||
@property | ||
def extra_state_attributes(self): | ||
"""Return the state attributes.""" | ||
return { | ||
ATTR_ACCOUNT_ID: self._account_id, | ||
ATTR_AVAILABLE: self._account["available"], | ||
ATTR_BALANCE: self._account["balance"], | ||
ATTR_ACCOUNT_NUMBER: self._account["accountNumber"], | ||
ATTR_NAME: self._account["name"], | ||
ATTR_ACCOUNT_TYPE: self._account["accountType"], | ||
ATTR_ACCOUNT_LIMIT: self._account["creditLimit"], | ||
ATTR_LAST_UPDATE: datetime.now().strftime("%d/%m/%Y %H:%M:%S"), | ||
ATTR_TRANSACTIONS: self._transactions, | ||
ATTR_PAYMENTS: self._payments, | ||
ATTR_STANDING_ORDERS: self._standing_orders, | ||
} | ||
|
||
async def async_update(self): | ||
"""Fetch new state data for the sensor. | ||
This is the only method that should fetch new data for Home Assistant. | ||
""" | ||
account = await self.hass.async_add_executor_job( | ||
self.api.get_account, self._account_id | ||
) | ||
transactions = await self.hass.async_add_executor_job( | ||
self.api.get_transactions, | ||
self._account_id, | ||
self.number_of_transactions, | ||
) | ||
payments = await self.hass.async_add_executor_job( | ||
self.api.get_payments, | ||
self._account_id, | ||
self.number_of_transactions, | ||
) | ||
|
||
standing_orders = await self.hass.async_add_executor_job( | ||
self.api.get_standing_orders, self._account_id | ||
) | ||
|
||
self._transactions = transactions | ||
self._payments = payments | ||
self._account = account | ||
self._standing_orders = standing_orders | ||
self._state = float(account["available"]) | ||
_LOGGER.debug(f"Updating Sbanken Sensors: {self._attr_name}") | ||
|
||
|
||
class CustomerInformationSensor(Entity): | ||
"""Representation of a Sensor.""" | ||
|
||
def __init__(self, api: SbankenApi, hass: HomeAssistant, customer_info) -> None: | ||
"""Initialize the sensor.""" | ||
self.api = api | ||
self.hass = hass | ||
self.customer_info = customer_info | ||
self._state = ( | ||
f"{self.customer_info['firstName']} {self.customer_info['lastName']}" | ||
) | ||
self._attr_unique_id = self.customer_info["customerId"] | ||
self._attr_name = "Customer information" | ||
self._attr_icon = "mdi:information-outline" | ||
|
||
@property | ||
def device_info(self): | ||
"""Return the device_info of the device.""" | ||
device_info = DeviceInfo( | ||
identifiers={(DOMAIN, self.customer_info["customerId"])}, | ||
name=f"Sbanken: {self.customer_info['firstName']} {self.customer_info['lastName']}", | ||
manufacturer="Sbanken", | ||
configuration_url="https://sbanken.no/", | ||
) | ||
return device_info | ||
|
||
@property | ||
def state(self): | ||
"""Return the state of the sensor.""" | ||
return self._state | ||
|
||
@property | ||
def extra_state_attributes(self): | ||
"""Return the state attributes.""" | ||
return { | ||
"customerId": self.customer_info["customerId"], | ||
"firstName": self.customer_info["firstName"], | ||
"lastName": self.customer_info["lastName"], | ||
"emailAddress": self.customer_info["emailAddress"], | ||
"dateOfBirth": self.customer_info["dateOfBirth"], | ||
"postalAddress": self.customer_info["postalAddress"], | ||
"streetAddress": self.customer_info["streetAddress"], | ||
"phoneNumbers": self.customer_info["phoneNumbers"], | ||
} | ||
|
||
async def async_update(self): | ||
"""Fetch new state data for the sensor. | ||
This is the only method that should fetch new data for Home Assistant. | ||
""" | ||
self.customer_info = await self.hass.async_add_executor_job( | ||
self.api.get_customer_information | ||
) | ||
_LOGGER.debug(f"Updating Sbanken Sensors: {self._attr_name}") |
Oops, something went wrong.