Skip to content

Commit

Permalink
Ignore tech folders fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurkash committed Jul 14, 2024
1 parent 443232b commit f8390a4
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
custom_components/loe_outages/__pycache__
config
2 changes: 1 addition & 1 deletion custom_components/loe_outages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
10 changes: 4 additions & 6 deletions custom_components/loe_outages/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -76,7 +76,5 @@ def get_events(

for interval in schedule.between(self.group, start_date, end_date):
result.append(interval)

return result


8 changes: 4 additions & 4 deletions custom_components/loe_outages/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return events
2 changes: 1 addition & 1 deletion custom_components/loe_outages/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
)
10 changes: 5 additions & 5 deletions custom_components/loe_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)",
Expand All @@ -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]
}[state]
2 changes: 1 addition & 1 deletion custom_components/loe_outages/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def device_info(self) -> DeviceInfo:
identifiers={(DOMAIN, self.coordinator.config_entry.entry_id)},
manufacturer="Loe",
entry_type=DeviceEntryType.SERVICE,
)
)
40 changes: 23 additions & 17 deletions custom_components/loe_outages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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),
Expand All @@ -23,45 +24,48 @@ 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
self.imageUrl = imageUrl
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:
Expand All @@ -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:
Expand All @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion custom_components/loe_outages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
return self.entity_description.val_func(self.coordinator)

0 comments on commit f8390a4

Please sign in to comment.