generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 7
/
__init__.py
136 lines (106 loc) · 4.23 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Custom integration to integrate pod_point with Home Assistant.
For more details about this integration, please refer to
https://github.com/mattrayner/pod-point-home-assistant-component
"""
import asyncio
from datetime import timedelta
import logging
from pathlib import Path
from typing import Dict, List
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from podpointclient.charge import Charge
from podpointclient.client import PodPointClient
from podpointclient.pod import Pod
from .const import (
APP_IMAGE_URL_BASE,
CONF_EMAIL,
CONF_HTTP_DEBUG,
CONF_PASSWORD,
CONF_SCAN_INTERVAL,
DEFAULT_HTTP_DEBUG,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
PLATFORMS,
STARTUP_MESSAGE,
)
from .coordinator import PodPointDataUpdateCoordinator
from .services import async_register_services, async_deregister_services
_LOGGER: logging.Logger = logging.getLogger(__package__)
# pylint: disable=unused-argument
async def async_setup(hass: HomeAssistant, config: Config):
"""Set up this integration using YAML is not supported."""
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""
# If data for pod_point is not setup, prime it
if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})
_LOGGER.info(STARTUP_MESSAGE)
email = entry.data.get(CONF_EMAIL)
password = entry.data.get(CONF_PASSWORD)
session = async_get_clientsession(hass)
# If http debug is set, use that, or default
try:
http_debug = entry.options[CONF_HTTP_DEBUG]
except KeyError:
http_debug = DEFAULT_HTTP_DEBUG
client = PodPointClient(
username=email, password=password, session=session, http_debug=http_debug
)
# If a scan interval is set, use that, or default
try:
scan_interval = timedelta(seconds=entry.options[CONF_SCAN_INTERVAL])
except KeyError:
scan_interval = timedelta(seconds=DEFAULT_SCAN_INTERVAL)
# Setup our data coordinator with the desired scan interval
coordinator = PodPointDataUpdateCoordinator(
hass, client=client, scan_interval=scan_interval
)
# Check the credentials we have and ensure that we can perform a refresh
await coordinator.async_config_entry_first_refresh()
# Given a successful inital refresh, store this coordinator for this specific config entry
hass.data[DOMAIN][entry.entry_id] = coordinator
# Setup static image asset serving
should_cache = False
files_path = Path(__file__).parent / "static"
if hass.http:
hass.http.register_static_path(
APP_IMAGE_URL_BASE, str(files_path), should_cache
)
# For every platform defined, check if the user has disabled it. If not, set it up
for platform in PLATFORMS:
if entry.options.get(platform, True):
coordinator.platforms.append(platform)
hass.async_add_job(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
# Register the services
await async_register_services(hass)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
unloaded = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
if platform in coordinator.platforms
],
)
)
if unloaded:
hass.data[DOMAIN].pop(entry.entry_id)
return unloaded
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Reload config entry."""
unloaded = await async_unload_entry(hass, entry)
if unloaded is False:
_LOGGER.error("Error unloading entry: %s", entry)
await async_setup_entry(hass, entry)