fix action Action commit Created package file minor updates Add binary_sensor Updated package file Updated package file Fix BinarySensor Class name Add more descriptions Updated package file Move stuff Updated package file add binary_sensor to resources.json Updated package file Add configuration Updated package file Remove switch Adds CONTRIBUTING Fix broken link Remove refrences Fix links fix broken actions Update actions Updated package file Updated package file Black format Fix config issues Updated package file Fix icon for binary_sensor Add switch platform Update README.md (#1) * Update README.md * Update README.md Update CONTRIBUTING.md (#2) Update __init__.py (#3) * Update __init__.py * Set enabled default to `True` Update const.py (#4) cleanup badges/add style (#5) * shield that supports styling * badge cleanup * Change discord ID Correct typo Create manifest.json (#12) Fixes https://github.com/custom-components/blueprint/issues/10 Move manifest to correct dir Fixes #9 by renaming the file Updates, updates, more updates, worst commit messge ever! revert Update main.workflow Delete resources.json Create example.yaml Update README.md Summer update 😎 Adds info about devcontainer Adds pylint Add translation example remove stalebot Adds integrationhelper adds requirements.txt Adds "tabnine.tabnine-vscode" to devcontainer Add support for config_flow configuration Add config_flow "docs" Update README.md use https for pip install ha@dev (#15) Typo on README.md (#18) Fix various typos in comments (#19) Add hassfest action (#22) * Add hassfest action * Fix manifest issue Spring cleaning ☀️ (#23) * Spring cleaning * Actions * Fix branches * Changes for config_flow Show how to only allow one instance Adds HACS validation action (#24) Update postCreateCommand Update tasks Minor updates (#26) * link correction in README.md and info.md * Add READMME.md for .devcontainer * Add automation.yaml file in the configuration * Complete CONTRIBUTING.md Improve README for container dev and library update. (#27) Feature/setup cfg (#28) * Add setup.cfg * Run black and isort. * Add blueprint to first party. * Make const import consistent. Move translation files Fixes #32 Use CoordinatorEntity (#33) Co-authored-by: Joakim Sørensen <hi@ludeeus.dev> Fixed typos (#34) Fix directory name for translations and link to the documentation (#36) Fix info.md (#40) Use debian based devcontainer (#44) Remove sampleclient (#45) Rename [blueprint|Blueprint] -> [integration_blueprint|Integration blueprint] (#47) Change HACS action (#39) Fix wrong path for link (#48) For an unknown reason the link was pointing to a one of my repository. Probably a too quick copy/paste. Add french translation and strings improvements (#49) Reusing work done on strings.json done in #37 Add example tests (#50) Prepare and explain how to step by step debugging (#51) Add version Fix testing by bumping pytest-homeassistant-custom-component (#54) Fix typo in api.py (#55) passeword -> password Fix a typo in the readme (#56) Update .gitignore to include .idea (#57) Update .gitignore to include .idea for those using Py Charm Add iot_class to manifest Use `enable_custom_integrations` fixture by default (#58) Fix typo (#59) retain user input after an error (#52) Update README.md Closes #61 remove async_timeout.timeout loop arg (#65) Correct name "Blueprint" ->"Integration blueprint" (#64) change entity.py to use extra_state_attributes (#66)
110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
"""
|
|
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)
|