File: wifirepeater.py

package info (click to toggle)
python-miio 0.5.12-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,912 kB
  • sloc: python: 23,424; makefile: 9
file content (145 lines) | stat: -rw-r--r-- 4,276 bytes parent folder | download | duplicates (2)
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
import logging

import click

from .click_common import command, format_output
from .device import Device, DeviceStatus
from .exceptions import DeviceException

_LOGGER = logging.getLogger(__name__)


class WifiRepeaterException(DeviceException):
    pass


class WifiRepeaterStatus(DeviceStatus):
    def __init__(self, data):
        """
        Response of a xiaomi.repeater.v2:

        {
          'sta': {'count': 2, 'access_policy': 0},
          'mat': [
            {'mac': 'aa:aa:aa:aa:aa:aa', 'ip': '192.168.1.133', 'last_time': 54371873},
            {'mac': 'bb:bb:bb:bb:bb:bb', 'ip': '192.168.1.156', 'last_time': 54371496}
          ],
          'access_list': {'mac': ''}
        }
        """
        self.data = data

    @property
    def access_policy(self) -> int:
        """Access policy of the associated stations."""
        return self.data["sta"]["access_policy"]

    @property
    def associated_stations(self) -> dict:
        """List of associated stations."""
        return self.data["mat"]

    def __repr__(self) -> str:
        s = "<WifiRepeaterStatus access_policy=%s, " "associated_stations=%s>" % (
            self.access_policy,
            len(self.associated_stations),
        )
        return s


class WifiRepeaterConfiguration(DeviceStatus):
    def __init__(self, data):
        """Response of a xiaomi.repeater.v2:

        {'ssid': 'SSID', 'pwd': 'PWD', 'hidden': 0}
        """
        self.data = data

    @property
    def ssid(self) -> str:
        return self.data["ssid"]

    @property
    def password(self) -> str:
        return self.data["pwd"]

    @property
    def ssid_hidden(self) -> bool:
        return self.data["hidden"] == 1


class WifiRepeater(Device):
    """Device class for Xiaomi Mi WiFi Repeater 2."""

    _supported_models = ["xiaomi.repeater.v2"]

    @command(
        default_output=format_output(
            "",
            "Access policy: {result.access_policy}\n"
            "Associated stations: {result.associated_stations}\n",
        )
    )
    def status(self) -> WifiRepeaterStatus:
        """Return the associated stations."""
        return WifiRepeaterStatus(self.send("miIO.get_repeater_sta_info"))

    @command(
        default_output=format_output(
            "",
            "SSID: {result.ssid}\n"
            "Password: {result.password}\n"
            "SSID hidden: {result.ssid_hidden}\n",
        )
    )
    def configuration(self) -> WifiRepeaterConfiguration:
        """Return the configuration of the accesspoint."""
        return WifiRepeaterConfiguration(self.send("miIO.get_repeater_ap_info"))

    @command(
        click.argument("wifi_roaming", type=bool),
        default_output=format_output(
            lambda led: "Turning on WiFi roaming" if led else "Turning off WiFi roaming"
        ),
    )
    def set_wifi_roaming(self, wifi_roaming: bool):
        """Turn the WiFi roaming on/off."""
        return self.send(
            "miIO.switch_wifi_explorer", [{"wifi_explorer": int(wifi_roaming)}]
        )

    @command(
        click.argument("ssid", type=str),
        click.argument("password", type=str),
        click.argument("ssid_hidden", type=bool),
        default_output=format_output("Setting accesspoint configuration"),
    )
    def set_configuration(self, ssid: str, password: str, ssid_hidden: bool = False):
        """Update the configuration of the accesspoint."""
        return self.send(
            "miIO.switch_wifi_ssid",
            [
                {
                    "ssid": ssid,
                    "pwd": password,
                    "hidden": int(ssid_hidden),
                    "wifi_explorer": 0,
                }
            ],
        )

    @command(
        default_output=format_output(
            lambda result: "WiFi roaming is enabled"
            if result
            else "WiFi roaming is disabled"
        )
    )
    def wifi_roaming(self) -> bool:
        """Return the roaming setting."""
        return self.info().raw["desc"]["wifi_explorer"] == 1

    @command(default_output=format_output("RSSI of the accesspoint: {result}"))
    def rssi_accesspoint(self) -> int:
        """Received signal strength indicator of the accesspoint."""
        return self.info().accesspoint["rssi"]