File: test_state.py

package info (click to toggle)
python-elgato 5.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: python: 1,288; makefile: 8; sh: 5
file content (158 lines) | stat: -rw-r--r-- 5,019 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""Tests for retrieving information from the Elgato Light device."""
from __future__ import annotations

import pytest
from aiohttp import ClientResponse, ClientSession
from aresponses import Response, ResponsesMockServer

from elgato import Elgato, ElgatoError, State

from . import load_fixture


async def test_state_temperature(aresponses: ResponsesMockServer) -> None:
    """Test getting Elgato Light state in temperature mode."""
    aresponses.add(
        "example.com:9123",
        "/elgato/lights",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("state-temperature.json"),
        ),
    )
    async with ClientSession() as session:
        elgato = Elgato("example.com", session=session)
        state: State = await elgato.state()
        assert state
        assert state.brightness == 21
        assert state.hue is None
        assert state.on
        assert state.saturation is None
        assert state.temperature == 297


async def test_state_color(aresponses: ResponsesMockServer) -> None:
    """Test getting Elgato Light state in color mode."""
    aresponses.add(
        "example.com:9123",
        "/elgato/lights",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("state-color.json"),
        ),
    )
    async with ClientSession() as session:
        elgato = Elgato("example.com", session=session)
        state: State = await elgato.state()
        assert state
        assert state.brightness == 50
        assert state.hue == 358.0
        assert state.on
        assert state.saturation == 6.0
        assert state.temperature is None


async def test_change_state_temperature(aresponses: ResponsesMockServer) -> None:
    """Test changing Elgato Light State in temperature mode."""

    async def response_handler(request: ClientResponse) -> Response:
        """Response handler for this test."""
        data = await request.json()
        assert data == {
            "numberOfLights": 1,
            "lights": [{"on": 1, "brightness": 100, "temperature": 200}],
        }
        return aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text="{}",
        )

    aresponses.add("example.com:9123", "/elgato/lights", "PUT", response_handler)

    async with ClientSession() as session:
        elgato = Elgato("example.com", session=session)
        await elgato.light(on=True, brightness=100, temperature=200)


async def test_change_state_color(aresponses: ResponsesMockServer) -> None:
    """Test changing Elgato Light State in color mode."""

    async def response_handler(request: ClientResponse) -> Response:
        """Response handler for this test."""
        data = await request.json()
        assert data == {
            "numberOfLights": 1,
            "lights": [{"on": 1, "brightness": 100, "hue": 10.1, "saturation": 20.2}],
        }
        return aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text="{}",
        )

    aresponses.add("example.com:9123", "/elgato/lights", "PUT", response_handler)

    async with ClientSession() as session:
        elgato = Elgato("example.com", session=session)
        await elgato.light(on=True, brightness=100, hue=10.1, saturation=20.2)


@pytest.mark.parametrize(
    ("state", "message"),
    [
        (
            {"hue": 10.1, "temperature": 10},
            "Cannot set temperature together with hue or saturation",
        ),
        (
            {"saturation": 10.1, "temperature": 10},
            "Cannot set temperature together with hue or saturation",
        ),
        (
            {"brightness": -1},
            "Brightness not between 0 and 100",
        ),
        (
            {"brightness": 101},
            "Brightness not between 0 and 100",
        ),
        (
            {"hue": -1},
            "Hue not between 0 and 360",
        ),
        (
            {"hue": 360.1},
            "Hue not between 0 and 360",
        ),
        (
            {"saturation": -1},
            "Saturation not between 0 and 100",
        ),
        (
            {"saturation": 100.1},
            "Saturation not between 0 and 100",
        ),
        (
            {"temperature": 142},
            "Color temperature out of range",
        ),
        (
            {"temperature": 345},
            "Color temperature out of range",
        ),
        (
            {},
            "No parameters to set, light not adjusted",
        ),
    ],
)
async def test_change_state_errors(state: dict[str, int | float], message: str) -> None:
    """Test changing Elgato Light State with invalid values."""
    elgato = Elgato("example.com")
    with pytest.raises(ElgatoError, match=message):
        await elgato.light(**state)  # type: ignore[arg-type]