Skip to content

Commit

Permalink
Merge pull request #26 from wxt9861/refactor/ynab
Browse files Browse the repository at this point in the history
refactor: Refactor code to better comply with pep8 style
  • Loading branch information
wxt9861 authored Mar 8, 2023
2 parents 0c15832 + cc3f0c5 commit 50752fc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ API:
Budget ID:
The budget ID is the combination between the slashes after the URL <https://app.youneedabudget.com>
If you only have one budget, you can omit the the budget option, if you have multiple budgets pick a budget you want the sensor to report on. At this time only 1 budget is retrieved.
If you only have one budget, you can omit the budget option, if you have multiple budgets pick a budget you want the sensor to report on. At this time only 1 budget is retrieved.
124 changes: 67 additions & 57 deletions custom_components/ynab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def async_setup(hass, config):
accounts = config[DOMAIN].get("accounts")
_LOGGER.debug("Monitoring accounts - %s", accounts)

hass.data[DOMAIN_DATA]["client"] = ynabData(hass, config)
hass.data[DOMAIN_DATA]["client"] = YnabData(hass, config)

# load platforms
for platform in PLATFORMS:
Expand All @@ -93,7 +93,7 @@ async def async_setup(hass, config):
return True


class ynabData:
class YnabData:
"""This class handles communication and data for YNAB integration."""

def __init__(self, hass, config):
Expand Down Expand Up @@ -146,7 +146,11 @@ async def update_data(self):

# get unapproved transactions
unapproved_transactions = len(
[t.amount for t in self.get_data.transactions if t.approved is not True]
[
transaction.amount
for transaction in self.get_data.transactions
if transaction.approved is not True
]
)
self.hass.data[DOMAIN_DATA]["need_approval"] = unapproved_transactions
_LOGGER.debug(
Expand All @@ -156,7 +160,11 @@ async def update_data(self):

# get number of uncleared transactions
uncleared_transactions = len(
[t.amount for t in self.get_data.transactions if t.cleared == "uncleared"]
[
transaction.amount
for transaction in self.get_data.transactions
if transaction.cleared == "uncleared"
]
)
self.hass.data[DOMAIN_DATA]["uncleared_transactions"] = uncleared_transactions
_LOGGER.debug(
Expand All @@ -165,9 +173,9 @@ async def update_data(self):

total_balance = 0
# get account data
for a in self.get_data.accounts:
if a.on_budget:
total_balance += a.balance
for account in self.get_data.accounts:
if account.on_budget:
total_balance += account.balance

# get to be budgeted data
self.hass.data[DOMAIN_DATA]["total_balance"] = total_balance / 1000
Expand All @@ -180,64 +188,66 @@ async def update_data(self):
for account in self.get_data.accounts:
if account.name not in self.accounts:
continue
else:
self.hass.data[DOMAIN_DATA].update(
[(account.name, account.balance / 1000)]
)
_LOGGER.debug(
"Received data for account: %s",
[account.name, account.balance / 1000],
)

self.hass.data[DOMAIN_DATA].update([(account.name, account.balance / 1000)])
_LOGGER.debug(
"Received data for account: %s",
[account.name, account.balance / 1000],
)

# get current month data
for m in self.get_data.months:
if m.month != date.today().strftime("%Y-%m-01"):
for month in self.get_data.months:
if month.month != date.today().strftime("%Y-%m-01"):
continue
else:
# budgeted
self.hass.data[DOMAIN_DATA]["budgeted_this_month"] = m.budgeted / 1000
_LOGGER.debug(
"Received data for: budgeted this month: %s",
self.hass.data[DOMAIN_DATA]["budgeted_this_month"],
)

# activity
self.hass.data[DOMAIN_DATA]["activity_this_month"] = m.activity / 1000
_LOGGER.debug(
"Received data for: activity this month: %s",
self.hass.data[DOMAIN_DATA]["activity_this_month"],
)
# budgeted
self.hass.data[DOMAIN_DATA]["budgeted_this_month"] = month.budgeted / 1000
_LOGGER.debug(
"Received data for: budgeted this month: %s",
self.hass.data[DOMAIN_DATA]["budgeted_this_month"],
)

# get age of money
self.hass.data[DOMAIN_DATA]["age_of_money"] = m.age_of_money
_LOGGER.debug(
"Received data for: age of money: %s",
self.hass.data[DOMAIN_DATA]["age_of_money"],
)
# activity
self.hass.data[DOMAIN_DATA]["activity_this_month"] = month.activity / 1000
_LOGGER.debug(
"Received data for: activity this month: %s",
self.hass.data[DOMAIN_DATA]["activity_this_month"],
)

# get number of overspend categories
overspent_categories = len(
[c.balance for c in m.categories if c.balance < 0]
# get age of money
self.hass.data[DOMAIN_DATA]["age_of_money"] = month.age_of_money
_LOGGER.debug(
"Received data for: age of money: %s",
self.hass.data[DOMAIN_DATA]["age_of_money"],
)

# get number of overspend categories
overspent_categories = len(
[
category.balance
for category in month.categories
if category.balance < 0
]
)
self.hass.data[DOMAIN_DATA]["overspent_categories"] = overspent_categories
_LOGGER.debug(
"Received data for: overspent categories: %s",
overspent_categories,
)

# get remaining category balances
for category in month.categories:
if category.name not in self.categories:
continue

self.hass.data[DOMAIN_DATA].update(
[(category.name, category.balance / 1000)]
)
self.hass.data[DOMAIN_DATA][
"overspent_categories"
] = overspent_categories
_LOGGER.debug(
"Received data for: overspent categories: %s",
overspent_categories,
"Received data for categories: %s",
[category.name, category.balance / 1000],
)

# get remaining category balances
for c in m.categories:
if c.name not in self.categories:
continue
else:
self.hass.data[DOMAIN_DATA].update([(c.name, c.balance / 1000)])
_LOGGER.debug(
"Received data for categories: %s",
[c.name, c.balance / 1000],
)


async def check_files(hass):
"""Return bool that indicates if all files are present."""
Expand All @@ -259,7 +269,7 @@ async def check_files(hass):

async def check_url():
"""Return bool that indicates YNAB URL is accessible."""
import aiohttp
import aiohttp # pylint: disable=import-outside-toplevel

url = "https://api.youneedabudget.com/v1"

Expand All @@ -275,7 +285,7 @@ async def check_url():
"but wasnt able to communicate with API endpoint"
)
result = False
except Exception as error:
except Exception as error: # pylint: disable=broad-except
_LOGGER.debug("Unable to establish connection with YNAB - %s", error)
result = False

Expand Down
2 changes: 1 addition & 1 deletion custom_components/ynab/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

PLATFORMS = ["sensor"]
REQUIRED_FILES = ["const.py", "manifest.json", "sensor.py"]
VERSION = "0.1.12"
VERSION = "0.1.13"
ISSUE_URL = "https://github.com/wxt9861/ynab/issues"

STARTUP = """
Expand Down
19 changes: 10 additions & 9 deletions custom_components/ynab/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"domain": "ynab",
"name": "ynab",
"documentation": "https://github.com/wxt9861/ynab",
"issue_tracker": "https://github.com/wxt9861/ynab/issues",
"dependencies": [],
"codeowners": ["@wxt9861"],
"requirements": ["ynab-sdk==0.4.0"],
"version": "0.1.12"
}
"domain": "ynab",
"name": "ynab",
"documentation": "https://github.com/wxt9861/ynab",
"issue_tracker": "https://github.com/wxt9861/ynab/issues",
"dependencies": [],
"codeowners": ["@wxt9861"],
"iot_class": "cloud_polling",
"requirements": ["ynab-sdk==0.4.0"],
"version": "0.1.13"
}
6 changes: 6 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ynab",
"content_in_root": false,
"homeassistant": "2021.12.0",
"render_readme": true
}
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ API:
Budget ID:
The budget ID is the combination between the slashes after the URL <https://app.youneedabudget.com>
If you only have one budget, you can omit the the budget option, if you have multiple budgets pick a budget you want the sensor to report on. At this time only 1 budget is retrieved.
If you only have one budget, you can omit the budget option, if you have multiple budgets pick a budget you want the sensor to report on. At this time only 1 budget is retrieved.

0 comments on commit 50752fc

Please sign in to comment.