Skip to content

Commit

Permalink
Ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 6, 2024
1 parent 820e30d commit 1e02fc8
Show file tree
Hide file tree
Showing 22 changed files with 741 additions and 381 deletions.
58 changes: 38 additions & 20 deletions custom_components/ohme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
from .const import *
from .utils import get_option
from ohme import OhmeApiClient
from .coordinator import OhmeChargeSessionsCoordinator, OhmeAccountInfoCoordinator, OhmeAdvancedSettingsCoordinator, OhmeChargeSchedulesCoordinator
from .coordinator import (
OhmeChargeSessionsCoordinator,
OhmeAccountInfoCoordinator,
OhmeAdvancedSettingsCoordinator,
OhmeChargeSchedulesCoordinator,
)
from homeassistant.exceptions import ConfigEntryNotReady

_LOGGER = logging.getLogger(__name__)


async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
"""Set up the Ohme EV Charger component."""
return True


async def async_setup_dependencies(hass, entry):
"""Instantiate client and refresh session"""
client = OhmeApiClient(entry.data['email'], entry.data['password'])
account_id = entry.data['email']
client = OhmeApiClient(entry.data["email"], entry.data["password"])
account_id = entry.data["email"]

hass.data[DOMAIN][account_id][DATA_CLIENT] = client

Expand All @@ -29,19 +35,19 @@ async def async_setup_dependencies(hass, entry):

async def async_update_listener(hass, entry):
"""Handle options flow credentials update."""

# Reload this instance
await hass.config_entries.async_reload(entry.entry_id)


async def async_setup_entry(hass, entry):
"""This is called from the config flow."""

def _update_unique_id(entry: RegistryEntry) -> dict[str, str] | None:
"""Update unique IDs from old format."""
if entry.unique_id.startswith("ohme_"):
parts = entry.unique_id.split('_')
legacy_id = '_'.join(parts[2:])
parts = entry.unique_id.split("_")
legacy_id = "_".join(parts[2:])

if legacy_id in LEGACY_MAPPING:
new_id = LEGACY_MAPPING[legacy_id]
Expand All @@ -55,24 +61,30 @@ def _update_unique_id(entry: RegistryEntry) -> dict[str, str] | None:

await async_migrate_entries(hass, entry.entry_id, _update_unique_id)

account_id = entry.data['email']
account_id = entry.data["email"]

hass.data.setdefault(DOMAIN, {})
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN].setdefault(account_id, {})

await async_setup_dependencies(hass, entry)

coordinators = [
OhmeChargeSessionsCoordinator(hass=hass, account_id=account_id), # COORDINATOR_CHARGESESSIONS
OhmeAccountInfoCoordinator(hass=hass, account_id=account_id), # COORDINATOR_ACCOUNTINFO
OhmeAdvancedSettingsCoordinator(hass=hass, account_id=account_id), # COORDINATOR_ADVANCED
OhmeChargeSchedulesCoordinator(hass=hass, account_id=account_id) # COORDINATOR_SCHEDULES
OhmeChargeSessionsCoordinator(
hass=hass, account_id=account_id
), # COORDINATOR_CHARGESESSIONS
OhmeAccountInfoCoordinator(
hass=hass, account_id=account_id
), # COORDINATOR_ACCOUNTINFO
OhmeAdvancedSettingsCoordinator(
hass=hass, account_id=account_id
), # COORDINATOR_ADVANCED
OhmeChargeSchedulesCoordinator(
hass=hass, account_id=account_id
), # COORDINATOR_SCHEDULES
]

# We can function without these so setup can continue
coordinators_optional = [
OhmeAdvancedSettingsCoordinator
]
coordinators_optional = [OhmeAdvancedSettingsCoordinator]

for coordinator in coordinators:
# Catch failures if this is an 'optional' coordinator
Expand All @@ -81,10 +93,14 @@ def _update_unique_id(entry: RegistryEntry) -> dict[str, str] | None:
except ConfigEntryNotReady as ex:
allow_failure = False
for optional in coordinators_optional:
allow_failure = True if isinstance(coordinator, optional) else allow_failure
allow_failure = (
True if isinstance(coordinator, optional) else allow_failure
)

if allow_failure:
_LOGGER.error(f"{coordinator.__class__.__name__} failed to setup. This coordinator is optional so the integration will still function, but please raise an issue if this persists.")
_LOGGER.error(
f"{coordinator.__class__.__name__} failed to setup. This coordinator is optional so the integration will still function, but please raise an issue if this persists."
)
else:
raise ex

Expand All @@ -108,9 +124,11 @@ async def async_migrate_entry(hass: core.HomeAssistant, config_entry) -> bool:
"""Migrate old entry."""
# Version number has gone backwards
if CONFIG_VERSION < config_entry.version:
_LOGGER.error("Backwards migration not possible. Please update the integration.")
_LOGGER.error(
"Backwards migration not possible. Please update the integration."
)
return False

# Version number has gone up
if config_entry.version < CONFIG_VERSION:
_LOGGER.debug("Migrating from version %s", config_entry.version)
Expand Down
7 changes: 3 additions & 4 deletions custom_components/ohme/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.core import callback


class OhmeEntity(Entity):
"""Base class for all Ohme entities."""

Expand All @@ -22,11 +23,9 @@ async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
self.async_on_remove(
self.coordinator.async_add_listener(
self._handle_coordinator_update, None
)
self.coordinator.async_add_listener(self._handle_coordinator_update, None)
)

@callback
def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()
Expand Down
Loading

0 comments on commit 1e02fc8

Please sign in to comment.