File: test_huum.py

package info (click to toggle)
python-huum 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: python: 454; makefile: 2
file content (118 lines) | stat: -rw-r--r-- 3,298 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from typing import Any
from unittest.mock import ANY, patch

import aiohttp
import pytest

from huum.const import SaunaStatus
from huum.exceptions import SafetyException
from huum.huum import Huum
from tests.utils import MockResponse


@pytest.mark.asyncio
async def test_with_session() -> None:
    session = aiohttp.ClientSession()
    Huum("test", "test", session)


@pytest.mark.asyncio
async def test_closing_session() -> None:
    huum = Huum("test", "test")
    await huum.open_session()
    assert huum.session._connector is not None
    await huum.close_session()
    assert huum.session._connector is None


@pytest.mark.asyncio
async def test_no_auth() -> None:
    with pytest.raises(TypeError):
        Huum()  # type: ignore


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_door_open_on_check(mock_request: Any) -> None:
    response = {
        "statusCode": SaunaStatus.ONLINE_NOT_HEATING,
        "door": False,
        "temperature": 80,
        "maxHeatingTime": 180,
        "saunaName": "test",
    }
    mock_request.return_value = MockResponse(response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    with pytest.raises(SafetyException):
        await huum._check_door()


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_bad_temperature_value(mock_request: Any) -> None:
    mock_request.return_value = MockResponse({}, 400)

    huum = Huum("test", "test")
    await huum.open_session()
    with pytest.raises(ValueError):
        await huum.turn_on(temperature=200)


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_bad_humidity_value(mock_request: Any) -> None:
    mock_request.return_value = MockResponse({}, 400)

    huum = Huum("test", "test")
    await huum.open_session()
    with pytest.raises(ValueError):
        await huum.turn_on(temperature=40, humidity=11)


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_set_temperature_turn_on(mock_request: Any) -> None:
    response = {
        "statusCode": SaunaStatus.ONLINE_NOT_HEATING,
        "door": True,
        "temperature": 80,
        "maxHeatingTime": 180,
        "saunaName": "test",
    }
    mock_request.return_value = MockResponse(response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    result_turn_on = await huum.turn_on(temperature=80)
    result_set_temperature = await huum.set_temperature(temperature=80)

    assert result_turn_on == result_set_temperature


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_humidity_turn_on(mock_request: Any) -> None:
    response = {
        "statusCode": SaunaStatus.ONLINE_NOT_HEATING,
        "door": True,
        "temperature": 80,
        "maxHeatingTime": 180,
        "saunaName": "test",
        "humidity": 5,
    }
    mock_request.return_value = MockResponse(response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    result_turn_on = await huum.turn_on(temperature=80, humidity=5)

    mock_request.assert_called_with(
        "POST",
        "https://sauna.huum.eu/action/home/start",
        data=None,
        auth=ANY,
        json={"targetTemperature": 80, "humidity": 5},
    )
    assert result_turn_on.humidity == 5