Skip to content

Commit

Permalink
Gestion invalid auth
Browse files Browse the repository at this point in the history
  • Loading branch information
cnico committed Apr 26, 2024
1 parent 6fed07c commit 275455e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,7 @@ For static content in the mobile app :
For device renaming from the mobile App, a server message is sent :

{'name': 'device', 'action': 'update', 'data': {'id': 'L4HActuator_....', 'provider': 'L4HActuator', 'name': 'New name for light', 'modelName': 'CWMSwd-2B', 'vendor': 'Chacon', 'hardwareVersion': '1.0', 'softwareVersion': '1.0.6', 'macAddress': '...', 'type': '.dio1.wifi.genericSwitch.switch.', 'roomName': '...', 'roomId': '...', 'image': None, 'isNew': False}}

For plug setting's to set the blue status light on or off :

{"method":"POST","path":"/device/L4HActuator_..../action/quietmode","parameters":{"value":1},"id":9}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dio-chacon-wifi-api"
version = "1.0.0"
version = "1.0.1"
description = "Python library for DIO Chacon wifi's protocol for shutters and switches"
authors = ["cnico"]
license = "GNU Lesser General Public License v3 or later (LGPLv3+)"
Expand Down
9 changes: 8 additions & 1 deletion src/dio_chacon_wifi_api/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""Exceptions for DIOChaconAPIClient."""
"""Exceptions for DIOChacon API."""


class DIOChaconAPIError(Exception):
"""Error from this api."""

def __init__(self, *args) -> None:
Exception.__init__(self, *args)


class DIOChaconInvalidAuthError(Exception):
"""Invalid auth detected"""

def __init__(self, *args) -> None:
Exception.__init__(self, *args)
7 changes: 7 additions & 0 deletions src/dio_chacon_wifi_api/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import aiohttp

from .exceptions import DIOChaconInvalidAuthError

MAX_FAILED_ATTEMPTS = 5

STATE_CONNECTED = "connected"
Expand Down Expand Up @@ -88,6 +90,11 @@ async def on_request_end(session, trace_config_ctx, params):
self._aiohttp_session = aiohttp.ClientSession(trace_configs=[trace_config])
async with self._aiohttp_session.post(url=self._auth_url, data=payload_data, headers=headers_token) as resp:
resp_json = await resp.json()
if resp_json["status"] == 400:
err_msg = resp_json["data"]
_LOGGER.debug("Invalid auth response received : %s", err_msg)
await self._aiohttp_session.close()
raise DIOChaconInvalidAuthError(err_msg)
self._sessionToken = str(resp_json["data"]["sessionToken"])
_LOGGER.debug("SessionToken of authentication : " + self._sessionToken)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
"""Tests client.py. DIOChaconAPIClient class."""
"""Tests consts."""
from dio_chacon_wifi_api.const import DeviceTypeEnum


Expand Down
14 changes: 14 additions & 0 deletions tests/test_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# coding: utf-8
"""Tests exceptions."""
import pytest
from dio_chacon_wifi_api.exceptions import DIOChaconAPIError
from dio_chacon_wifi_api.exceptions import DIOChaconInvalidAuthError


def test_exceptions() -> None:

with pytest.raises(DIOChaconAPIError):
raise DIOChaconAPIError("Dumb test coverage")

with pytest.raises(DIOChaconInvalidAuthError):
raise DIOChaconInvalidAuthError("Dumb 2")

0 comments on commit 275455e

Please sign in to comment.