Skip to content

Commit

Permalink
implemented isloadhedding, and next loadshedding
Browse files Browse the repository at this point in the history
  • Loading branch information
tiaanv committed Dec 14, 2020
1 parent d0f3cce commit f8eedf3
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 79 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
}
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"*.yaml": "home-assistant"
}
}
10 changes: 6 additions & 4 deletions custom_components/eskom_loadshedding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .eskom_interface import eskom_interface
from .loadshedding_schedule import *

from .const import (
CONF_SCAN_PERIOD,
Expand Down Expand Up @@ -41,10 +42,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
scan_period = timedelta(
seconds=entry.options.get(CONF_SCAN_PERIOD, DEFAULT_SCAN_PERIOD)
)

coct_area = entry.options.get(CONF_AREA, DEFAULT_AREA)

coordinator = EskomDataUpdateCoordinator(hass, scan_period)
coordinator = EskomDataUpdateCoordinator(hass, scan_period, coct_area)
await coordinator.async_refresh()

if not coordinator.last_update_success:
Expand All @@ -68,17 +69,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
class EskomDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API."""

def __init__(self, hass, scan_period):
def __init__(self, hass, scan_period, coct_area):
"""Initialize."""
self.api = eskom_interface()
self.platforms = []

super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=scan_period)
self.coct_area = int(coct_area)

async def _async_update_data(self):
"""Update data via library."""
try:
data = await self.api.async_get_data()
data = await self.api.async_get_data(self.coct_area)
return data.get("data", {})
except Exception as exception:
raise UpdateFailed(exception)
Expand Down
55 changes: 51 additions & 4 deletions custom_components/eskom_loadshedding/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def available(self):
@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return self.config_entry.entry_id
return self.config_entry.entry_id + "stg"

@property
def device_info(self):
Expand All @@ -49,7 +49,8 @@ async def async_added_to_hass(self):
async def async_update(self):
"""Update entity."""
await self.coordinator.async_request_refresh()



class LoadSheddingActiveEntity(entity.Entity):
def __init__(self, coordinator, config_entry):
self.coordinator = coordinator
Expand All @@ -68,7 +69,7 @@ def available(self):
@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return self.config_entry.entry_id
return self.config_entry.entry_id + "lsa"

@property
def device_info(self):
Expand All @@ -95,4 +96,50 @@ async def async_added_to_hass(self):
async def async_update(self):
"""Update entity."""
await self.coordinator.async_request_refresh()



class NextLoadSheddingEntity(entity.Entity):
def __init__(self, coordinator, config_entry):
self.coordinator = coordinator
self.config_entry = config_entry

@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
return False

@property
def available(self):
"""Return if entity is available."""
return self.coordinator.last_update_success

@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return self.config_entry.entry_id + "nls"

@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self.unique_id)},
"name": NAME,
"model": VERSION,
"manufacturer": "swartjean",
}

@property
def device_state_attributes(self):
"""Return the state attributes."""
return {
"next_load_shedding": self.coordinator.data.get("next_load_shedding"),
}

async def async_added_to_hass(self):
"""Connect to dispatcher listening for entity data notifications."""
self.async_on_remove(
self.coordinator.async_add_listener(self.async_write_ha_state)
)

async def async_update(self):
"""Update entity."""
await self.coordinator.async_request_refresh()
18 changes: 16 additions & 2 deletions custom_components/eskom_loadshedding/eskom_interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ssl
from .loadshedding_schedule import isLoadSheddingNow, getNextTimeSlot


from aiohttp.client_exceptions import ClientConnectorError, ServerDisconnectedError
from aiohttp_retry import RetryClient
Expand Down Expand Up @@ -68,10 +70,22 @@ async def async_get_stage(self, attempts=50):
f"Error, invalid loadshedding stage received from API after {attempts} attempts"
)

async def async_get_data(self):
async def async_get_data(self, coct_area):
"""Fetches data from the loadshedding API"""
stage = await self.async_get_stage()

if stage > 0:
load_shedding_active = isLoadSheddingNow(stage, coct_area)["status"]
next_load_shedding = getNextTimeSlot(stage, coct_area)["date"]
else:
load_shedding_active = False
next_load_shedding = "N/A"

data = {
"data": {"stage": stage},
"data": {
"stage": stage,
"load_shedding_active": load_shedding_active,
"next_load_shedding": next_load_shedding,
},
}
return data
Loading

0 comments on commit f8eedf3

Please sign in to comment.