Skip to content

Commit

Permalink
Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
swartjean committed Nov 16, 2024
1 parent 7c74128 commit 6e49a20
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 33 deletions.
7 changes: 4 additions & 3 deletions custom_components/eskom_loadshedding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
For more details about this integration, please refer to
https://github.com/swartjean/ha-eskom-loadshedding
"""

import asyncio
from datetime import timedelta
import logging
from datetime import timedelta

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand All @@ -16,8 +17,8 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import (
CONF_SCAN_PERIOD,
CONF_API_KEY,
CONF_SCAN_PERIOD,
DEFAULT_SCAN_PERIOD,
DOMAIN,
PLATFORMS,
Expand Down Expand Up @@ -57,7 +58,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

hass.data[DOMAIN][entry.entry_id] = coordinator

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

if not entry.update_listeners:
entry.add_update_listener(async_reload_entry)
Expand Down
23 changes: 13 additions & 10 deletions custom_components/eskom_loadshedding/calendar.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""Sensor platform for Eskom Loadshedding Interface."""
from datetime import datetime, timedelta

import re
from datetime import datetime, timedelta

from homeassistant.components.calendar import CalendarEntity, CalendarEvent
from homeassistant.core import callback

from .const import (
DEFAULT_CALENDAR_SCAN_PERIOD,
DOMAIN,
LOCAL_EVENTS_ID,
LOCAL_EVENTS_NAME,
LOCAL_SCHEDULE_ID,
LOCAL_SCHEDULE_NAME,
DEFAULT_CALENDAR_SCAN_PERIOD,
)
from .entity import EskomEntity

Expand Down Expand Up @@ -62,7 +63,8 @@ def name(self):

@property
def should_poll(self) -> bool:
"""Enable polling for the entity.
"""
Enable polling for the entity.
The coordinator is used to query the API, but polling is used to update
the entity state more frequently.
Expand Down Expand Up @@ -112,11 +114,11 @@ async def async_get_events(
)
for event in events
]
else:
return []
return []

async def async_update(self) -> None:
"""Disable update behavior.
"""
Disable update behavior.
Event updates are performed through the coordinator callback.
This is simply used to evaluate the entity state
"""
Expand Down Expand Up @@ -145,7 +147,8 @@ def name(self):

@property
def should_poll(self) -> bool:
"""Enable polling for the entity.
"""
Enable polling for the entity.
The coordinator is used to query the API, but polling is used to update
the entity state more frequently.
Expand Down Expand Up @@ -215,11 +218,11 @@ async def async_get_events(
)

return calendar_events
else:
return []
return []

async def async_update(self) -> None:
"""Disable update behavior.
"""
Disable update behavior.
Event updates are performed through the coordinator callback.
This is simply used to evaluate the entity state
"""
20 changes: 8 additions & 12 deletions custom_components/eskom_loadshedding/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Adds config flow for the Eskom Loadshedding Interface."""

from collections import OrderedDict

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.selector import selector
import voluptuous as vol

from .const import ( # pylint: disable=unused-import
CONF_SCAN_PERIOD,
CONF_API_KEY,
CONF_SCAN_PERIOD,
DEFAULT_SCAN_PERIOD,
DOMAIN,
MIN_SCAN_PERIOD,
Expand Down Expand Up @@ -41,8 +42,7 @@ async def async_step_user(self, user_input=None):
# Proceed to the next configuration step
return await self.async_step_area_search()

else:
self._errors["base"] = "auth"
self._errors["base"] = "auth"

return await self._show_user_config_form(user_input)

Expand Down Expand Up @@ -91,8 +91,7 @@ async def async_step_area_selection(self, user_input=None):
CONF_API_KEY: self.api_key,
},
)
else:
self._errors["base"] = "no_area_selection"
self._errors["base"] = "no_area_selection"

# Reformat the areas as label/value pairs for the selector
area_options = [
Expand Down Expand Up @@ -146,8 +145,7 @@ async def validate_key(self, api_key: str) -> bool:
data = await interface.async_query_api("/api_allowance")
if "error" in data:
return False
else:
return True
return True
except Exception: # pylint: disable=broad-except
pass
return False
Expand Down Expand Up @@ -187,8 +185,7 @@ async def async_step_user(self, user_input=None):
# Update all options
self.options.update(user_input)
return await self._update_options()
else:
self._errors["base"] = "auth"
self._errors["base"] = "auth"

data_schema = OrderedDict()
data_schema[
Expand Down Expand Up @@ -226,8 +223,7 @@ async def validate_key(self, api_key: str) -> bool:

if "error" in data:
return False
else:
return True
return True
except Exception: # pylint: disable=broad-except
pass
return False
1 change: 1 addition & 0 deletions custom_components/eskom_loadshedding/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Constants for eskom loadshedding interface"""

# Base component constants
NAME = "Eskom Loadshedding Interface"
DEVICE_NAME = "Loadshedding"
Expand Down
1 change: 1 addition & 0 deletions custom_components/eskom_loadshedding/entity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""EskomEntity class"""

from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DEVICE_NAME, DOMAIN, VERSION
Expand Down
7 changes: 4 additions & 3 deletions custom_components/eskom_loadshedding/eskom_interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import logging
import socket

Expand All @@ -25,14 +24,16 @@ def __init__(
}

async def async_query_api(self, endpoint: str, payload: dict = None):
"""Queries a given endpoint on the EskomSePush API with the specified payload
"""
Queries a given endpoint on the EskomSePush API with the specified payload
Args:
endpoint (string): The endpoint of the EskomSePush API
payload (dict, optional): The parameters to apply to the query. Defaults to None.
Returns:
The response object from the request
"""
query_url = self.base_url + endpoint
try:
Expand All @@ -52,7 +53,7 @@ async def async_query_api(self, endpoint: str, payload: dict = None):
# Re-raise the ClientResponseError to allow checking for valid headers during config
# These will be caught by the DataUpdateCoordinator
raise
except asyncio.TimeoutError as exception:
except TimeoutError as exception:
_LOGGER.error(
"Timeout fetching information from %s: %s",
query_url,
Expand Down
12 changes: 7 additions & 5 deletions custom_components/eskom_loadshedding/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sensor platform for Eskom Loadshedding Interface."""
from datetime import datetime

import re
from datetime import datetime

from homeassistant.components.sensor import SensorEntity

Expand Down Expand Up @@ -156,10 +157,8 @@ def native_value(self):
matches = re.findall(r"\d+", events[0]["note"])
if matches:
return int(matches[0])
else:
return events[0]["note"]
else:
return 0
return events[0]["note"]
return 0

@property
def icon(self):
Expand Down Expand Up @@ -219,6 +218,8 @@ def native_value(self):
if allowance:
return int(allowance["limit"]) - int(allowance["count"])

return None

@property
def icon(self):
"""Return the icon of the sensor."""
Expand All @@ -236,3 +237,4 @@ def extra_state_attributes(self):
"Limit": int(allowance["limit"]),
"Type": allowance["type"],
}
return None

0 comments on commit 6e49a20

Please sign in to comment.