Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Support for new subgroups #41

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions custom_components/yasno_outages/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""The Yasno Outages API module."""

from .base import YasnoOutagesApi
from .weekly import YasnoWeeklyOutagesApi

__all__ = ["YasnoOutagesApi", "YasnoWeeklyOutagesApi"]
47 changes: 47 additions & 0 deletions custom_components/yasno_outages/api/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Base class for Yasno outages API."""

import logging
from abc import ABC, abstractmethod

import requests

from .const import API_ENDPOINT

LOGGER = logging.getLogger(__name__)


class YasnoOutagesApi(ABC):
"""Abstract base class to interact with Yasno outages API."""

def __init__(self, city: str | None = None, group: str | None = None) -> None:
"""Initialize the YasnoBaseApi with city and group."""
self.group = group
self.city = city
self.api_url = API_ENDPOINT
self.schedule = None

def fetch_schedule(self) -> None:
"""Fetch outages from the API and store the raw schedule."""
try:
response = requests.get(self.api_url, timeout=60)
response.raise_for_status()
self.schedule = response.json()
except requests.RequestException as error:
LOGGER.exception("Error fetching schedule from Yasno API: %s", error) # noqa: TRY401
self.schedule = {}

@abstractmethod
def get_cities(self) -> list[str]:
"""Get a list of available cities."""

@abstractmethod
def get_group_schedule(self, city: str, group: str) -> list:
"""Get the schedule for a specific group."""

@abstractmethod
def get_events(self) -> list[dict]:
"""Get all events."""

@abstractmethod
def get_current_event(self) -> dict | None:
"""Get the current event."""
9 changes: 9 additions & 0 deletions custom_components/yasno_outages/api/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Constants for the Yasno Outages API."""

API_ENDPOINT = (
"https://api.yasno.com.ua/api/v1/pages/home/schedule-turn-off-electricity"
)
START_OF_DAY = 0
END_OF_DAY = 24

OUTAGES_TEMPLATE_NAME = "electricity-outages-daily-schedule"
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
import requests
from dateutil.rrule import WEEKLY, rrule

LOGGER = logging.getLogger(__name__)
from .base import YasnoOutagesApi
from .const import API_ENDPOINT, END_OF_DAY, OUTAGES_TEMPLATE_NAME, START_OF_DAY

API_ENDPOINT = (
"https://api.yasno.com.ua/api/v1/pages/home/schedule-turn-off-electricity"
)
START_OF_DAY = 0
END_OF_DAY = 24
LOGGER = logging.getLogger(__name__)


class YasnoOutagesApi:
class YasnoWeeklyOutagesApi(YasnoOutagesApi):
"""Class to interact with Yasno outages API."""

"""Group name format"""
Expand All @@ -34,7 +31,7 @@ def _extract_schedule(self, data: dict) -> dict | None:
(
item
for item in data["components"]
if item["template_name"] == "electricity-outages-schedule"
if item["template_name"] == OUTAGES_TEMPLATE_NAME
),
None,
)
Expand Down
6 changes: 3 additions & 3 deletions custom_components/yasno_outages/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
SelectSelectorConfig,
)

from .api import YasnoOutagesApi
from .api import YasnoOutagesApi, YasnoWeeklyOutagesApi
from .const import CONF_CITY, CONF_GROUP, DEFAULT_CITY, DEFAULT_GROUP, DOMAIN, NAME

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -93,7 +93,7 @@ class YasnoOutagesOptionsFlow(OptionsFlow):
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
self.api = YasnoOutagesApi()
self.api = YasnoWeeklyOutagesApi()
self.data: dict[str, Any] = {}

async def async_step_init(self, user_input: dict | None = None) -> ConfigFlowResult:
Expand Down Expand Up @@ -140,7 +140,7 @@ class YasnoOutagesConfigFlow(ConfigFlow, domain=DOMAIN):

def __init__(self) -> None:
"""Initialize config flow."""
self.api = YasnoOutagesApi()
self.api = YasnoWeeklyOutagesApi()
self.data: dict[str, Any] = {}

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion custom_components/yasno_outages/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Defaults
DEFAULT_CITY: Final = "kiev"
DEFAULT_GROUP: Final = "1"
DEFAULT_GROUP: Final = "1.1"

# Consts
UPDATE_INTERVAL: Final = 15
Expand Down
12 changes: 9 additions & 3 deletions custom_components/yasno_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.util import dt as dt_utils

from .api import YasnoOutagesApi
from .api import YasnoWeeklyOutagesApi
from .const import (
CONF_CITY,
CONF_GROUP,
Expand Down Expand Up @@ -60,7 +60,13 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
LOGGER.warning("City not set in configuration. Setting to default.")
self.city = DEFAULT_CITY

self.api = YasnoOutagesApi(city=self.city, group=self.group)
LOGGER.debug(
"Creating Yasno API with city: %s, group: %s",
self.city,
self.group,
)

self.api = YasnoWeeklyOutagesApi(city=self.city, group=self.group)

@property
def event_name_map(self) -> dict:
Expand All @@ -84,7 +90,7 @@ async def update_config(
if city_updated or group_updated:
LOGGER.debug("Updating group from %s -> %s", self.group, new_group)
self.group = new_group
self.api = YasnoOutagesApi(city=self.city, group=self.group)
self.api = YasnoWeeklyOutagesApi(city=self.city, group=self.group)
await self.async_refresh()
else:
LOGGER.debug("No group update necessary.")
Expand Down
Loading