Skip to content

Commit

Permalink
Add new pull dates in the past
Browse files Browse the repository at this point in the history
  • Loading branch information
soloam committed May 3, 2024
1 parent cd166e7 commit b1850f6
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 23 deletions.
37 changes: 37 additions & 0 deletions custom_components/fireflyiii_integration/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import annotations

import logging
from datetime import datetime
from typing import Any, Mapping, Optional

from homeassistant import config_entries, core
from homeassistant.components.binary_sensor import BinarySensorEntity
Expand Down Expand Up @@ -54,3 +56,38 @@ def __init__(
def is_on(self) -> bool:
"""Return if connected."""
return self.coordinator.last_update_success

@property
def start_date(self) -> Optional[datetime]:
"""Import Start Date Time"""
timerange = self.coordinator.timerange
if not timerange:
return None

return timerange.start_datetime

@property
def end_date(self) -> Optional[datetime]:
"""Import End Date Time"""
timerange = self.coordinator.timerange
if not timerange:
return None

return timerange.end_datetime

@property
def extra_state_attributes(self) -> Optional[Mapping[str, Any]]:
"""Return entity specific state attributes."""

attributes = ["start_date", "end_date"]

state_attr = {}

for attr in attributes:
try:
if getattr(self, attr) is not None:
state_attr[attr] = getattr(self, attr)
except AttributeError:
continue

return state_attr
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@
CONF_RETURN_PIGGY_BANKS = "return_piggy_banks"
CONF_RETURN_RANGE = "return_range"
CONF_RETURN_RANGE_DAY_TYPE = "day"
CONF_RETURN_RANGE_LAST_MONTH_TYPE = "last_month"
CONF_RETURN_RANGE_LAST_WEEK_TYPE = "last_week"
CONF_RETURN_RANGE_LAST_YEAR_TYPE = "last_year"
CONF_RETURN_RANGE_LASTX_TYPE = "lastx"
CONF_RETURN_RANGE_MONTH_TYPE = "month"
CONF_RETURN_RANGE_WEEK_TYPE = "week"
CONF_RETURN_RANGE_YEAR_TYPE = "year"
CONF_RETURN_RANGE_YESTERDAY_TYPE = "yesterday"

CONF_NAME_DEFAULT = "FireflyIII"
CONF_RETURN_ACCOUNT_TYPE_DEFAULT = ["asset"]
Expand All @@ -64,6 +68,10 @@
]

CONF_RETURN_RANGE_TYPES = [
CONF_RETURN_RANGE_LAST_YEAR_TYPE,
CONF_RETURN_RANGE_LAST_MONTH_TYPE,
CONF_RETURN_RANGE_LAST_WEEK_TYPE,
CONF_RETURN_RANGE_YESTERDAY_TYPE,
CONF_RETURN_RANGE_YEAR_TYPE,
CONF_RETURN_RANGE_MONTH_TYPE,
CONF_RETURN_RANGE_WEEK_TYPE,
Expand Down Expand Up @@ -194,21 +202,41 @@ def is_date_range_year(self) -> bool:
"""Firefly config is ranged in years - flag"""
return self.date_range_type == CONF_RETURN_RANGE_YEAR_TYPE

@property
def is_date_range_last_year(self) -> bool:
"""Firefly config is ranged in years - flag"""
return self.date_range_type == CONF_RETURN_RANGE_LAST_YEAR_TYPE

@property
def is_date_range_month(self) -> bool:
"""Firefly config is ranged in months - flag"""
return self.date_range_type == CONF_RETURN_RANGE_MONTH_TYPE

@property
def is_date_range_last_month(self) -> bool:
"""Firefly config is ranged in months - flag"""
return self.date_range_type == CONF_RETURN_RANGE_LAST_MONTH_TYPE

@property
def is_date_range_week(self) -> bool:
"""Firefly config is ranged in weeks - flag"""
return self.date_range_type == CONF_RETURN_RANGE_WEEK_TYPE

@property
def is_date_range_last_week(self) -> bool:
"""Firefly config is ranged in weeks - flag"""
return self.date_range_type == CONF_RETURN_RANGE_LAST_WEEK_TYPE

@property
def is_date_range_day(self) -> bool:
"""Firefly config is ranged in days - flag"""
return self.date_range_type == CONF_RETURN_RANGE_DAY_TYPE

@property
def is_date_range_yesterday(self) -> bool:
"""Firefly config is ranged in days - flag"""
return self.date_range_type == CONF_RETURN_RANGE_YESTERDAY_TYPE

@property
def is_date_range_lastx(self) -> bool:
"""Firefly config is ranged in custum - flag"""
Expand Down Expand Up @@ -573,11 +601,20 @@ def schema_config(cls):
schema.update(cls.account_types())
schema.update(cls.account_ids())

if cls.data_source().is_date_range_year:
if (
cls.data_source().is_date_range_year
or cls.data_source().is_date_range_last_year
):
schema.update(cls.year_start())
elif cls.data_source().is_date_range_month:
elif (
cls.data_source().is_date_range_month
or cls.data_source().is_date_range_last_month
):
schema.update(cls.month_start())
elif cls.data_source().is_date_range_week:
elif (
cls.data_source().is_date_range_week
or cls.data_source().is_date_range_last_week
):
schema.update(cls.week_start())
elif cls.data_source().is_date_range_lastx:
schema.update(cls.lastx_time())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ def timerange(self) -> DateTimeRange | None:

timerange = None

if self.user_data.is_date_range_year:
reference = datetime.today()

if self.user_data.is_date_range_year or self.user_data.is_date_range_last_year:
if self.user_data.is_date_range_last_year:
reference = reference.replace(year=reference.year - 1)

year_start_date_str = self.user_data.year_start

try:
year_start_date = datetime.strptime(year_start_date_str, "%Y-%m-%d")
except ValueError:
year_start_date = datetime.today().replace(
year=datetime.today().year,
year_start_date = reference.replace(
year=reference.year,
month=1,
day=1,
hour=0,
Expand All @@ -95,7 +100,7 @@ def timerange(self) -> DateTimeRange | None:
microsecond=0,
)

if year_start_date > datetime.today():
if year_start_date > reference:
year_end_date = year_start_date - timedelta(microseconds=1)

year_start_date = year_start_date.replace(year=year_start_date.year - 1)
Expand All @@ -106,12 +111,19 @@ def timerange(self) -> DateTimeRange | None:

timerange = DateTimeRange(year_start_date, year_end_date)

elif self.user_data.is_date_range_month:
elif (
self.user_data.is_date_range_month
or self.user_data.is_date_range_last_month
):
if self.user_data.is_date_range_last_month:
last_month = reference.replace(day=1) - timedelta(days=1)
reference = self._set_day_bound(last_month, reference.day)

month_start_day = self.user_data.month_start

date_ref = self._set_day_bound(datetime.today(), month_start_day)
date_ref = self._set_day_bound(reference, month_start_day)

if date_ref > datetime.today():
if date_ref > reference:
date_start = date_ref - timedelta(days=date_ref.day + 7)
date_start = self._set_day_bound(date_start, month_start_day)
date_end = self._set_day_bound(date_ref, (month_start_day - 1)).replace(
Expand All @@ -125,12 +137,15 @@ def timerange(self) -> DateTimeRange | None:
)

timerange = DateTimeRange(date_start, date_end)
elif self.user_data.is_date_range_week:
elif (
self.user_data.is_date_range_week or self.user_data.is_date_range_last_week
):
if self.user_data.is_date_range_last_week:
reference = reference - timedelta(weeks=1)

week_start_day_str = self.user_data.week_start

date_ref = datetime.today().replace(
hour=0, minute=0, second=0, microsecond=0
)
date_ref = reference.replace(hour=0, minute=0, second=0, microsecond=0)

try:
week_start_day = WEEKDAYS.index(week_start_day_str)
Expand All @@ -142,11 +157,14 @@ def timerange(self) -> DateTimeRange | None:
week_end_date = week_end_date - timedelta(microseconds=1)

timerange = DateTimeRange(week_start_date, week_end_date)
elif self.user_data.is_date_range_day:
day_start_date = datetime.today().replace(
elif self.user_data.is_date_range_day or self.user_data.is_date_range_yesterday:
if self.user_data.is_date_range_last_week:
reference = reference - timedelta(days=1)

day_start_date = reference.replace(
hour=0, minute=0, second=0, microsecond=0
)
day_end_date = datetime.today().replace(
day_end_date = reference.replace(
hour=23, minute=59, second=59, microsecond=59
)

Expand All @@ -166,9 +184,7 @@ def timerange(self) -> DateTimeRange | None:
elif self.user_data.is_lastx_days_type_days:
keys_date["days"] = lastx_number

date_ref = datetime.today().replace(
hour=0, minute=0, second=0, microsecond=0
)
date_ref = reference.replace(hour=0, minute=0, second=0, microsecond=0)

lastx_start_date = date_ref - timedelta(
days=keys_date["days"], weeks=keys_date["weeks"]
Expand Down
1 change: 1 addition & 0 deletions custom_components/fireflyiii_integration/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from homeassistant import config_entries, core
from homeassistant.components.sensor import SensorEntity
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import (
COORDINATOR,
Expand Down
10 changes: 7 additions & 3 deletions custom_components/fireflyiii_integration/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@
},
"return_range": {
"options": {
"year": "Last year",
"month": "Last month",
"week": "Last week",
"last_year": "Last year",
"last_month": "Last month",
"last_week": "Last week",
"yesterday": "Yesterday",
"year": "This year",
"month": "This month",
"week": "This week",
"day": "Today",
"lastx": "Custom"
}
Expand Down

0 comments on commit b1850f6

Please sign in to comment.