Skip to content

Commit

Permalink
Add support for update (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocalvo authored Aug 11, 2023
1 parent dd26cd6 commit 0eb6d16
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
38 changes: 38 additions & 0 deletions ccm15/CCM15Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

BASE_URL = "http://{0}:{1}/{2}"
CONF_URL_STATUS = "status.xml"
CONF_URL_CTRL = "ctrl.xml"
DEFAULT_TIMEOUT = 10

class CCM15Device:
Expand Down Expand Up @@ -38,4 +39,41 @@ async def _fetch_data(self) -> CCM15DeviceState:
async def get_status_async(self) -> CCM15DeviceState:
return await self._fetch_data()

async def async_test_connection(self): # pragma: no cover
"""Test the connection to the CCM15 device."""
url = f"http://{self._host}:{self._port}/{CONF_URL_STATUS}"
try:
async with aiohttp.ClientSession() as session, session.get(
url, self.timeout
) as response:
if response.status == 200:
return True
return False
except (aiohttp.ClientError, asyncio.TimeoutError):
_LOGGER.debug("Test connection: Timeout")
return False

async def async_send_state(self, url: str) -> bool: # pragma: no cover
"""Send the url to set state to the ccm15 slave."""
async with httpx.AsyncClient() as client:
response = await client.get(url, self.timeout)
return response.status_code in (httpx.codes.OK, httpx.codes.FOUND)

async def async_set_state(self, ac_index: int, state: str, value: int) -> bool:
"""Set new target states."""
ac_id: int = 2**ac_index
url = BASE_URL.format(
self.host,
self.port,
CONF_URL_CTRL
+ "?ac0="
+ str(ac_id)
+ "&ac1=0"
+ "&"
+ state
+ "="
+ str(value),
)

return await self.async_send_state(url)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="py-ccm15",
version="0.0.3",
version="0.0.4",
author="Oscar Calvo",
author_email="[email protected]",
description="A package to control Midea CCM15 data converter modules",
Expand Down
11 changes: 11 additions & 0 deletions tests/test_ccm15.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,16 @@ async def test_get_status_async(self, mock_get) -> None:
self.assertIsInstance(state.devices[0], CCM15SlaveDevice)
self.assertIsInstance(state.devices[1], CCM15SlaveDevice)

@patch("httpx.AsyncClient.get")
async def test_async_set_state(self, mock_get):
# Set up mock response
mock_response = MagicMock()
mock_response.status_code = 200
mock_get.return_value = mock_response

# Call method and check result
result = await self.ccm.async_set_state(0, "state", 1)
self.assertTrue(result)

if __name__ == "__main__":
unittest.main()

0 comments on commit 0eb6d16

Please sign in to comment.