Skip to content

Commit

Permalink
Update __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberjunky committed Jan 7, 2024
1 parent b5a2826 commit 8301ac3
Showing 1 changed file with 53 additions and 34 deletions.
87 changes: 53 additions & 34 deletions custom_components/shell_recharge_ev/__init__.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,74 @@
"""Custom integration to integrate integration_blueprint with Home Assistant.
For more details about this integration, please refer to
https://github.com/ludeeus/integration_blueprint
"""
"""The shell_recharge_ev integration."""
from __future__ import annotations

import logging

import async_timeout
import shellrechargeev

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .api import IntegrationBlueprintApiClient
from .const import DOMAIN
from .coordinator import BlueprintDataUpdateCoordinator
from .const import DOMAIN, UPDATE_INTERVAL, LocationId

PLATFORMS: list[Platform] = [
Platform.SENSOR,
Platform.BINARY_SENSOR,
Platform.SWITCH,
]
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.SENSOR]


# https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up this integration using UI."""
"""Set up shell_recharge_ev from a config entry."""

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = coordinator = BlueprintDataUpdateCoordinator(
hass=hass,
client=IntegrationBlueprintApiClient(
username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD],
session=async_get_clientsession(hass),
),
)
# https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities
await coordinator.async_config_entry_first_refresh()

api = shellrechargeev.Api(websession=async_get_clientsession(hass))
# missing: validate api connection

coordinator = ShellRechargeEVDataUpdateCoordinator(hass, api, entry.data["location_id"])
hass.data[DOMAIN][entry.entry_id] = coordinator
await coordinator.async_config_entry_first_refresh()
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_reload_entry))

return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
if unloaded := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return unloaded

return unload_ok


class ShellRechargeEVDataUpdateCoordinator(DataUpdateCoordinator):
"""My custom coordinator."""

def __init__(self, hass, api: shellrechargeev.Api, location_id: LocationId):
"""Initialize my coordinator."""
super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=UPDATE_INTERVAL,
)
self.api = api
self.location_id = location_id


async def _async_update_data(self) -> shellrechargeev.Location | None:
"""Fetch data from API endpoint.
This is the place to pre-process the data to lookup tables
so entities can quickly look up their data.
"""
location = {}
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(10):
location = await self.api.location_by_id(self.location_id)
if location is None:
_LOGGER.info("received empty update")

async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
return location

0 comments on commit 8301ac3

Please sign in to comment.