Skip to content

Commit 4ec905a

Browse files
author
yurii
committed
Update API interaction
1 parent a082876 commit 4ec905a

File tree

5 files changed

+21
-55
lines changed

5 files changed

+21
-55
lines changed

custom_components/loe_outages/const.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
UPDATE_INTERVAL: Final = 60
1616

1717
# Values
18-
STATE_ON: Final = "on"
19-
STATE_OFF: Final = "off"
20-
STATE_MAYBE: Final = "maybe"
21-
18+
STATE_ON: Final = "PowerOn"
19+
STATE_OFF: Final = "PowerOff"
2220
# Endpoint paths
2321
SCHEDULE_PATH = "https://lps.yuriishunkin.com/api/schedule/latest/{group}"
2422
API_BASE_URL = "https://lps.yuriishunkin.com/api"
2523

2624

2725
# Keys
28-
TRANSLATION_KEY_EVENT_OFF: Final = f"component.{DOMAIN}.common.electricity_off"
29-
TRANSLATION_KEY_EVENT_MAYBE: Final = f"component.{DOMAIN}.common.electricity_maybe"
26+
TRANSLATION_KEY_EVENT_OFF: Final = f"component.{DOMAIN}.common.electricity_off"

custom_components/loe_outages/coordinator.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import datetime
44
import logging
5+
import requests
56

67
from homeassistant.components.calendar import CalendarEvent
78
from homeassistant.config_entries import ConfigEntry
@@ -14,10 +15,8 @@
1415
from .const import (
1516
CONF_GROUP,
1617
DOMAIN,
17-
STATE_MAYBE,
1818
STATE_OFF,
1919
STATE_ON,
20-
TRANSLATION_KEY_EVENT_MAYBE,
2120
TRANSLATION_KEY_EVENT_OFF,
2221
UPDATE_INTERVAL,
2322
)
@@ -52,7 +51,6 @@ def event_name_map(self) -> dict:
5251
"""Return a mapping of event names to translations."""
5352
return {
5453
STATE_OFF: self.translations.get(TRANSLATION_KEY_EVENT_OFF),
55-
STATE_MAYBE: self.translations.get(TRANSLATION_KEY_EVENT_MAYBE),
5654
}
5755

5856
async def update_config(
@@ -100,28 +98,19 @@ def next_outage(self) -> datetime.datetime | None:
10098
return event.start
10199
return None
102100

103-
@property
104-
def next_possible_outage(self) -> datetime.datetime | None:
105-
"""Get the next outage time."""
106-
next_events = self.get_next_events()
107-
for event in next_events:
108-
if self._event_to_state(event) == STATE_MAYBE:
109-
return event.start
110-
return None
111-
112101
@property
113102
def next_connectivity(self) -> datetime.datetime | None:
114103
"""Get next connectivity time."""
115104
now = dt_utils.now()
116105
current_event = self.get_event_at(now)
117-
# If current event is maybe, return the end time
118-
if self._event_to_state(current_event) == STATE_MAYBE:
106+
# If current event is OFF, return the end time
107+
if self._event_to_state(current_event) == STATE_OFF:
119108
return current_event.end
120109

121-
# Otherwise, return the next maybe event's end
110+
# Otherwise, return the next OFF event's end
122111
next_events = self.get_next_events()
123112
for event in next_events:
124-
if self._event_to_state(event) == STATE_MAYBE:
113+
if self._event_to_state(event) == STATE_OFF:
125114
return event.end
126115
return None
127116

@@ -168,10 +157,10 @@ def _get_calendar_event(
168157
if not event:
169158
return None
170159

171-
event_summary = event.get("SUMMARY")
172-
translated_summary = self.event_name_map.get(event_summary)
173-
event_start = event.decoded("DTSTART")
174-
event_end = event.decoded("DTEND")
160+
event_summary = event.get("state")
161+
translated_summary = self.event_name_map.get(event_summary) if translate else event_summary
162+
event_start = datetime.datetime.fromisoformat(event["startTime"])
163+
event_end = datetime.datetime.fromisoformat(event["endTime"])
175164

176165
LOGGER.debug(
177166
"Transforming event: %s (%s -> %s)",
@@ -181,7 +170,7 @@ def _get_calendar_event(
181170
)
182171

183172
return CalendarEvent(
184-
summary=translated_summary if translate else event_summary,
173+
summary=translated_summary,
185174
start=event_start,
186175
end=event_end,
187176
description=event_summary,
@@ -191,6 +180,5 @@ def _event_to_state(self, event: CalendarEvent | None) -> str:
191180
summary = event.as_dict().get("summary") if event else None
192181
return {
193182
STATE_OFF: STATE_OFF,
194-
STATE_MAYBE: STATE_MAYBE,
195183
None: STATE_ON,
196184
}[summary]

custom_components/loe_outages/sensor.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from homeassistant.core import HomeAssistant
1414
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1515

16-
from .const import STATE_MAYBE, STATE_OFF, STATE_ON
16+
from .const import STATE_OFF, STATE_ON
1717
from .coordinator import LoeOutagesCoordinator
1818
from .entity import LoeOutagesEntity
1919

@@ -39,7 +39,7 @@ def get_next_outage(coordinator: LoeOutagesCoordinator) -> str:
3939
translation_key="electricity",
4040
icon="mdi:transmission-tower",
4141
device_class=SensorDeviceClass.ENUM,
42-
options=[STATE_ON, STATE_OFF, STATE_MAYBE],
42+
options=[STATE_ON, STATE_OFF],
4343
val_func=lambda coordinator: coordinator.current_state,
4444
),
4545
LoeOutagesSensorDescription(
@@ -49,13 +49,6 @@ def get_next_outage(coordinator: LoeOutagesCoordinator) -> str:
4949
device_class=SensorDeviceClass.TIMESTAMP,
5050
val_func=lambda coordinator: coordinator.next_outage,
5151
),
52-
LoeOutagesSensorDescription(
53-
key="next_possible_outage",
54-
translation_key="next_possible_outage",
55-
icon="mdi:calendar-question",
56-
device_class=SensorDeviceClass.TIMESTAMP,
57-
val_func=lambda coordinator: coordinator.next_possible_outage,
58-
),
5952
LoeOutagesSensorDescription(
6053
key="next_connectivity",
6154
translation_key="next_connectivity",

custom_components/loe_outages/translations/en.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
"message": {
4242
"name": "Connectivity",
4343
"state": {
44-
"off": "Electricity Outage",
45-
"maybe": "Possible Outage"
44+
"off": "Electricity Outage"
4645
}
4746
}
4847
}
@@ -53,24 +52,19 @@
5352
"name": "Electricity",
5453
"state": {
5554
"on": "Connected",
56-
"off": "Outage",
57-
"maybe": "Possible Outage"
55+
"off": "Outage"
5856
}
5957
},
6058
"next_outage": {
6159
"name": "Next Outage"
6260
},
63-
"next_possible_outage": {
64-
"name": "Next Possible Outage"
65-
},
6661
"next_connectivity": {
6762
"name": "Next Connectivity"
6863
}
6964
}
7065
},
7166
"common": {
7267
"electricity_on": "Connected",
73-
"electricity_off": "Outage",
74-
"electricity_maybe": "Possible Outage"
68+
"electricity_off": "Outage"
7569
}
7670
}

custom_components/loe_outages/translations/uk.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
"message": {
4242
"name": "Підключення",
4343
"state": {
44-
"off": "Відключення",
45-
"maybe": "Можливе відключення"
44+
"off": "Відключення"
4645
}
4746
}
4847
}
@@ -53,24 +52,19 @@
5352
"name": "Електрика",
5453
"state": {
5554
"on": "Заживлено",
56-
"off": "Відключення",
57-
"maybe": "Можливе відключення"
55+
"off": "Відключення"
5856
}
5957
},
6058
"next_outage": {
6159
"name": "Наступне відключення"
6260
},
63-
"next_possible_outage": {
64-
"name": "Наступне можливе відключення"
65-
},
6661
"next_connectivity": {
6762
"name": "Наступна заживленість"
6863
}
6964
}
7065
},
7166
"common": {
7267
"electricity_on": "Заживлено",
73-
"electricity_off": "Відключення",
74-
"electricity_maybe": "Можливе відключення"
68+
"electricity_off": "Відключення"
7569
}
7670
}

0 commit comments

Comments
 (0)