Skip to content

Commit

Permalink
Merge pull request #6 from Jurkash/develop
Browse files Browse the repository at this point in the history
Update API interaction
  • Loading branch information
Jurkash authored Jul 13, 2024
2 parents 9d98400 + 4ec905a commit 20df3c8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 55 deletions.
9 changes: 3 additions & 6 deletions custom_components/loe_outages/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
UPDATE_INTERVAL: Final = 60

# Values
STATE_ON: Final = "on"
STATE_OFF: Final = "off"
STATE_MAYBE: Final = "maybe"

STATE_ON: Final = "PowerOn"
STATE_OFF: Final = "PowerOff"
# Endpoint paths
SCHEDULE_PATH = "https://lps.yuriishunkin.com/api/schedule/latest/{group}"
API_BASE_URL = "https://lps.yuriishunkin.com/api"


# Keys
TRANSLATION_KEY_EVENT_OFF: Final = f"component.{DOMAIN}.common.electricity_off"
TRANSLATION_KEY_EVENT_MAYBE: Final = f"component.{DOMAIN}.common.electricity_maybe"
TRANSLATION_KEY_EVENT_OFF: Final = f"component.{DOMAIN}.common.electricity_off"
32 changes: 10 additions & 22 deletions custom_components/loe_outages/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import logging
import requests

from homeassistant.components.calendar import CalendarEvent
from homeassistant.config_entries import ConfigEntry
Expand All @@ -14,10 +15,8 @@
from .const import (
CONF_GROUP,
DOMAIN,
STATE_MAYBE,
STATE_OFF,
STATE_ON,
TRANSLATION_KEY_EVENT_MAYBE,
TRANSLATION_KEY_EVENT_OFF,
UPDATE_INTERVAL,
)
Expand Down Expand Up @@ -52,7 +51,6 @@ 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_MAYBE: self.translations.get(TRANSLATION_KEY_EVENT_MAYBE),
}

async def update_config(
Expand Down Expand Up @@ -100,28 +98,19 @@ def next_outage(self) -> datetime.datetime | None:
return event.start
return None

@property
def next_possible_outage(self) -> datetime.datetime | None:
"""Get the next outage time."""
next_events = self.get_next_events()
for event in next_events:
if self._event_to_state(event) == STATE_MAYBE:
return event.start
return None

@property
def next_connectivity(self) -> datetime.datetime | None:
"""Get next connectivity time."""
now = dt_utils.now()
current_event = self.get_event_at(now)
# If current event is maybe, return the end time
if self._event_to_state(current_event) == STATE_MAYBE:
# If current event is OFF, return the end time
if self._event_to_state(current_event) == STATE_OFF:
return current_event.end

# Otherwise, return the next maybe event's end
# Otherwise, return the next OFF event's end
next_events = self.get_next_events()
for event in next_events:
if self._event_to_state(event) == STATE_MAYBE:
if self._event_to_state(event) == STATE_OFF:
return event.end
return None

Expand Down Expand Up @@ -168,10 +157,10 @@ def _get_calendar_event(
if not event:
return None

event_summary = event.get("SUMMARY")
translated_summary = self.event_name_map.get(event_summary)
event_start = event.decoded("DTSTART")
event_end = event.decoded("DTEND")
event_summary = event.get("state")
translated_summary = self.event_name_map.get(event_summary) if translate else event_summary
event_start = datetime.datetime.fromisoformat(event["startTime"])
event_end = datetime.datetime.fromisoformat(event["endTime"])

LOGGER.debug(
"Transforming event: %s (%s -> %s)",
Expand All @@ -181,7 +170,7 @@ def _get_calendar_event(
)

return CalendarEvent(
summary=translated_summary if translate else event_summary,
summary=translated_summary,
start=event_start,
end=event_end,
description=event_summary,
Expand All @@ -191,6 +180,5 @@ def _event_to_state(self, event: CalendarEvent | None) -> str:
summary = event.as_dict().get("summary") if event else None
return {
STATE_OFF: STATE_OFF,
STATE_MAYBE: STATE_MAYBE,
None: STATE_ON,
}[summary]
11 changes: 2 additions & 9 deletions custom_components/loe_outages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import STATE_MAYBE, STATE_OFF, STATE_ON
from .const import STATE_OFF, STATE_ON
from .coordinator import LoeOutagesCoordinator
from .entity import LoeOutagesEntity

Expand All @@ -39,7 +39,7 @@ def get_next_outage(coordinator: LoeOutagesCoordinator) -> str:
translation_key="electricity",
icon="mdi:transmission-tower",
device_class=SensorDeviceClass.ENUM,
options=[STATE_ON, STATE_OFF, STATE_MAYBE],
options=[STATE_ON, STATE_OFF],
val_func=lambda coordinator: coordinator.current_state,
),
LoeOutagesSensorDescription(
Expand All @@ -49,13 +49,6 @@ def get_next_outage(coordinator: LoeOutagesCoordinator) -> str:
device_class=SensorDeviceClass.TIMESTAMP,
val_func=lambda coordinator: coordinator.next_outage,
),
LoeOutagesSensorDescription(
key="next_possible_outage",
translation_key="next_possible_outage",
icon="mdi:calendar-question",
device_class=SensorDeviceClass.TIMESTAMP,
val_func=lambda coordinator: coordinator.next_possible_outage,
),
LoeOutagesSensorDescription(
key="next_connectivity",
translation_key="next_connectivity",
Expand Down
12 changes: 3 additions & 9 deletions custom_components/loe_outages/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"message": {
"name": "Connectivity",
"state": {
"off": "Electricity Outage",
"maybe": "Possible Outage"
"off": "Electricity Outage"
}
}
}
Expand All @@ -53,24 +52,19 @@
"name": "Electricity",
"state": {
"on": "Connected",
"off": "Outage",
"maybe": "Possible Outage"
"off": "Outage"
}
},
"next_outage": {
"name": "Next Outage"
},
"next_possible_outage": {
"name": "Next Possible Outage"
},
"next_connectivity": {
"name": "Next Connectivity"
}
}
},
"common": {
"electricity_on": "Connected",
"electricity_off": "Outage",
"electricity_maybe": "Possible Outage"
"electricity_off": "Outage"
}
}
12 changes: 3 additions & 9 deletions custom_components/loe_outages/translations/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"message": {
"name": "Підключення",
"state": {
"off": "Відключення",
"maybe": "Можливе відключення"
"off": "Відключення"
}
}
}
Expand All @@ -53,24 +52,19 @@
"name": "Електрика",
"state": {
"on": "Заживлено",
"off": "Відключення",
"maybe": "Можливе відключення"
"off": "Відключення"
}
},
"next_outage": {
"name": "Наступне відключення"
},
"next_possible_outage": {
"name": "Наступне можливе відключення"
},
"next_connectivity": {
"name": "Наступна заживленість"
}
}
},
"common": {
"electricity_on": "Заживлено",
"electricity_off": "Відключення",
"electricity_maybe": "Можливе відключення"
"electricity_off": "Відключення"
}
}

0 comments on commit 20df3c8

Please sign in to comment.