Skip to content

Commit

Permalink
💵 capture refreshed time of account
Browse files Browse the repository at this point in the history
restructure account attributes for Lovelace card usage
  • Loading branch information
iantrich committed Jan 12, 2019
1 parent e0123ad commit 8ab1175
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)](#)

Expand Down
28 changes: 22 additions & 6 deletions custom_components/sensor/personalcapital.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."""
Expand All @@ -259,15 +259,16 @@ 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', ''),
"balance": account.get('balance', 0.0),
"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):
Expand Down Expand Up @@ -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'

0 comments on commit 8ab1175

Please sign in to comment.