diff --git a/aiopvapi/resources/scene.py b/aiopvapi/resources/scene.py index 6f341da..b41dd86 100644 --- a/aiopvapi/resources/scene.py +++ b/aiopvapi/resources/scene.py @@ -42,7 +42,7 @@ async def activate(self) -> list[int]: _val = await self.request.get( self.base_path, params={ATTR_SCENE_ID: self._id} ) - # v2 returns format {'sceneIds': ids} so flattening the list to align v3 + # v2 returns format {'shadeIds': ids} so flattening the list to align v3 _val = _val.get(ATTR_SHADE_IDS) # should return an array of ID's that belong to the scene return _val diff --git a/tests/test_room.py b/tests/test_room.py index 974c552..58b0bc9 100644 --- a/tests/test_room.py +++ b/tests/test_room.py @@ -1,9 +1,13 @@ +import asyncio +from aiohttp import ClientResponse from unittest.mock import Mock -from aiopvapi.helpers.aiorequest import PvApiResponseStatusError +from aiopvapi.helpers.aiorequest import AioRequest, PvApiResponseStatusError from aiopvapi.resources.room import Room from tests.fake_server import FAKE_BASE_URL from tests.test_apiresource import TestApiResource +from tests.test_scene_members import AsyncMock + ROOM_RAW_DATA = { "order": 2, @@ -23,7 +27,7 @@ def get_resource_uri(self): return "http://{}/api/rooms/26756".format(FAKE_BASE_URL) def get_resource(self): - _request = Mock() + _request = Mock(spec=AioRequest) _request.hub_ip = FAKE_BASE_URL _request.api_version = 2 _request.api_path = "api" @@ -35,7 +39,8 @@ def test_full_path(self): ) def test_name_property(self): - # No name_unicode, so base64 encoded is returned + # No name_unicode, although name is base64 encoded + # thus base64 decoded is returned self.assertEqual("Dining Room", self.resource.name) def test_delete_room_success(self): @@ -55,8 +60,19 @@ def test_delete_room_fail(self): async def go(): await self.start_fake_server() - room = self.get_resource() + # room = self.get_resource() + + loop = asyncio.get_event_loop() + request = AioRequest(FAKE_BASE_URL, loop, api_version=2) + + response = Mock(spec=ClientResponse) + response.status = 500 + response.release = AsyncMock(return_value=None) + request.websession.delete = AsyncMock(return_value=response) + + room = Room(ROOM_RAW_DATA, request) room._resource_path += "1" + resp = await room.delete() return resp diff --git a/tests/test_scene.py b/tests/test_scene.py index f45ea2d..e7f2212 100644 --- a/tests/test_scene.py +++ b/tests/test_scene.py @@ -1,15 +1,18 @@ +import asyncio +from aiohttp import ClientResponse from unittest.mock import Mock -from aiopvapi.helpers.aiorequest import PvApiResponseStatusError -from aiopvapi.helpers.aiorequest import AioRequest +from aiopvapi.helpers.aiorequest import AioRequest, PvApiResponseStatusError from aiopvapi.resources.scene import Scene from tests.fake_server import FAKE_BASE_URL from tests.test_apiresource import TestApiResource +from tests.test_scene_members import AsyncMock + SCENE_RAW_DATA = { "roomId": 26756, - "name": "RGluaW5nIFZhbmVzIE9wZW4=", + "name": "RGluaW5nIFZhbmVzIE9wZW4=", # "Dining Vanes Open" "colorId": 0, "iconId": 0, "id": 37217, @@ -32,8 +35,8 @@ def get_resource(self): return Scene(SCENE_RAW_DATA, _request) def test_name_property(self): - # No name_unicode, so base64 encoded is returned - # "RGluaW5nIFZhbmVzIE9wZW4=" + # No name_unicode, although name is base64 encoded + # thus base64 decoded is returned self.assertEqual("Dining Vanes Open", self.resource.name) def test_room_id_property(self): @@ -51,18 +54,31 @@ def test_activate_200(self): async def go(): await self.start_fake_server() scene = self.get_resource() + scene.request.get = AsyncMock(return_value={'shadeIds': [10]}) resp = await scene.activate() return resp resp = self.loop.run_until_complete(go()) - self.assertEqual(resp["id"], 10) + self.assertEqual(resp[0], 10) def test_activate_404(self): """Test scene activation""" async def go(): await self.start_fake_server() - scene = self.get_resource() + # scene = self.get_resource() + + loop = asyncio.get_event_loop() + request = AioRequest(FAKE_BASE_URL, loop, api_version=2) + + response = Mock(spec=ClientResponse) + response.status = 404 + response.release = AsyncMock(return_value=None) + request.websession.get = AsyncMock(return_value=response) + + scene = Scene(SCENE_RAW_DATA, request) + scene._resource_path += "1" + resp = await scene.activate() return resp diff --git a/tests/test_shade.py b/tests/test_shade.py index 241b540..3e163fe 100644 --- a/tests/test_shade.py +++ b/tests/test_shade.py @@ -1,7 +1,7 @@ from unittest.mock import Mock from aiopvapi.helpers.aiorequest import AioRequest -from aiopvapi.resources.shade import BaseShade, ShadeType +from aiopvapi.resources.shade import BaseShade, ShadeType, ShadePosition from aiopvapi.helpers.constants import ( ATTR_POSKIND1, ATTR_POSITION1, @@ -69,21 +69,22 @@ async def go(): def test_convert_g2(self): shade = self.get_resource() self.assertEqual( - shade.convert_to_v2({ATTR_PRIMARY: MAX_POSITION}), - {ATTR_POSITION1: MAX_POSITION_V2, ATTR_POSKIND1: 1}, + shade.percent_to_api(MAX_POSITION, ATTR_PRIMARY), + MAX_POSITION_V2 ) self.assertEqual( - shade.convert_to_v2({ATTR_TILT: MID_POSITION}), - {ATTR_POSITION1: MID_POSITION_V2, ATTR_POSKIND1: 3}, + shade.percent_to_api(MID_POSITION, ATTR_TILT), + MID_POSITION_V2 ) + positions = shade.structured_to_raw(ShadePosition(MAX_POSITION, None, MID_POSITION))['shade']['positions'] self.assertEqual( - shade.convert_to_v2({ATTR_PRIMARY: MAX_POSITION, ATTR_TILT: MID_POSITION}), + positions, { ATTR_POSKIND1: 1, ATTR_POSITION1: MAX_POSITION_V2, ATTR_POSKIND2: 3, ATTR_POSITION2: MID_POSITION_V2, - }, + } )