Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clientsession fix #24

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Release"

on:
release:
types: [published, edited]

permissions: {}

jobs:
release:
name: "Release"
runs-on: "ubuntu-latest"
permissions:
contents: write
steps:
- name: "Checkout the repository"
uses: "actions/checkout@v3.5.3"

- name: "Adjust version number"
shell: "bash"
run: |
yq -i -o json '.version="${{ github.event.release.tag_name }}"' \
"${{ github.workspace }}/custom_components/coway/manifest.json"

- name: "ZIP the integration directory"
shell: "bash"
run: |
cd "${{ github.workspace }}/custom_components/coway"
zip coway.zip -r ./

- name: "Upload the ZIP file to the release"
uses: softprops/action-gh-release@v0.1.15
with:
files: ${{ github.workspace }}/custom_components/coway/coway.zip
7 changes: 5 additions & 2 deletions custom_components/coway/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_create_clientsession
import homeassistant.helpers.config_validation as cv

from .const import DEFAULT_NAME, DOMAIN, SKIP_PASSWORD_CHANGE
Expand Down Expand Up @@ -59,7 +60,8 @@ async def async_step_reauth_confirm(
password = user_input[CONF_PASSWORD]
skip_password_change = user_input[SKIP_PASSWORD_CHANGE] if SKIP_PASSWORD_CHANGE in user_input else False
try:
await async_validate_api(username, password, skip_password_change)
session = async_create_clientsession(self.hass)
await async_validate_api(self.hass, username, password, skip_password_change, session)
except AuthError:
errors["base"] = "invalid_auth"
except ConnectionError:
Expand Down Expand Up @@ -101,7 +103,8 @@ async def async_step_user(
password = user_input[CONF_PASSWORD]
skip_password_change = user_input[SKIP_PASSWORD_CHANGE] if SKIP_PASSWORD_CHANGE in user_input else False
try:
await async_validate_api(username, password, skip_password_change)
session = async_create_clientsession(self.hass)
await async_validate_api(self.hass, username, password, skip_password_change, session)
except AuthError:
errors["base"] = "invalid_auth"
except ConnectionError:
Expand Down
16 changes: 10 additions & 6 deletions custom_components/coway/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

from datetime import timedelta

from cowayaio import CowayClient
from cowayaio.exceptions import AuthError, CowayError, PasswordExpired
from cowayaio.purifier_model import PurifierData

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER, SKIP_PASSWORD_CHANGE
from .util import COWAY_CLIENT
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER, SKIP_PASSWORD_CHANGE, TIMEOUT


class CowayDataUpdateCoordinator(DataUpdateCoordinator):
Expand All @@ -24,10 +25,13 @@ class CowayDataUpdateCoordinator(DataUpdateCoordinator):
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Initialize the Coway coordinator."""

COWAY_CLIENT.username = entry.data[CONF_USERNAME]
COWAY_CLIENT.password = entry.data[CONF_PASSWORD]
COWAY_CLIENT.skip_password_change = entry.options[SKIP_PASSWORD_CHANGE]
self.client = COWAY_CLIENT
self.client = CowayClient(
entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD],
session=async_create_clientsession(hass),
timeout=TIMEOUT,
)
self.client.skip_password_change = entry.options[SKIP_PASSWORD_CHANGE]
super().__init__(
hass,
LOGGER,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/coway/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/RobertD502/home-assistant-iocare/issues",
"requirements": ["cowayaio==0.1.7"],
"version": "0.1.9"
"version": "0.1.10"
}

30 changes: 10 additions & 20 deletions custom_components/coway/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,25 @@

import async_timeout

from aiohttp import ClientSession
from cowayaio import CowayClient
from cowayaio.exceptions import AuthError, CowayError, PasswordExpired

from homeassistant.core import async_get_hass, HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.core import HomeAssistant

from .const import LOGGER, COWAY_ERRORS, TIMEOUT


# Create a single instance of CowayClient that is
# used during the initial config flow and afterwards by
# the update coordinator. The creation of two CowayClient objects
# during initial integration configuration caused errors as Coway's
# servers don't like rapid back-to-back logins.
COWAY_CLIENT = CowayClient(
"",
"",
session=async_get_clientsession(async_get_hass()),
timeout=TIMEOUT
)


async def async_validate_api(username: str, password: str, skip_password_change: bool) -> None:
async def async_validate_api(hass: HomeAssistant, username: str, password: str, skip_password_change: bool, session: ClientSession) -> None:
"""Get data from API."""

COWAY_CLIENT.username = username
COWAY_CLIENT.password = password
COWAY_CLIENT.skip_password_change = skip_password_change
client = COWAY_CLIENT
client = CowayClient(
username,
password,
session=session,
timeout=TIMEOUT,
)
client.skip_password_change = skip_password_change

try:
async with async_timeout.timeout(TIMEOUT):
Expand Down
5 changes: 4 additions & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "Coway IoCare",
"render_readme": true,
"homeassistant": "2022.11.0b0"
"country": "US",
"homeassistant": "2022.11.0b0",
"zip_release": true,
"filename": "coway.zip"
}