Skip to content

Commit 2e60bc0

Browse files
authored
Merge pull request #10 from Jurkash/develop
Develop
2 parents da74803 + fb9866c commit 2e60bc0

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

.pre-commit-config.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.6.0
4+
hooks:
5+
- id: check-added-large-files
6+
- id: check-ast
7+
- id: check-builtin-literals
8+
- id: check-case-conflict
9+
- id: check-docstring-first
10+
- id: check-executables-have-shebangs
11+
- id: check-json
12+
- id: check-shebang-scripts-are-executable
13+
- id: check-symlinks
14+
- id: check-toml
15+
- id: check-yaml
16+
- id: debug-statements
17+
- id: destroyed-symlinks
18+
- id: end-of-file-fixer
19+
- id: mixed-line-ending
20+
- id: requirements-txt-fixer
21+
- id: trailing-whitespace
22+
23+
- repo: https://github.com/astral-sh/ruff-pre-commit
24+
rev: v0.4.10
25+
hooks:
26+
- id: ruff
27+
args: [--fix]
28+
- id: ruff-format
29+
30+
- repo: https://github.com/pre-commit/mirrors-prettier
31+
rev: "v3.1.0"
32+
hooks:
33+
- id: prettier

custom_components/loe_outages/api.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, group: str) -> None:
1919
self.group = group
2020
self.schedule = []
2121

22-
async def async_fetch_json_from_endpoint(self) -> dict:
22+
async def async_fetch_latest_json(self) -> dict:
2323
"""Fetch outages from the async API endpoint."""
2424
url = "https://lps.yuriishunkin.com/api/Schedule/latest"
2525
async with aiohttp.ClientSession() as session:
@@ -31,18 +31,34 @@ async def async_fetch_json_from_endpoint(self) -> dict:
3131
LOGGER.error(f"Failed to fetch schedule: {response.status}")
3232
return None
3333

34+
async def async_fetch_all_json(self) -> dict:
35+
"""Fetch outages from the async API endpoint."""
36+
url = "https://lps.yuriishunkin.com/api/Schedule/all"
37+
async with aiohttp.ClientSession() as session:
38+
async with session.get(url) as response:
39+
if response.status == 200:
40+
data = await response.json()
41+
return data
42+
else:
43+
LOGGER.error(f"Failed to fetch schedule: {response.status}")
44+
return None
45+
3446
async def async_fetch_schedule(self) -> None:
3547
"""Fetch outages from the JSON response."""
36-
schedule_data = await self.async_fetch_json_from_endpoint()
37-
schedule = OutageSchedule.from_dict(schedule_data)
3848
if len(self.schedule) == 0:
39-
self.schedule.append(schedule)
49+
schedule_data = await self.async_fetch_all_json()
50+
schedules = OutageSchedule.from_list(schedule_data)
51+
for schedule in sorted(schedules, key=lambda s: s.date):
52+
self.schedule.append(schedule)
4053
return
41-
42-
if self.schedule[-1].id != schedule.id:
43-
if self.schedule[-1].dateString == schedule.dateString:
44-
self.schedule.remove(self.schedule[-1])
45-
self.schedule.append(schedule)
54+
else:
55+
schedule_data = await self.async_fetch_latest_json()
56+
schedule = OutageSchedule.from_dict(schedule_data)
57+
58+
if self.schedule[-1].id != schedule.id:
59+
if self.schedule[-1].dateString == schedule.dateString:
60+
self.schedule.remove(self.schedule[-1])
61+
self.schedule.append(schedule)
4662

4763
def get_current_event(self, at: datetime) -> dict:
4864
"""Get the current event."""

custom_components/loe_outages/calendar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def event(self) -> CalendarEvent | None:
5353
now = dt_utils.now()
5454
LOGGER.debug("Getting current event for %s", now)
5555
interval = self.coordinator.get_event_at(now)
56+
if not interval:
57+
return None
58+
5659
return CalendarEvent(
5760
summary=interval.state,
5861
start=interval.startTime,

custom_components/loe_outages/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ def __init__(
5858
self.groups = groups
5959

6060
@staticmethod
61-
def from_dict(obj: dict) -> "OutageSchedule":
61+
def from_list(obj_list: List[dict]) -> List["OutageSchedule"]:
62+
return [OutageSchedule.from_dict(item) for item in obj_list]
63+
64+
@staticmethod
65+
def from_dict(obj: dict) -> list["OutageSchedule"]:
6266
groups = [Group.from_dict(group) for group in obj.get("groups", [])]
6367
return OutageSchedule(
6468
id=obj.get("id"),

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
homeassistant==2024.7.0
22
pip>=21.0,<24.2
33
pre-commit>=3.7.1
4-
ruff==0.5.0
4+
ruff==0.5.0

scripts/develop

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ fi
1717
export PYTHONPATH="${PYTHONPATH}:${PWD}/custom_components"
1818

1919
# Start Home Assistant
20-
hass --config "${PWD}/config" --debug
20+
hass --config "${PWD}/config" --debug

scripts/setup

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ cd "$(dirname "$0")/.."
66

77
python3 -m pip install --requirement requirements.txt
88

9-
# pre-commit install
9+
pre-commit install

0 commit comments

Comments
 (0)