From 4ec905a662decc20faf9163721b77649fdc426e8 Mon Sep 17 00:00:00 2001 From: yurii Date: Sat, 13 Jul 2024 16:09:39 +0300 Subject: [PATCH] Update API interaction --- custom_components/loe_outages/const.py | 9 ++---- custom_components/loe_outages/coordinator.py | 32 ++++++------------- custom_components/loe_outages/sensor.py | 11 ++----- .../loe_outages/translations/en.json | 12 ++----- .../loe_outages/translations/uk.json | 12 ++----- 5 files changed, 21 insertions(+), 55 deletions(-) diff --git a/custom_components/loe_outages/const.py b/custom_components/loe_outages/const.py index 6316de4..881df03 100644 --- a/custom_components/loe_outages/const.py +++ b/custom_components/loe_outages/const.py @@ -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" \ No newline at end of file +TRANSLATION_KEY_EVENT_OFF: Final = f"component.{DOMAIN}.common.electricity_off" \ No newline at end of file diff --git a/custom_components/loe_outages/coordinator.py b/custom_components/loe_outages/coordinator.py index 107a514..5673466 100644 --- a/custom_components/loe_outages/coordinator.py +++ b/custom_components/loe_outages/coordinator.py @@ -2,6 +2,7 @@ import datetime import logging +import requests from homeassistant.components.calendar import CalendarEvent from homeassistant.config_entries import ConfigEntry @@ -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, ) @@ -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( @@ -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 @@ -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)", @@ -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, @@ -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] \ No newline at end of file diff --git a/custom_components/loe_outages/sensor.py b/custom_components/loe_outages/sensor.py index 1a74dce..29b544a 100644 --- a/custom_components/loe_outages/sensor.py +++ b/custom_components/loe_outages/sensor.py @@ -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 @@ -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( @@ -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", diff --git a/custom_components/loe_outages/translations/en.json b/custom_components/loe_outages/translations/en.json index fcecce5..6ea4ea2 100644 --- a/custom_components/loe_outages/translations/en.json +++ b/custom_components/loe_outages/translations/en.json @@ -41,8 +41,7 @@ "message": { "name": "Connectivity", "state": { - "off": "Electricity Outage", - "maybe": "Possible Outage" + "off": "Electricity Outage" } } } @@ -53,16 +52,12 @@ "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" } @@ -70,7 +65,6 @@ }, "common": { "electricity_on": "Connected", - "electricity_off": "Outage", - "electricity_maybe": "Possible Outage" + "electricity_off": "Outage" } } \ No newline at end of file diff --git a/custom_components/loe_outages/translations/uk.json b/custom_components/loe_outages/translations/uk.json index 1e9b168..4622acc 100644 --- a/custom_components/loe_outages/translations/uk.json +++ b/custom_components/loe_outages/translations/uk.json @@ -41,8 +41,7 @@ "message": { "name": "Підключення", "state": { - "off": "Відключення", - "maybe": "Можливе відключення" + "off": "Відключення" } } } @@ -53,16 +52,12 @@ "name": "Електрика", "state": { "on": "Заживлено", - "off": "Відключення", - "maybe": "Можливе відключення" + "off": "Відключення" } }, "next_outage": { "name": "Наступне відключення" }, - "next_possible_outage": { - "name": "Наступне можливе відключення" - }, "next_connectivity": { "name": "Наступна заживленість" } @@ -70,7 +65,6 @@ }, "common": { "electricity_on": "Заживлено", - "electricity_off": "Відключення", - "electricity_maybe": "Можливе відключення" + "electricity_off": "Відключення" } } \ No newline at end of file