-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from imhotep/open-support-refactor
adding support for OPEN and small refactor
- Loading branch information
Showing
8 changed files
with
84 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Unifi Access Coordinator. | ||
This module has the Unifi Access Coordinator to be used by entities. | ||
""" | ||
|
||
import asyncio | ||
from datetime import timedelta | ||
import logging | ||
|
||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryAuthFailed | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
|
||
from .errors import ApiAuthError, ApiError | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class UnifiAccessCoordinator(DataUpdateCoordinator): | ||
"""Unifi Access Coordinator. This is mostly used for local polling.""" | ||
|
||
def __init__(self, hass: HomeAssistant, hub) -> None: | ||
"""Initialize Unifi Access Coordinator.""" | ||
update_interval = timedelta(seconds=3) if hub.use_polling is True else None | ||
|
||
super().__init__( | ||
hass, | ||
_LOGGER, | ||
name="Unifi Access Coordinator", | ||
update_interval=update_interval, | ||
) | ||
self.hub = hub | ||
|
||
async def _async_update_data(self): | ||
"""Handle Unifi Access Coordinator updates.""" | ||
try: | ||
async with asyncio.timeout(10): | ||
return await self.hass.async_add_executor_job(self.hub.update) | ||
except ApiAuthError as err: | ||
raise ConfigEntryAuthFailed from err | ||
except ApiError as err: | ||
raise UpdateFailed("Error communicating with API") from err |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""Unifi Access Errors. | ||
This module lists Unifi Access API errors. | ||
""" | ||
|
||
|
||
class ApiAuthError(Exception): | ||
"""Raised when we can't authenticate with the API Token.""" | ||
|
||
|
||
class ApiError(Exception): | ||
"""Raised when we have some trouble using the API.""" |
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