diff --git a/README.md b/README.md index 9926254..1926592 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Version](https://img.shields.io/badge/version-0.0.6-green.svg?style=for-the-badge)](#) [![mantained](https://img.shields.io/maintenance/yes/2019.svg?style=for-the-badge)](#) +[![Version](https://img.shields.io/badge/version-0.0.7-green.svg?style=for-the-badge)](#) [![mantained](https://img.shields.io/maintenance/yes/2019.svg?style=for-the-badge)](#) [![maintainer](https://img.shields.io/badge/maintainer-Ian%20Richardson%20%40iantrich-blue.svg?style=for-the-badge)](#) diff --git a/custom_components/sensor/personalcapital.py b/custom_components/sensor/personalcapital.py index bd380ba..501181e 100644 --- a/custom_components/sensor/personalcapital.py +++ b/custom_components/sensor/personalcapital.py @@ -8,13 +8,14 @@ import logging import voluptuous as vol import json +import time from datetime import timedelta from homeassistant.helpers.entity import Entity import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import (PLATFORM_SCHEMA) from homeassistant.util import Throttle -__version__ = '0.0.6' +__version__ = '0.0.7' REQUIREMENTS = ['personalcapital==1.0.1'] @@ -101,7 +102,7 @@ def personalcapital_configuration_callback(data): def load_session(hass): try: - with open(hass.config.path(SESSION_FILE)) as data_file: + with open(hass.config.path(SESSION_FILE)) as data_file: cookies = {} try: cookies = json.load(data_file) @@ -177,7 +178,6 @@ def update(self): """Get the latest state of the sensor.""" self._rest.update() data = self._rest.data.json()['spData'] - _LOGGER.error(data) self._state = data.get('networth', 0.0) self._networth = data.get('networth', 0.0) self._assets = data.get('assets', 0.0) @@ -248,7 +248,7 @@ def __init__(self, hass, rest, unit_of_measurement, sensor_type): self._balanceName = SENSOR_TYPES[sensor_type][2] self._state = None self._unit_of_measurement = unit_of_measurement - self.hass.data[self._productType] = {} + self.hass.data[self._productType] = {'accounts': []} def update(self): """Get the latest state of the sensor.""" @@ -259,7 +259,7 @@ def update(self): for account in accounts: if self._productType == account.get('productType') and account.get('closeDate', '') == '': - self.hass.data[self._productType][account.get('name', '')] = { + self.hass.data[self._productType].get('accounts').append({ "name": account.get('name', ''), "firm_name": account.get('firmName', ''), "logo": account.get('logoPath', ''), @@ -267,7 +267,8 @@ def update(self): "account_type": account.get('accountType', ''), "url": account.get('homeUrl', ''), "currency": account.get('currency', ''), - } + "refreshed": how_long_ago(account.get('lastRefreshed', 0)) + ' ago', + }) @property def name(self): @@ -311,3 +312,18 @@ def update(self): if not self.data or not self.data.json()['spHeader']['success']: self._pc.login(self._config[CONF_EMAIL], self._config[CONF_PASSWORD]) self.data = self._pc.fetch('/newaccount/getAccounts') + + +def how_long_ago(last_epoch): + a = last_epoch + b = time.time() + c = b - a + days = c // 86400 + hours = c // 3600 % 24 + minutes = c // 60 % 60 + + if minutes < 60: + return str(round(minutes)) + ' minutes' + if hours < 24: + return str(round(hours)) + ' hours' + return str(round(days)) + ' days'