Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/birdwing/ynab
Browse files Browse the repository at this point in the history
  • Loading branch information
birdwing committed Jan 28, 2025
2 parents 4b431d7 + 4a25775 commit 3fe28bd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
8 changes: 6 additions & 2 deletions custom_components/ynab/api/data_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
CONF_CURRENCY_KEY,
CONF_BUDGET_KEY,
CONF_CATEGORIES_KEY,
CONF_CATEGORIES_ALL,
CONF_ACCOUNTS_KEY,
CONF_ACCOUNTS_ALL,
DEFAULT_API_ENDPOINT,
DOMAIN
)
Expand Down Expand Up @@ -61,7 +63,9 @@ def __init__(self, hass, config):
self.api_key = config[CONF_API_KEY]
self.budget = config[CONF_BUDGET_KEY]
self.categories = config[CONF_CATEGORIES_KEY]
self.categories_all = config[CONF_CATEGORIES_ALL]
self.accounts = config[CONF_ACCOUNTS_KEY]
self.accounts_all = config[CONF_ACCOUNTS_ALL]


async def _async_update_data(self):
Expand Down Expand Up @@ -128,7 +132,7 @@ async def _async_update_data(self):
# get accounts
accounts: dict[str, AccountModel] = {}
for account in get_data.accounts:
if account.id not in self.accounts:
if not self.accounts_all and account.id not in self.accounts:
continue

accounts.update([(account.id, AccountModel(account.name, account.balance / 1000))])
Expand Down Expand Up @@ -179,7 +183,7 @@ async def _async_update_data(self):
# get remaining category balances
categories: dict[str, CategoryModel] = {}
for category in month.categories:
if category.id not in self.categories:
if not self.categories_all and category.id not in self.categories:
continue

categories.update(
Expand Down
6 changes: 6 additions & 0 deletions custom_components/ynab/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import voluptuous as vol
from .const import (
CONF_ACCOUNTS_KEY,
CONF_ACCOUNTS_ALL,
CONF_BUDGET_KEY,
CONF_CATEGORIES_KEY,
CONF_CATEGORIES_ALL,
CONF_CURRENCY_KEY,
DOMAIN
)
Expand Down Expand Up @@ -77,6 +79,7 @@ async def async_step_budgets(self, user_input=None):

async def async_step_categories(self, user_input=None):
if user_input is not None:
self.data[CONF_CATEGORIES_ALL] = user_input[CONF_CATEGORIES_ALL]
if CONF_CATEGORIES_KEY in user_input:
self.data[CONF_CATEGORIES_KEY] = user_input[CONF_CATEGORIES_KEY]
else:
Expand All @@ -87,6 +90,7 @@ async def async_step_categories(self, user_input=None):
categories_by_name = await self.fetch_categories()

data_schema = {
vol.Required(CONF_CATEGORIES_ALL): bool,
vol.Optional(CONF_CATEGORIES_KEY): selector({
"select": {
"options": [{"label": name, "value": category.id} for name, category in categories_by_name.items()],
Expand All @@ -111,6 +115,7 @@ async def fetch_categories(self):

async def async_step_accounts(self, user_input=None):
if user_input is not None:
self.data[CONF_ACCOUNTS_ALL] = user_input[CONF_ACCOUNTS_ALL]
if CONF_ACCOUNTS_KEY in user_input:
self.data[CONF_ACCOUNTS_KEY] = user_input[CONF_ACCOUNTS_KEY]
else:
Expand All @@ -124,6 +129,7 @@ async def async_step_accounts(self, user_input=None):
accounts_response = await self.hass.async_add_executor_job(self.ynab.accounts.get_accounts, self.data["budget"])

data_schema = {
vol.Required(CONF_ACCOUNTS_ALL): bool,
vol.Optional(CONF_ACCOUNTS_KEY): selector({
"select": {
"options": [{"label": account.name, "value": account.id} for account in accounts_response.data.accounts],
Expand Down
2 changes: 2 additions & 0 deletions custom_components/ynab/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
CONF_BUDGET_KEY = "budget"
CONF_BUDGET_NAME_KEY = "budget_name"
CONF_CATEGORIES_KEY = "categories"
CONF_CATEGORIES_ALL = "categories_all"
CONF_ACCOUNTS_KEY = "accounts"
CONF_ACCOUNTS_ALL = "accounts_all"
CONF_CURRENCY_KEY = "currency"
12 changes: 8 additions & 4 deletions custom_components/ynab/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import logging
from homeassistant.helpers.entity import DeviceInfo

from .const import (DOMAIN, CONF_ACCOUNTS_KEY,
from .const import (DOMAIN, CONF_ACCOUNTS_KEY, CONF_ACCOUNTS_ALL,
CONF_BUDGET_KEY, CONF_CATEGORIES_KEY,
CONF_BUDGET_NAME_KEY)
CONF_CATEGORIES_ALL, CONF_BUDGET_NAME_KEY)

from .sensors.balance_sensor import CategorySensor, AccountSensor
from .sensors.budget_sensor import BudgetSensor
Expand Down Expand Up @@ -35,14 +35,18 @@ async def async_setup_entry(
sensors = [BudgetSensor(coordinator, budget_id, budget_name, device_info)]

categories = []
if CONF_CATEGORIES_KEY in config_entry.data:
if config_entry.data[CONF_CATEGORIES_ALL]:
categories = coordinator.data.categories.keys()
elif CONF_CATEGORIES_KEY in config_entry.data:
categories = config_entry.data[CONF_CATEGORIES_KEY]

for category in categories:
sensors.append(CategorySensor(coordinator, category_id=category, device_info=device_info, budget_name=budget_name))

accounts = []
if CONF_ACCOUNTS_KEY in config_entry.data:
if config_entry.data[CONF_ACCOUNTS_ALL]:
accounts = coordinator.data.accounts.keys()
elif CONF_ACCOUNTS_KEY in config_entry.data:
accounts = config_entry.data[CONF_ACCOUNTS_KEY]

for account in accounts:
Expand Down

0 comments on commit 3fe28bd

Please sign in to comment.