Skip to content

Commit

Permalink
feat: add shutter child lock
Browse files Browse the repository at this point in the history
  • Loading branch information
YogevBokobza committed Dec 29, 2024
1 parent c6a0a14 commit a0f9c6c
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
86 changes: 85 additions & 1 deletion app/tests/test_web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest_asyncio
from aiohttp import web
from aioswitcher.api import Command, DeviceState
from aioswitcher.api import Command, DeviceState, ShutterChildLock
from aioswitcher.schedule import Days
from assertpy import assert_that
from pytest import fixture, mark
Expand Down Expand Up @@ -63,6 +63,18 @@
set_position_uri2 = f"{webapp.ENDPOINT_SET_POSITION}?{fake_devicetype_runner_qparams}&{fake_device_qparams}&{fake_device_login_key_qparams}"
# /switcher/set_shutter_position?id=ab1c2d&ip=1.2.3.4&index=0&token=zvVvd7JxtN7CgvkD1Psujw==
set_position_uri3 = f"{webapp.ENDPOINT_SET_POSITION}?{fake_devicetype_runner_qparams}&{fake_device_qparams}&{fake_device_index_qparams}&{fake_device_token_qparams}"
# /switcher/turn_on_shutter_child_lock?id=ab1c2d&ip=1.2.3.4
turn_on_shutter_child_lock_uri = f"{webapp.ENDPOINT_TURN_ON_SHUTTER_CHILD_LOCK}?{fake_devicetype_runner_qparams}&{fake_device_qparams}"
# /switcher/turn_on_shutter_child_lock?id=ab1c2d&ip=1.2.3.4&key=18
turn_on_shutter_child_lock_uri2 = f"{webapp.ENDPOINT_TURN_ON_SHUTTER_CHILD_LOCK}?{fake_devicetype_runner_qparams}&{fake_device_qparams}&{fake_device_login_key_qparams}"
# /switcher/turn_on_shutter_child_lock?id=ab1c2d&ip=1.2.3.4&index=0&token=zvVvd7JxtN7CgvkD1Psujw==
turn_on_shutter_child_lock_uri3 = f"{webapp.ENDPOINT_TURN_ON_SHUTTER_CHILD_LOCK}?{fake_devicetype_runner_qparams}&{fake_device_qparams}&{fake_device_index_qparams}&{fake_device_token_qparams}"
# /switcher/turn_on_shutter_child_lock?id=ab1c2d&ip=1.2.3.4
turn_off_shutter_child_lock_uri = f"{webapp.ENDPOINT_TURN_OFF_SHUTTER_CHILD_LOCK}?{fake_devicetype_runner_qparams}&{fake_device_qparams}"
# /switcher/turn_off_shutter_child_lock?id=ab1c2d&ip=1.2.3.4&key=18
turn_off_shutter_child_lock_uri2 = f"{webapp.ENDPOINT_TURN_OFF_SHUTTER_CHILD_LOCK}?{fake_devicetype_runner_qparams}&{fake_device_qparams}&{fake_device_login_key_qparams}"
# /switcher/turn_off_shutter_child_lock?id=ab1c2d&ip=1.2.3.4&index=0&token=zvVvd7JxtN7CgvkD1Psujw==
turn_off_shutter_child_lock_uri3 = f"{webapp.ENDPOINT_TURN_OFF_SHUTTER_CHILD_LOCK}?{fake_devicetype_runner_qparams}&{fake_device_qparams}&{fake_device_index_qparams}&{fake_device_token_qparams}"
# /switcher/turn_on_light?id=ab1c2d&ip=1.2.3.4&index=0&token=zvVvd7JxtN7CgvkD1Psujw==
turn_on_light_uri = f"{webapp.ENDPOINT_TURN_ON_LIGHT}?{fake_devicetype_runner_qparams}&{fake_device_qparams}&{fake_device_index_qparams}&{fake_device_token_qparams}"
# /switcher/turn_off_light?id=ab1c2d&ip=1.2.3.4&index=0&token=zvVvd7JxtN7CgvkD1Psujw==
Expand Down Expand Up @@ -758,6 +770,78 @@ async def test_set_position_post_request(
assert_that(await response.json()).contains_entry(fake_serialized_data)


@mark.parametrize(
"api_uri, json_body, expected_values",
[
(turn_on_shutter_child_lock_uri, dict(), (ShutterChildLock.ON, 0)),
(turn_on_shutter_child_lock_uri2, dict(), (ShutterChildLock.ON, 0)),
(turn_on_shutter_child_lock_uri3, dict(), (ShutterChildLock.ON, 0)),
],
)
@patch("aioswitcher.api.SwitcherApi.set_shutter_child_lock")
async def test_successful_turn_on_shutter_child_lock_post_request(
set_shutter_child_lock,
response_serializer,
response_mock,
api_connect,
api_disconnect,
api_client,
api_uri,
json_body,
expected_values,
):
# stub api_turn_on_shutter_child_lock to return mock response
set_shutter_child_lock.return_value = response_mock
# send post request for turn_on_shutter_child_lock endpoint
response = await api_client.post(api_uri, json=json_body)
# verify mocks calling
api_connect.assert_called_once()
set_shutter_child_lock.assert_called_once_with(
expected_values[0], expected_values[1]
)
response_serializer.assert_called_once_with(response_mock)
api_disconnect.assert_called_once()
# assert the expected response
assert_that(response.status).is_equal_to(200)
assert_that(await response.json()).contains_entry(fake_serialized_data)


@mark.parametrize(
"api_uri, json_body, expected_values",
[
(turn_off_shutter_child_lock_uri, dict(), (ShutterChildLock.OFF, 0)),
(turn_off_shutter_child_lock_uri2, dict(), (ShutterChildLock.OFF, 0)),
(turn_off_shutter_child_lock_uri3, dict(), (ShutterChildLock.OFF, 0)),
],
)
@patch("aioswitcher.api.SwitcherApi.set_shutter_child_lock")
async def test_successful_turn_off_shutter_child_lock_post_request(
set_shutter_child_lock,
response_serializer,
response_mock,
api_connect,
api_disconnect,
api_client,
api_uri,
json_body,
expected_values,
):
# stub turn_off_shutter_child_lock to return mock response
set_shutter_child_lock.return_value = response_mock
# send post request for turn_off_shutter_child_lock endpoint
response = await api_client.post(api_uri, json=json_body)
# verify mocks calling
api_connect.assert_called_once()
set_shutter_child_lock.assert_called_once_with(
expected_values[0], expected_values[1]
)
response_serializer.assert_called_once_with(response_mock)
api_disconnect.assert_called_once()
# assert the expected response
assert_that(response.status).is_equal_to(200)
assert_that(await response.json()).contains_entry(fake_serialized_data)


@mark.parametrize(
"api_uri, json_body, expected_values",
[
Expand Down
55 changes: 55 additions & 0 deletions app/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from aioswitcher.device import (
DeviceState,
DeviceType,
ShutterChildLock,
ThermostatFanLevel,
ThermostatMode,
ThermostatSwing,
Expand Down Expand Up @@ -59,6 +60,8 @@
ENDPOINT_DELETE_SCHEDULE = "/switcher/delete_schedule"
ENDPOINT_CREATE_SCHEDULE = "/switcher/create_schedule"
ENDPOINT_SET_POSITION = "/switcher/set_shutter_position"
ENDPOINT_TURN_ON_SHUTTER_CHILD_LOCK = "/switcher/turn_on_shutter_child_lock"
ENDPOINT_TURN_OFF_SHUTTER_CHILD_LOCK = "/switcher/turn_off_shutter_child_lock"
ENDPOINT_TURN_ON_LIGHT = "/switcher/turn_on_light"
ENDPOINT_TURN_OFF_LIGHT = "/switcher/turn_off_light"
ENDPOINT_GET_BREEZE_STATE = "/switcher/get_breeze_state"
Expand Down Expand Up @@ -309,6 +312,58 @@ async def set_position(request: web.Request) -> web.Response:
)


@routes.post(ENDPOINT_TURN_ON_SHUTTER_CHILD_LOCK)
async def turn_on_shutter_child_lock(request: web.Request) -> web.Response:
"""Use to turn on the shutter child lock."""
device_type = DEVICES[request.query[KEY_TYPE]]
if KEY_LOGIN_KEY in request.query:
login_key = request.query[KEY_LOGIN_KEY]
else:
login_key = "00"
if KEY_INDEX in request.query:
index = int(request.query[KEY_INDEX])
else:
index = 0
if KEY_TOKEN in request.query:
token = request.query[KEY_TOKEN]
else:
token = None
async with SwitcherApi(
device_type, request.query[KEY_IP], request.query[KEY_ID], login_key, token
) as swapi:
return web.json_response(
_serialize_object(
await swapi.set_shutter_child_lock(ShutterChildLock.ON, index)
)
)


@routes.post(ENDPOINT_TURN_OFF_SHUTTER_CHILD_LOCK)
async def turn_off_shutter_child_lock(request: web.Request) -> web.Response:
"""Use to turn off the shutter child lock."""
device_type = DEVICES[request.query[KEY_TYPE]]
if KEY_LOGIN_KEY in request.query:
login_key = request.query[KEY_LOGIN_KEY]
else:
login_key = "00"
if KEY_INDEX in request.query:
index = int(request.query[KEY_INDEX])
else:
index = 0
if KEY_TOKEN in request.query:
token = request.query[KEY_TOKEN]
else:
token = None
async with SwitcherApi(
device_type, request.query[KEY_IP], request.query[KEY_ID], login_key, token
) as swapi:
return web.json_response(
_serialize_object(
await swapi.set_shutter_child_lock(ShutterChildLock.OFF, index)
)
)


@routes.post(ENDPOINT_TURN_ON_LIGHT)
async def turn_on_light(request: web.Request) -> web.Response:
"""Use to turn on the light device."""
Expand Down
16 changes: 16 additions & 0 deletions docs/endpoints_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@

--8<-- "query_params.md"

### ==Turn On Shutter Child Lock==

| Method | Endpoint | Description |
|:-------|:-------------------------------------|:------------------------------|
| POST | /switcher/turn_on_shutter_child_lock | Turn a shutter child lock on. |

--8<-- "query_params.md"

### ==Turn Off Shutter Child Lock==

| Method | Endpoint | Description |
|:-------|:--------------------------------------|:-------------------------------|
| POST | /switcher/turn_off_shutter_child_lock | Turn a shutter child lock off. |

--8<-- "query_params.md"

### ==Turn On Light==

| Method | Endpoint | Description |
Expand Down

0 comments on commit a0f9c6c

Please sign in to comment.