-
-
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.
- Loading branch information
Showing
13 changed files
with
129 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"python.linting.pylintEnabled": true, | ||
"python.linting.enabled": true, | ||
"python.pythonPath": "/usr/local/bin/python" | ||
} |
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,75 @@ | ||
"""Sample API Client.""" | ||
import logging | ||
import asyncio | ||
import socket | ||
from typing import Optional | ||
import aiohttp | ||
import async_timeout | ||
|
||
TIMEOUT = 10 | ||
|
||
|
||
_LOGGER: logging.Logger = logging.getLogger(__package__) | ||
|
||
HEADERS = {"Content-type": "application/json; charset=UTF-8"} | ||
|
||
|
||
class BlueprintApiClient: | ||
def __init__( | ||
self, username: str, password: str, session: aiohttp.ClientSession | ||
) -> None: | ||
"""Sample API Client.""" | ||
self._username = username | ||
self._passeword = password | ||
self._session = session | ||
|
||
async def async_get_data(self) -> dict: | ||
"""Get data from the API.""" | ||
url = "https://jsonplaceholder.typicode.com/posts/1" | ||
return await self.api_wrapper("get", url) | ||
|
||
async def async_set_title(self, value: str) -> None: | ||
"""Get data from the API.""" | ||
url = "https://jsonplaceholder.typicode.com/posts/1" | ||
await self.api_wrapper("patch", url, data={"title": value}, headers=HEADERS) | ||
|
||
async def api_wrapper( | ||
self, method: str, url: str, data: dict = {}, headers: dict = {} | ||
) -> dict: | ||
"""Get information from the API.""" | ||
try: | ||
async with async_timeout.timeout(TIMEOUT, loop=asyncio.get_event_loop()): | ||
if method == "get": | ||
response = await self._session.get(url, headers=headers) | ||
return await response.json() | ||
|
||
elif method == "put": | ||
await self._session.put(url, headers=headers, json=data) | ||
|
||
elif method == "patch": | ||
await self._session.patch(url, headers=headers, json=data) | ||
|
||
elif method == "post": | ||
await self._session.post(url, headers=headers, json=data) | ||
|
||
except asyncio.TimeoutError as exception: | ||
_LOGGER.error( | ||
"Timeout error fetching information from %s - %s", | ||
url, | ||
exception, | ||
) | ||
|
||
except (KeyError, TypeError) as exception: | ||
_LOGGER.error( | ||
"Error parsing information from %s - %s", | ||
url, | ||
exception, | ||
) | ||
except (aiohttp.ClientError, socket.gaierror) as exception: | ||
_LOGGER.error( | ||
"Error fetching information from %s - %s", | ||
url, | ||
exception, | ||
) | ||
except Exception as exception: # pylint: disable=broad-except | ||
_LOGGER.error("Something really wrong happend! - %s", exception) |
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 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 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"name": "Blueprint", | ||
"hacs": "0.24.0", | ||
"hacs": "1.6.0", | ||
"domains": [ | ||
"binary_sensor", | ||
"sensor", | ||
"switch" | ||
], | ||
"iot_class": "Cloud Polling", | ||
"homeassistant": "0.115.0" | ||
"homeassistant": "0.118.0" | ||
} |
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