-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename [blueprint|Blueprint] -> [integration_blueprint|Integration bl…
…ueprint] (#47)
- Loading branch information
Showing
19 changed files
with
309 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
216 changes: 109 additions & 107 deletions
216
custom_components/blueprint/__init__.py → ...ponents/integration_blueprint/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,109 @@ | ||
""" | ||
Custom integration to integrate blueprint with Home Assistant. | ||
For more details about this integration, please refer to | ||
https://github.com/custom-components/blueprint | ||
""" | ||
import asyncio | ||
from datetime import timedelta | ||
import logging | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import Config, HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryNotReady | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .api import BlueprintApiClient | ||
|
||
from .const import ( | ||
CONF_PASSWORD, | ||
CONF_USERNAME, | ||
DOMAIN, | ||
PLATFORMS, | ||
STARTUP_MESSAGE, | ||
) | ||
|
||
SCAN_INTERVAL = timedelta(seconds=30) | ||
|
||
_LOGGER: logging.Logger = logging.getLogger(__package__) | ||
|
||
|
||
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 hass.data.get(DOMAIN) is None: | ||
hass.data.setdefault(DOMAIN, {}) | ||
_LOGGER.info(STARTUP_MESSAGE) | ||
|
||
username = entry.data.get(CONF_USERNAME) | ||
password = entry.data.get(CONF_PASSWORD) | ||
|
||
session = async_get_clientsession(hass) | ||
client = BlueprintApiClient(username, password, session) | ||
|
||
coordinator = BlueprintDataUpdateCoordinator(hass, client=client) | ||
await coordinator.async_refresh() | ||
|
||
if not coordinator.last_update_success: | ||
raise ConfigEntryNotReady | ||
|
||
hass.data[DOMAIN][entry.entry_id] = coordinator | ||
|
||
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.add_update_listener(async_reload_entry) | ||
return True | ||
|
||
|
||
class BlueprintDataUpdateCoordinator(DataUpdateCoordinator): | ||
"""Class to manage fetching data from the API.""" | ||
|
||
def __init__(self, hass: HomeAssistant, client: BlueprintApiClient) -> None: | ||
"""Initialize.""" | ||
self.api: BlueprintApiClient = client | ||
self.platforms = [] | ||
|
||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL) | ||
|
||
async def _async_update_data(self): | ||
"""Update data via library.""" | ||
try: | ||
return await self.api.async_get_data() | ||
except Exception as exception: | ||
raise UpdateFailed() from exception | ||
|
||
|
||
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.""" | ||
await async_unload_entry(hass, entry) | ||
await async_setup_entry(hass, entry) | ||
""" | ||
Custom integration to integrate integration_blueprint with Home Assistant. | ||
For more details about this integration, please refer to | ||
https://github.com/custom-components/integration_blueprint | ||
""" | ||
import asyncio | ||
from datetime import timedelta | ||
import logging | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import Config, HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryNotReady | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .api import IntegrationBlueprintApiClient | ||
|
||
from .const import ( | ||
CONF_PASSWORD, | ||
CONF_USERNAME, | ||
DOMAIN, | ||
PLATFORMS, | ||
STARTUP_MESSAGE, | ||
) | ||
|
||
SCAN_INTERVAL = timedelta(seconds=30) | ||
|
||
_LOGGER: logging.Logger = logging.getLogger(__package__) | ||
|
||
|
||
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 hass.data.get(DOMAIN) is None: | ||
hass.data.setdefault(DOMAIN, {}) | ||
_LOGGER.info(STARTUP_MESSAGE) | ||
|
||
username = entry.data.get(CONF_USERNAME) | ||
password = entry.data.get(CONF_PASSWORD) | ||
|
||
session = async_get_clientsession(hass) | ||
client = IntegrationBlueprintApiClient(username, password, session) | ||
|
||
coordinator = BlueprintDataUpdateCoordinator(hass, client=client) | ||
await coordinator.async_refresh() | ||
|
||
if not coordinator.last_update_success: | ||
raise ConfigEntryNotReady | ||
|
||
hass.data[DOMAIN][entry.entry_id] = coordinator | ||
|
||
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.add_update_listener(async_reload_entry) | ||
return True | ||
|
||
|
||
class BlueprintDataUpdateCoordinator(DataUpdateCoordinator): | ||
"""Class to manage fetching data from the API.""" | ||
|
||
def __init__( | ||
self, hass: HomeAssistant, client: IntegrationBlueprintApiClient | ||
) -> None: | ||
"""Initialize.""" | ||
self.api = client | ||
self.platforms = [] | ||
|
||
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL) | ||
|
||
async def _async_update_data(self): | ||
"""Update data via library.""" | ||
try: | ||
return await self.api.async_get_data() | ||
except Exception as exception: | ||
raise UpdateFailed() from exception | ||
|
||
|
||
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.""" | ||
await async_unload_entry(hass, entry) | ||
await async_setup_entry(hass, entry) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.