Skip to content

Commit

Permalink
formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Jun 27, 2024
1 parent 35ce031 commit 57961ba
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 48 deletions.
15 changes: 8 additions & 7 deletions custom_components/openei/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Custom integration to integrate OpenEI with Home Assistant."""

from datetime import datetime, timedelta
import logging
from datetime import datetime, timedelta

import openeihttp
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
import openeihttp

from .const import (
BINARY_SENSORS,
Expand All @@ -27,7 +27,9 @@
_LOGGER: logging.Logger = logging.getLogger(__package__)


async def async_setup(hass: HomeAssistant, config: Config):
async def async_setup( # pylint: disable-next=unused-argument
hass: HomeAssistant, config: Config
):
"""Set up this integration using YAML is not supported."""
return True

Expand Down Expand Up @@ -107,7 +109,7 @@ async def _async_update_data(self) -> dict:
raise UpdateFailed() from exception
return self._data

async def _async_refresh_data(self, data=None) -> None:
async def _async_refresh_data(self) -> None:
"""Update data via library."""
delta = timedelta(hours=1)
now = datetime.now()
Expand Down Expand Up @@ -154,7 +156,7 @@ def get_sensors(hass, config) -> dict:
rate.update()
data = {}

for sensor in SENSOR_TYPES:
for sensor in SENSOR_TYPES: # pylint: disable=consider-using-dict-items
_sensor = {}
value = getattr(rate, SENSOR_TYPES[sensor].key)
if isinstance(value, tuple):
Expand All @@ -164,7 +166,7 @@ def get_sensors(hass, config) -> dict:
_sensor[sensor] = getattr(rate, SENSOR_TYPES[sensor].key)
data.update(_sensor)

for sensor in BINARY_SENSORS:
for sensor in BINARY_SENSORS: # pylint: disable=consider-using-dict-items
_sensor = {}
_sensor[sensor] = getattr(rate, sensor)
data.update(_sensor)
Expand All @@ -182,7 +184,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Update listener."""

if not config_entry.options:
return

Expand Down
8 changes: 3 additions & 5 deletions custom_components/openei/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand All @@ -15,13 +14,13 @@


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup binary_sensor platform."""
"""Set up binary_sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]

binary_sensors = []
for binary_sensor in BINARY_SENSORS:
for binary_sensor in BINARY_SENSORS: # pylint: disable=consider-using-dict-items
binary_sensors.append(
OpenEIBinarySensor(hass, BINARY_SENSORS[binary_sensor], entry, coordinator)
OpenEIBinarySensor(BINARY_SENSORS[binary_sensor], entry, coordinator)
)

async_add_devices(binary_sensors, False)
Expand All @@ -32,7 +31,6 @@ class OpenEIBinarySensor(CoordinatorEntity, BinarySensorEntity):

def __init__(
self,
hass: HomeAssistant,
sensor_description: BinarySensorEntityDescription,
entry: ConfigEntry,
coordinator: str,
Expand Down
40 changes: 15 additions & 25 deletions custom_components/openei/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import logging
from typing import Any, Dict, List, Optional

import openeihttp
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.sensor import DOMAIN as SENSORS_DOMAIN
from homeassistant.core import HomeAssistant, callback
import openeihttp
import voluptuous as vol

from .const import (
CONF_API_KEY,
Expand Down Expand Up @@ -71,14 +71,15 @@ async def async_step_user_3(self, user_input=None):
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Enable option flow."""
return OpenEIOptionsFlowHandler(config_entry)

async def _show_config_form(self, user_input): # pylint: disable=unused-argument
"""Show the configuration form to edit location data."""
defaults = {}
return self.async_show_form(
step_id="user",
data_schema=_get_schema_step_1(self.hass, user_input, defaults),
data_schema=_get_schema_step_1(user_input, defaults),
errors=self._errors,
)

Expand All @@ -88,9 +89,7 @@ async def _show_config_form_2(self, user_input): # pylint: disable=unused-argum
utility_list = await _get_utility_list(self.hass, self._data)
return self.async_show_form(
step_id="user_2",
data_schema=_get_schema_step_2(
self.hass, self._data, defaults, utility_list
),
data_schema=_get_schema_step_2(self._data, defaults, utility_list),
errors=self._errors,
)

Expand Down Expand Up @@ -152,7 +151,7 @@ async def _show_config_form(self, user_input: Optional[Dict[str, Any]]):
"""Show the configuration form to edit location data."""
return self.async_show_form(
step_id="user",
data_schema=_get_schema_step_1(self.hass, user_input, self._data),
data_schema=_get_schema_step_1(user_input, self._data),
errors=self._errors,
)

Expand All @@ -161,9 +160,7 @@ async def _show_config_form_2(self, user_input: Optional[Dict[str, Any]]):
utility_list = await _get_utility_list(self.hass, self._data)
return self.async_show_form(
step_id="user_2",
data_schema=_get_schema_step_2(
self.hass, user_input, self._data, utility_list
),
data_schema=_get_schema_step_2(user_input, self._data, utility_list),
errors=self._errors,
)

Expand All @@ -180,12 +177,10 @@ async def _show_config_form_3(self, user_input: Optional[Dict[str, Any]]):


def _get_schema_step_1(
hass: HomeAssistant,
user_input: Optional[Dict[str, Any]],
default_dict: Dict[str, Any],
entry_id: str = None,
) -> vol.Schema:
"""Gets a schema using the default_dict as a backup."""
"""Get a schema using the default_dict as a backup."""
if user_input is None:
user_input = {}

Expand All @@ -196,7 +191,7 @@ def _get_schema_step_1(
default_dict[CONF_LOCATION] = ""

def _get_default(key: str, fallback_default: Any = None) -> None:
"""Gets default value for key."""
"""Get default value for key."""
return user_input.get(key, default_dict.get(key, fallback_default))

return vol.Schema(
Expand All @@ -211,18 +206,16 @@ def _get_default(key: str, fallback_default: Any = None) -> None:


def _get_schema_step_2(
hass: HomeAssistant,
user_input: Optional[Dict[str, Any]],
default_dict: Dict[str, Any],
utility_list: list,
entry_id: str = None,
) -> vol.Schema:
"""Gets a schema using the default_dict as a backup."""
"""Get a schema using the default_dict as a backup."""
if user_input is None:
user_input = {}

def _get_default(key: str, fallback_default: Any = None) -> None:
"""Gets default value for key."""
"""Get default value for key."""
return user_input.get(key, default_dict.get(key, fallback_default))

return vol.Schema(
Expand All @@ -239,17 +232,16 @@ def _get_schema_step_3(
user_input: Optional[Dict[str, Any]],
default_dict: Dict[str, Any],
plan_list: list,
entry_id: str = None,
) -> vol.Schema:
"""Gets a schema using the default_dict as a backup."""
"""Get a schema using the default_dict as a backup."""
if user_input is None:
user_input = {}

if CONF_SENSOR in default_dict.keys() and default_dict[CONF_SENSOR] == "(none)":
default_dict.pop(CONF_SENSOR, None)

def _get_default(key: str, fallback_default: Any = None) -> Any | None:
"""Gets default value for key."""
"""Get default value for key."""
return user_input.get(key, default_dict.get(key, fallback_default))

return vol.Schema(
Expand Down Expand Up @@ -291,7 +283,6 @@ async def _get_utility_list(hass, user_input) -> list | None:

async def _get_plan_list(hass, user_input) -> list | None:
"""Return list of rate plans by lat/lon."""

lat = None
lon = None
address = user_input[CONF_LOCATION]
Expand Down Expand Up @@ -335,12 +326,11 @@ def _get_entities(
for entity in hass.data[domain].entities:
if not hasattr(entity, "device_class"):
continue
elif search is not None and not entity.device_class == search:
if search is not None and not entity.device_class == search:
continue
data.append(entity.entity_id)

data.sort
if extra_entities:
data.insert(0, extra_entities)

return data
return data.sort
13 changes: 5 additions & 8 deletions custom_components/openei/const.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
"""Constants for integration_blueprint."""
"""Constants for OpenEI integration."""

from __future__ import annotations
from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
)
from homeassistant.components.sensor import (
SensorEntityDescription,
)
from homeassistant.helpers.entity import EntityCategory

from typing import Final

from homeassistant.components.binary_sensor import BinarySensorEntityDescription
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.helpers.entity import EntityCategory

# Base component constants
NAME = "OpenEI"
DOMAIN = "openei"
Expand Down
5 changes: 2 additions & 3 deletions custom_components/openei/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify


from .const import ATTRIBUTION, DOMAIN, SENSOR_TYPES


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup sensor platform."""
"""Set up sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]

sensors = []
for sensor in SENSOR_TYPES:
for sensor in SENSOR_TYPES: # pylint: disable=consider-using-dict-items
if sensor == "all_rates":
continue
sensors.append(OpenEISensor(hass, SENSOR_TYPES[sensor], entry, coordinator))
Expand Down
2 changes: 2 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Constants for OpenEI integration tests."""

CONFIG_DATA = {
"api_key": "fakeAPIKey",
"utility": "Fake Utility Co",
Expand Down

0 comments on commit 57961ba

Please sign in to comment.