From f8390a4b0844272fb2dcd7c89a680d3231f68dd7 Mon Sep 17 00:00:00 2001 From: yurii Date: Sun, 14 Jul 2024 10:48:34 +0300 Subject: [PATCH] Ignore tech folders fix formatting --- .gitignore | 2 + custom_components/loe_outages/__init__.py | 2 +- custom_components/loe_outages/api.py | 10 ++--- custom_components/loe_outages/calendar.py | 8 ++-- custom_components/loe_outages/config_flow.py | 2 +- custom_components/loe_outages/coordinator.py | 10 ++--- custom_components/loe_outages/entity.py | 2 +- custom_components/loe_outages/models.py | 40 +++++++++++--------- custom_components/loe_outages/sensor.py | 2 +- 9 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3cca82 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +custom_components/loe_outages/__pycache__ +config diff --git a/custom_components/loe_outages/__init__.py b/custom_components/loe_outages/__init__.py index 774fbe2..0db8186 100644 --- a/custom_components/loe_outages/__init__.py +++ b/custom_components/loe_outages/__init__.py @@ -38,4 +38,4 @@ async def async_unload_entry( """Handle removal of an entry.""" LOGGER.info("Unload entry: %s", entry) """Unload a config entry.""" - return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) \ No newline at end of file + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/custom_components/loe_outages/api.py b/custom_components/loe_outages/api.py index 2ddf33c..d1192c6 100644 --- a/custom_components/loe_outages/api.py +++ b/custom_components/loe_outages/api.py @@ -48,12 +48,12 @@ def get_current_event(self, at: datetime) -> dict: """Get the current event.""" if not self.schedule: return None - + twoDaysBefore = datetime.datetime.now() + datetime.timedelta(days=-2) for schedule in reversed(self.schedule): if schedule.date < twoDaysBefore.astimezone(pytz.UTC): return None - + events_at = schedule.get_current_event(self.group, at) if not events_at: return None @@ -67,7 +67,7 @@ def get_events( """Get all events.""" if not self.schedule: return [] - + result = [] twoDaysBeforeStart = start_date + datetime.timedelta(days=-2) for schedule in reversed(self.schedule): @@ -76,7 +76,5 @@ def get_events( for interval in schedule.between(self.group, start_date, end_date): result.append(interval) - + return result - - \ No newline at end of file diff --git a/custom_components/loe_outages/calendar.py b/custom_components/loe_outages/calendar.py index a685fa0..f38265b 100644 --- a/custom_components/loe_outages/calendar.py +++ b/custom_components/loe_outages/calendar.py @@ -69,14 +69,14 @@ async def async_get_events( """Return calendar events within a datetime range.""" LOGGER.debug('Getting all events between "%s" -> "%s"', start_date, end_date) intervals = self.coordinator.get_events_between(start_date, end_date) - events = [] - + events = [] + for interval in intervals: - event = CalendarEvent( + event = CalendarEvent( summary=interval.state, start=interval.startTime, end=interval.endTime, description=interval.state, ) events.append(event) - return events \ No newline at end of file + return events diff --git a/custom_components/loe_outages/config_flow.py b/custom_components/loe_outages/config_flow.py index c93caf1..63c839e 100644 --- a/custom_components/loe_outages/config_flow.py +++ b/custom_components/loe_outages/config_flow.py @@ -90,4 +90,4 @@ async def async_step_user(self, user_input: dict | None = None) -> ConfigFlowRes return self.async_show_form( step_id="user", data_schema=build_schema(config_entry=None), - ) \ No newline at end of file + ) diff --git a/custom_components/loe_outages/coordinator.py b/custom_components/loe_outages/coordinator.py index e03e537..4993957 100644 --- a/custom_components/loe_outages/coordinator.py +++ b/custom_components/loe_outages/coordinator.py @@ -53,7 +53,7 @@ def event_name_map(self) -> dict: """Return a mapping of event names to translations.""" return { STATE_OFF: self.translations.get(TRANSLATION_KEY_EVENT_OFF), - STATE_ON: self.translations.get(TRANSLATION_KEY_EVENT_ON) + STATE_ON: self.translations.get(TRANSLATION_KEY_EVENT_ON), } async def update_config( @@ -166,10 +166,10 @@ def _get_calendar_event( if not event: return None - event_summary = event['state'] + event_summary = event["state"] translated_summary = self.event_name_map.get(event_summary) - event_start = event['startTime'] - event_end = event['endTime'] + event_start = event["startTime"] + event_end = event["endTime"] LOGGER.debug( "Transforming event: %s (%s -> %s)", @@ -190,4 +190,4 @@ def _event_to_state(self, event: Interval | None) -> str: STATE_ON: STATE_ON, STATE_OFF: STATE_OFF, None: STATE_ON, - }[state] \ No newline at end of file + }[state] diff --git a/custom_components/loe_outages/entity.py b/custom_components/loe_outages/entity.py index 9719140..fff3baf 100644 --- a/custom_components/loe_outages/entity.py +++ b/custom_components/loe_outages/entity.py @@ -21,4 +21,4 @@ def device_info(self) -> DeviceInfo: identifiers={(DOMAIN, self.coordinator.config_entry.entry_id)}, manufacturer="Loe", entry_type=DeviceEntryType.SERVICE, - ) \ No newline at end of file + ) diff --git a/custom_components/loe_outages/models.py b/custom_components/loe_outages/models.py index f111d92..e0c4fc7 100644 --- a/custom_components/loe_outages/models.py +++ b/custom_components/loe_outages/models.py @@ -3,7 +3,8 @@ from dateutil import parser from typing import List -utc=pytz.UTC +utc = pytz.UTC + class Interval: def __init__(self, state: str, startTime: str, endTime: str): @@ -12,7 +13,7 @@ def __init__(self, state: str, startTime: str, endTime: str): self.endTime = endTime @staticmethod - def from_dict(obj: dict) -> 'Interval': + def from_dict(obj: dict) -> "Interval": return Interval( state=obj.get("state"), startTime=parser.parse(obj.get("startTime")).astimezone(utc), @@ -23,30 +24,33 @@ def to_dict(self) -> dict: return { "state": self.state, "startTime": self.startTime, - "endTime": self.endTime + "endTime": self.endTime, } + class Group: def __init__(self, id: str, intervals: List[Interval]): self.id = id self.intervals = intervals @staticmethod - def from_dict(obj: dict) -> 'Group': - intervals = [Interval.from_dict(interval) for interval in obj.get("intervals", [])] - return Group( - id=obj.get("id"), - intervals=intervals - ) + def from_dict(obj: dict) -> "Group": + intervals = [ + Interval.from_dict(interval) for interval in obj.get("intervals", []) + ] + return Group(id=obj.get("id"), intervals=intervals) def to_dict(self) -> dict: return { "id": self.id, - "intervals": [interval.to_dict() for interval in self.intervals] + "intervals": [interval.to_dict() for interval in self.intervals], } + class OutageSchedule: - def __init__(self, id: str, date: str, dateString: str, imageUrl: str, groups: List[Group]): + def __init__( + self, id: str, date: str, dateString: str, imageUrl: str, groups: List[Group] + ): self.id = id self.date = date self.dateString = dateString @@ -54,14 +58,14 @@ def __init__(self, id: str, date: str, dateString: str, imageUrl: str, groups: L self.groups = groups @staticmethod - def from_dict(obj: dict) -> 'OutageSchedule': + def from_dict(obj: dict) -> "OutageSchedule": groups = [Group.from_dict(group) for group in obj.get("groups", [])] return OutageSchedule( id=obj.get("id"), date=parser.parse(obj.get("date")).astimezone(utc), dateString=obj.get("dateString"), imageUrl=obj.get("imageUrl"), - groups=groups + groups=groups, ) def to_dict(self) -> dict: @@ -70,9 +74,9 @@ def to_dict(self) -> dict: "date": self.date, "dateString": self.dateString, "imageUrl": self.imageUrl, - "groups": [group.to_dict() for group in self.groups] + "groups": [group.to_dict() for group in self.groups], } - + def get_current_event(self, group_id: str, at: datetime.datetime) -> dict: at = at.astimezone(utc) for group in self.groups: @@ -81,8 +85,10 @@ def get_current_event(self, group_id: str, at: datetime.datetime) -> dict: if interval.startTime <= at <= interval.endTime: return interval.to_dict() return {} - - def between(self, group_id: str, start: datetime.datetime, end: datetime.datetime) -> list[dict]: + + def between( + self, group_id: str, start: datetime.datetime, end: datetime.datetime + ) -> list[dict]: start = start.astimezone(utc) end = end.astimezone(utc) res = [] diff --git a/custom_components/loe_outages/sensor.py b/custom_components/loe_outages/sensor.py index 6a7708c..975edf7 100644 --- a/custom_components/loe_outages/sensor.py +++ b/custom_components/loe_outages/sensor.py @@ -92,4 +92,4 @@ def __init__( @property def native_value(self) -> str | None: """Return the state of the sensor.""" - return self.entity_description.val_func(self.coordinator) \ No newline at end of file + return self.entity_description.val_func(self.coordinator)