From b1850f6b05246434d26a7fed19c5a90b4a1f40b1 Mon Sep 17 00:00:00 2001 From: soloam <11949987+soloam@users.noreply.github.com> Date: Fri, 3 May 2024 11:55:41 +0100 Subject: [PATCH] Add new pull dates in the past --- .../fireflyiii_integration/binary_sensor.py | 37 ++++++++++++++ .../integrations/fireflyiii_config.py | 43 ++++++++++++++-- .../integrations/fireflyiii_coordinator.py | 50 ++++++++++++------- .../fireflyiii_integration/sensor.py | 1 + .../translations/en.json | 10 ++-- 5 files changed, 118 insertions(+), 23 deletions(-) diff --git a/custom_components/fireflyiii_integration/binary_sensor.py b/custom_components/fireflyiii_integration/binary_sensor.py index 5cf0746..0a8f028 100644 --- a/custom_components/fireflyiii_integration/binary_sensor.py +++ b/custom_components/fireflyiii_integration/binary_sensor.py @@ -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 @@ -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 diff --git a/custom_components/fireflyiii_integration/integrations/fireflyiii_config.py b/custom_components/fireflyiii_integration/integrations/fireflyiii_config.py index ca9cdb1..64ff081 100644 --- a/custom_components/fireflyiii_integration/integrations/fireflyiii_config.py +++ b/custom_components/fireflyiii_integration/integrations/fireflyiii_config.py @@ -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"] @@ -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, @@ -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""" @@ -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()) diff --git a/custom_components/fireflyiii_integration/integrations/fireflyiii_coordinator.py b/custom_components/fireflyiii_integration/integrations/fireflyiii_coordinator.py index a333b3b..bf3c65e 100644 --- a/custom_components/fireflyiii_integration/integrations/fireflyiii_coordinator.py +++ b/custom_components/fireflyiii_integration/integrations/fireflyiii_coordinator.py @@ -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, @@ -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) @@ -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( @@ -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) @@ -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 ) @@ -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"] diff --git a/custom_components/fireflyiii_integration/sensor.py b/custom_components/fireflyiii_integration/sensor.py index f766007..20fc6c5 100644 --- a/custom_components/fireflyiii_integration/sensor.py +++ b/custom_components/fireflyiii_integration/sensor.py @@ -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, diff --git a/custom_components/fireflyiii_integration/translations/en.json b/custom_components/fireflyiii_integration/translations/en.json index 9a830fe..9ba7902 100644 --- a/custom_components/fireflyiii_integration/translations/en.json +++ b/custom_components/fireflyiii_integration/translations/en.json @@ -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" }