File: ceil.py

package info (click to toggle)
python-miio 0.5.12-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,888 kB
  • sloc: python: 23,425; makefile: 9
file content (189 lines) | stat: -rw-r--r-- 6,142 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import logging
from collections import defaultdict
from typing import Any, Dict

import click

from miio import Device, DeviceException, DeviceStatus
from miio.click_common import command, format_output

_LOGGER = logging.getLogger(__name__)


SUPPORTED_MODELS = ["philips.light.ceiling", "philips.light.zyceiling"]


class CeilException(DeviceException):
    pass


class CeilStatus(DeviceStatus):
    """Container for status reports from Xiaomi Philips LED Ceiling Lamp."""

    def __init__(self, data: Dict[str, Any]) -> None:
        # {'power': 'off', 'bright': 0, 'snm': 4, 'dv': 0,
        #  'cctsw': [[0, 3], [0, 2], [0, 1]], 'bl': 1,
        #  'mb': 1, 'ac': 1, 'mssw': 1, 'cct': 99}

        # NOTE: Only 8 properties can be requested at the same time
        self.data = data

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

    @property
    def is_on(self) -> bool:
        """True if the device is turned on."""
        return self.power == "on"

    @property
    def brightness(self) -> int:
        """Current brightness."""
        return self.data["bright"]

    @property
    def scene(self) -> int:
        """Current fixed scene (brightness & colortemp)."""
        return self.data["snm"]

    @property
    def delay_off_countdown(self) -> int:
        """Countdown until turning off in seconds."""
        return self.data["dv"]

    @property
    def color_temperature(self) -> int:
        """Current color temperature."""
        return self.data["cct"]

    @property
    def smart_night_light(self) -> bool:
        """Smart night mode state."""
        return self.data["bl"] == 1

    @property
    def automatic_color_temperature(self) -> bool:
        """Automatic color temperature state."""
        return self.data["ac"] == 1


class Ceil(Device):
    """Main class representing Xiaomi Philips LED Ceiling Lamp."""

    # TODO: - Auto On/Off Not Supported
    #       - Adjust Scenes with Wall Switch Not Supported

    _supported_models = SUPPORTED_MODELS

    @command(
        default_output=format_output(
            "",
            "Power: {result.power}\n"
            "Brightness: {result.brightness}\n"
            "Color temperature: {result.color_temperature}\n"
            "Scene: {result.scene}\n"
            "Delayed turn off: {result.delay_off_countdown}\n"
            "Smart night light: {result.smart_night_light}\n"
            "Automatic color temperature: {result.automatic_color_temperature}\n",
        )
    )
    def status(self) -> CeilStatus:
        """Retrieve properties."""
        properties = ["power", "bright", "cct", "snm", "dv", "bl", "ac"]
        values = self.get_properties(properties)

        return CeilStatus(defaultdict(lambda: None, zip(properties, values)))

    @command(default_output=format_output("Powering on"))
    def on(self):
        """Power on."""
        return self.send("set_power", ["on"])

    @command(default_output=format_output("Powering on"))
    def off(self):
        """Power off."""
        return self.send("set_power", ["off"])

    @command(
        click.argument("level", type=int),
        default_output=format_output("Setting brightness to {level}"),
    )
    def set_brightness(self, level: int):
        """Set brightness level."""
        if level < 1 or level > 100:
            raise CeilException("Invalid brightness: %s" % level)

        return self.send("set_bright", [level])

    @command(
        click.argument("level", type=int),
        default_output=format_output("Setting color temperature to {level}"),
    )
    def set_color_temperature(self, level: int):
        """Set Correlated Color Temperature."""
        if level < 1 or level > 100:
            raise CeilException("Invalid color temperature: %s" % level)

        return self.send("set_cct", [level])

    @command(
        click.argument("brightness", type=int),
        click.argument("cct", type=int),
        default_output=format_output(
            "Setting brightness to {brightness} and color temperature to {cct}"
        ),
    )
    def set_brightness_and_color_temperature(self, brightness: int, cct: int):
        """Set brightness level and the correlated color temperature."""
        if brightness < 1 or brightness > 100:
            raise CeilException("Invalid brightness: %s" % brightness)

        if cct < 1 or cct > 100:
            raise CeilException("Invalid color temperature: %s" % cct)

        return self.send("set_bricct", [brightness, cct])

    @command(
        click.argument("seconds", type=int),
        default_output=format_output("Setting delayed turn off to {seconds} seconds"),
    )
    def delay_off(self, seconds: int):
        """Turn off delay in seconds."""

        if seconds < 1:
            raise CeilException("Invalid value for a delayed turn off: %s" % seconds)

        return self.send("delay_off", [seconds])

    @command(
        click.argument("number", type=int),
        default_output=format_output("Setting fixed scene to {number}"),
    )
    def set_scene(self, number: int):
        """Set a fixed scene (1-4)."""
        if number < 1 or number > 4:
            raise CeilException("Invalid fixed scene number: %s" % number)

        return self.send("apply_fixed_scene", [number])

    @command(default_output=format_output("Turning on smart night light"))
    def smart_night_light_on(self):
        """Smart Night Light On."""
        return self.send("enable_bl", [1])

    @command(default_output=format_output("Turning off smart night light"))
    def smart_night_light_off(self):
        """Smart Night Light off."""
        return self.send("enable_bl", [0])

    @command(default_output=format_output("Turning on automatic color temperature"))
    def automatic_color_temperature_on(self):
        """Automatic color temperature on."""
        return self.send("enable_ac", [1])

    @command(default_output=format_output("Turning off automatic color temperature"))
    def automatic_color_temperature_off(self):
        """Automatic color temperature off."""
        return self.send("enable_ac", [0])