File: test_hmip_cli.py

package info (click to toggle)
python-homematicip 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,796 kB
  • sloc: python: 15,164; makefile: 17; sh: 4
file content (151 lines) | stat: -rw-r--r-- 5,277 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
import json
import logging

from homematicip.base.enums import CliActions, DoorState
from homematicip.base.helpers import anonymizeConfig, handle_config
from homematicip.cli.hmip_cli import (
    _channel_supports_action,
    _execute_action_for_device,
    _get_target_channel_indices,
    _get_target_channels,
    getRssiBarString,
)
from homematicip.home import Home
from homematicip_demo.helper import no_ssl_verification

logger = logging.getLogger("test_hmip_cli")


def test_getRssiBarString():
    assert getRssiBarString(-50) == "[**********]"
    assert getRssiBarString(-55) == "[*********_]"
    assert getRssiBarString(-60) == "[********__]"
    assert getRssiBarString(-65) == "[*******___]"
    assert getRssiBarString(-70) == "[******____]"
    assert getRssiBarString(-75) == "[*****_____]"
    assert getRssiBarString(-80) == "[****______]"
    assert getRssiBarString(-85) == "[***_______]"
    assert getRssiBarString(-90) == "[**________]"
    assert getRssiBarString(-95) == "[*_________]"
    assert getRssiBarString(-100) == "[__________]"


def test_handle_config_error():
    assert handle_config({"errorCode": "Dummy"}, False) is None


def test_anonymizeConfig():
    c = (
        '{"id":"d0fea2b1-ef3b-44b1-ae96-f9b31f75de84",'
        '"id2":"d0fea2b1-ef3b-44b1-ae96-f9b31f75de84",'
        '"inboxGroup":"2dc54a8d-ceee-4626-8f27-b24e78dc05de",'
        '"availableFirmwareVersion": "0.0.0",'
        '"sgtin":"3014F71112345AB891234561", "sgtin_silvercrest" : "301503771234567891234567",'
        '"location":'
        '{"city": "Vatican City, Vatican","latitude":"41.9026011","longitude":"12.4533701"}}'
    )
    c = handle_config(json.loads(c), True)

    js = json.loads(c)

    assert js["id"] == "00000000-0000-0000-0000-000000000000"
    assert js["id"] == js["id2"]
    assert js["inboxGroup"] == "00000000-0000-0000-0000-000000000001"
    assert js["sgtin"] == "3014F7110000000000000000"
    assert js["sgtin_silvercrest"] == "3014F7110000000000000001"
    assert js["availableFirmwareVersion"] == "0.0.0"

    location = js["location"]
    assert location["city"] == "1010, Vienna, Austria"
    assert location["latitude"] == "48.208088"
    assert location["longitude"] == "16.358608"

    c = '{"id":"test"}'
    c = anonymizeConfig(c, "original", "REPLACED")
    assert c == '{"id":"test"}'


def test_get_target_channel_indices(fake_home: Home):
    d = fake_home.search_device_by_id("3014F71100000000000DRBL4")

    assert _get_target_channel_indices(d, [1]) == [1]
    assert _get_target_channel_indices(d, None) == [1]
    assert _get_target_channel_indices(d, [1, 2, 3]) == [1, 2, 3]


def test_get_target_channels(fake_home: Home):
    d = fake_home.search_device_by_id("3014F71100000000000DRBL4")

    result = _get_target_channels(d, None)
    assert len(result) == 1
    assert result[0].index == 1

    result = _get_target_channels(d, [1, 3])
    assert len(result) == 2
    assert result[0].index == 1
    assert result[1].index == 3


def test_execute_action_for_device_shutter_level(fake_home: Home):
    class Args:
        def __init__(self) -> None:
            self.channels: list = [1, 2, 3]

    args = Args()
    d = fake_home.search_device_by_id("3014F71100000000000DRBL4")

    with no_ssl_verification():
        _execute_action_for_device(
            d, args, CliActions.SET_SHUTTER_LEVEL, "set_shutter_level", 0.5
        )
        fake_home.get_current_state()
        d = fake_home.search_device_by_id("3014F71100000000000DRBL4")
        assert d.functionalChannels[1].shutterLevel == 0.5
        assert d.functionalChannels[2].shutterLevel == 0.5
        assert d.functionalChannels[3].shutterLevel == 0.5


def test_execute_action_for_device_slats_level(fake_home: Home):
    class Args:
        def __init__(self) -> None:
            self.channels: list = [1, 2, 3]

    args = Args()
    d = fake_home.search_device_by_id("3014F71100000000000DRBL4")

    with no_ssl_verification():
        _execute_action_for_device(
            d, args, CliActions.SET_SLATS_LEVEL, "set_slats_level", 0.5
        )
        fake_home.get_current_state()
        d = fake_home.search_device_by_id("3014F71100000000000DRBL4")
        assert d.functionalChannels[1].slatsLevel == 0.5
        assert d.functionalChannels[2].slatsLevel == 0.5
        assert d.functionalChannels[3].slatsLevel == 0.5


def test_execute_action_for_device_send_door_command(fake_home: Home):
    class Args:
        def __init__(self) -> None:
            self.channels = None

    args = Args()
    d = fake_home.search_device_by_id("3014F0000000000000FAF9B4")

    with no_ssl_verification():
        _execute_action_for_device(
            d, args, CliActions.SEND_DOOR_COMMAND, "send_door_command", "OPEN"
        )
        fake_home.get_current_state()
        d = fake_home.search_device_by_id("3014F0000000000000FAF9B4")
        assert d.functionalChannels[1].doorState == DoorState.OPEN


def test_channel_supports_action(fake_home: Home):
    d = fake_home.search_device_by_id("3014F71100000000000DRBL4")
    assert False is _channel_supports_action(
        d.functionalChannels[1], CliActions.SET_DIM_LEVEL
    )
    assert True is _channel_supports_action(
        d.functionalChannels[1], CliActions.SET_SHUTTER_LEVEL
    )