Skip to content

Commit

Permalink
switched to sessionid_timestamp checking for re-authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
IngoS11 committed Jul 2, 2022
1 parent 0878dc3 commit c9f43bd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
python-version: 3.9
- name: Install dependencies
run: pip install 'isort==5.*' black flake8 mypy pydocstyle types-python-dateutil
run: pip install 'isort==5.*' black flake8 mypy pydocstyle pylint types-python-dateutil
- name: Install requirements
run: pip install -r requirements.txt
- name: Change directory
Expand All @@ -28,6 +28,8 @@ jobs:
run: isort --line-width 88 --trailing-comma -m 3 --profile black --check-only .
- name: black
run: black --check .
- name: pylint
run: pylint --min-similarity-lines=6 custom_components/schluter
- name: flake8
run: flake8 --max-line-length 88 .
- name: pydocstyle
Expand All @@ -41,4 +43,4 @@ jobs:
- name: Bandit Check (Python security linter)
uses: jpetrucciani/bandit-check@master
with:
path: '/custom_components/schluter/'
path: "/custom_components/schluter/"
17 changes: 14 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
"label": "Flake8",
"type": "shell",
"command": "pre-commit run flake8 --all-files",
"command": "pre-commit run flake8 custom_components/schluter",
"group": {
"kind": "test"
},
Expand All @@ -41,7 +41,7 @@
{
"label": "Pylint",
"type": "shell",
"command": "pylint custom_components/schluter",
"command": "pylint --min-similarity-lines=6 custom_components/schluter",
"dependsOn": [
"Install all Requirements"
],
Expand All @@ -58,7 +58,18 @@
"label": "Install all Requirements",
"type": "shell",
"command": "pip3 install --use-deprecated=legacy-resolver -r requirements.txt -r requirements_test.txt",
"group": "build",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": []
},
{
"label": "Isort",
"type": "shell",
"command": "isort --line-width 88 --trailing-comma -m 3 --profile black .",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "new"
Expand Down
28 changes: 16 additions & 12 deletions custom_components/schluter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import logging
from dataclasses import dataclass
from datetime import timedelta
from datetime import datetime, timedelta
from typing import Any

import async_timeout
Expand Down Expand Up @@ -91,21 +91,25 @@ async def _async_update_data(self) -> dict[str, Any]:
try:
async with async_timeout.timeout(10):
if self._sessionid is None:
_LOGGER.info("No Schluter Sessionid found, authenticating")
self._sessionid = await self._api.async_get_sessionid(
self._username, self._password
)
return await self._api.async_get_current_thermostats(self._sessionid)
except InvalidSessionIdError:
# if we get a 401 UNAUTHENTICATED the aio library raises the
# InvalidSessionIdError and we need to get a new sessionid
try:
self._sessionid = await self._api.async_get_sessionid(
self._username, self._password
expiration_timestamp = self._api.sessionid_timestamp + timedelta(
days=+1
)
_LOGGER.debug(
"Sessionid expiration timestamp is: %s",
expiration_timestamp.strftime("%Y-%m-%d %H:%M:%S"),
)
except InvalidUserPasswordError as err:
raise ConfigEntryAuthFailed from err
except (ApiError, ClientConnectorError) as err:
raise UpdateFailed(err) from err
if expiration_timestamp >= datetime.now():
_LOGGER.info("Schluter Sessionid is expired, authenticating again")
self._sessionid = await self._api.async_get_sessionid(
self._username, self._password
)
return await self._api.async_get_current_thermostats(self._sessionid)
except InvalidSessionIdError as err:
raise ConfigEntryAuthFailed from err
except InvalidUserPasswordError as err:
raise ConfigEntryAuthFailed from err
except (ApiError, ClientConnectorError) as err:
Expand Down
10 changes: 10 additions & 0 deletions custom_components/schluter/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from __future__ import annotations

import asyncio
import logging
from collections.abc import Mapping
from typing import Any

import voluptuous as vol
Expand All @@ -16,6 +18,8 @@

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): str,
Expand Down Expand Up @@ -61,3 +65,9 @@ async def async_step_user(
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)

async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
"""Handle re-auth if token invalid."""
# pylint: disable=unused-argument
_LOGGER.debug("Not implemented yet")
return await self.async_step_user()
2 changes: 1 addition & 1 deletion custom_components/schluter/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"documentation": "https://github.com/Ingos11/ha-schluter",
"issue_tracker": "https://github.com/Ingos11/ha-schluter/issues",
"requirements": [
"aioschluter==0.1.3"
"aioschluter==0.1.5"
],
"ssdp": [],
"zeroconf": [],
Expand Down

0 comments on commit c9f43bd

Please sign in to comment.