Skip to content

Commit

Permalink
Raise exception when spa is unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-mp committed Jul 3, 2022
1 parent 5b7bd43 commit 9c2147c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ This python package follows Semantic Versioning 2.0.0

***Reminder**: Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable.*

### 0.7.0 - 2022-07-03

Raise exception when spa is unreachable

**Breaking change**: Refactor return values

### 0.6.0 - 2022-07-03

Handle network exceptions
Expand Down
5 changes: 4 additions & 1 deletion intex_spa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Initialize the package."""

from intex_spa.intex_spa import IntexSpa
from intex_spa.intex_spa_status import IntexSpaStatus
from intex_spa.intex_spa_exceptions import (
IntexSpaUnreachableException,
IntexSpaDnsException,
)
22 changes: 5 additions & 17 deletions intex_spa/intex_spa.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""IntexSpa"""
import logging
import time
import asyncio
import typing

from .intex_spa_network_layer import IntexSpaNetworkLayer
from .intex_spa_query import IntexSpaQuery
from .intex_spa_status import IntexSpaStatus
from .intex_spa_exceptions import IntexSpaUnreachableException

_LOGGER = logging.getLogger(__name__)

Expand All @@ -21,10 +21,6 @@ class IntexSpa:
The network layer object for communications with intex spa wifi module
status : IntexSpaStatus
The status object of the spa
last_successful_update_ms : int
The millisecond timestamp of the last successful update
is_reachable : bool
Is the spa wifi module available
"""

def __init__(self, address: str = "SPA_DEVICE", port: str = "8990"):
Expand All @@ -41,8 +37,6 @@ def __init__(self, address: str = "SPA_DEVICE", port: str = "8990"):
_LOGGER.warning("Initializing IntexSpa instance")
self.network = IntexSpaNetworkLayer(address, port)
self.status = IntexSpaStatus()
self.last_successful_update_ms: int = None
self.is_reachable: bool = None
self._semaphore = asyncio.Semaphore(1)

async def _async_handle_intent(
Expand Down Expand Up @@ -112,13 +106,11 @@ async def _async_handle_intent(
# And update the status object with it
self.status = query.response_status

# Set availability info
self.last_successful_update_ms = int(time.time() * 1000)
self.is_reachable = True
except (AssertionError,):
_LOGGER.info("Malformed spa response during spa querying")
await asyncio.sleep(2)
continue

except (
asyncio.IncompleteReadError,
asyncio.TimeoutError,
Expand All @@ -131,19 +123,15 @@ async def _async_handle_intent(
await self.network.async_force_reconnect()
await asyncio.sleep(2)
continue

else:
break
else: # No retry has succeeded
_LOGGER.info("Spa is unreachable")
# Set unavailability info
self.is_reachable = False
self.status = IntexSpaStatus()
raise IntexSpaUnreachableException("Spa is unreachable")

return {
"status": self.status,
"is_reachable": self.is_reachable,
"last_successful_update_ms": self.last_successful_update_ms,
}
return self.status

async def async_update_status(self) -> IntexSpaStatus:
"""Update known status of the spa
Expand Down
9 changes: 9 additions & 0 deletions intex_spa/intex_spa_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""IntexSpaExceptions"""


class IntexSpaUnreachableException(Exception):
"""Exception raised when spa is unreachable"""


class IntexSpaDnsException(Exception):
"""Exception raised when DNS address cannot be resolved"""
19 changes: 16 additions & 3 deletions intex_spa/intex_spa_network_layer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""IntexSpa"""
import logging
import asyncio
import socket

from .intex_spa_exceptions import IntexSpaDnsException

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,9 +48,19 @@ async def _async_connect(self) -> None:
self.address,
self.port,
)
self.reader, self.writer = await asyncio.open_connection(
self.address, self.port
)
try:
self.reader, self.writer = await asyncio.open_connection(
self.address, self.port
)

except socket.gaierror as err:
if err.args[0] == socket.EAI_NONAME:
raise IntexSpaDnsException(
f"Cannot resolve DNS address for {self.address}"
) from err
else:
raise socket.gaierror from err

_LOGGER.info(
"TCP connection established with the spa",
)
Expand Down

0 comments on commit 9c2147c

Please sign in to comment.