Skip to content

Commit

Permalink
Add tests for homewizard_energy (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
DCSBL authored Feb 15, 2022
1 parent fdaaa91 commit 02eda96
Show file tree
Hide file tree
Showing 4 changed files with 421 additions and 19 deletions.
Binary file added .coverage
Binary file not shown.
30 changes: 11 additions & 19 deletions homewizard_energy/homewizard_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ class HomeWizardEnergy:

_session: ClientSession | None
_close_session: bool = False
_request_timeout: int = 10

_detected_product_type: str | None = None
_detected_api_version: str | None = None

def __init__(self, host: str, clientsession: ClientSession = None):
def __init__(
self, host: str, clientsession: ClientSession = None, timeout: int = 10
):
"""Create a HomeWizard Energy object.
Args:
Expand All @@ -34,6 +37,7 @@ def __init__(self, host: str, clientsession: ClientSession = None):

self._host = host
self._session = clientsession
self._request_timeout = timeout

@property
def host(self) -> str:
Expand Down Expand Up @@ -65,11 +69,6 @@ async def data(self) -> Data:
if not self._detected_api_version:
await self.device()

if self._detected_api_version != SUPPORTED_API_VERSION:
raise UnsupportedError(
f"Unsupported API version, detected {self._detected_api_version}"
)

response = await self.request("api/v1/data")
return Data.from_dict(response)

Expand All @@ -81,12 +80,6 @@ async def state(self) -> State | None:
if self._detected_product_type not in DEVICES_WITH_STATE:
return None

if self._detected_api_version != SUPPORTED_API_VERSION:
raise UnsupportedError(
f"Unsupported device or API version, "
f"detected API:{self._detected_api_version} with {self._detected_product_type}"
)

response = await self.request("api/v1/state")
return State.from_dict(response)

Expand All @@ -110,9 +103,8 @@ async def state_set(
_LOGGER.error("At least one state update is required")
return False

response = await self.request("api/v1/state", method=METH_PUT, data=state)
if response is not None:
return True
await self.request("api/v1/state", method=METH_PUT, data=state)
return True

async def request(
self, path: str, method: str = METH_GET, data: object = None
Expand All @@ -128,7 +120,7 @@ async def request(
_LOGGER.debug("%s, %s, %s", method, url, data)

try:
async with async_timeout.timeout(8):
async with async_timeout.timeout(self._request_timeout):
resp = await self._session.request(
method,
url,
Expand Down Expand Up @@ -156,10 +148,10 @@ async def request(
raise RequestError(f"API request error ({resp.status})")

content_type = resp.headers.get("Content-Type", "")
if "application/json" not in content_type:
raise RequestError("Unexpected content type")
if "application/json" in content_type:
return await resp.json()

return await resp.json()
return await resp.text()

async def close(self):
"""Close client session."""
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/device_energysocket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"product_type": "HWE-SKT",
"product_name": "Energy Socket",
"serial": "3c39e7aabbcc",
"firmware_version": "2.11",
"api_version": "v1"
}
Loading

0 comments on commit 02eda96

Please sign in to comment.