8000 Rename bulb interface to light and move fan and light interface to in… · python-kasa/python-kasa@d7b0033 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7b0033

Browse files
authored
Rename bulb interface to light and move fan and light interface to interfaces (#910)
Also rename BulbPreset to LightPreset.
1 parent f259a8f commit d7b0033

File tree

11 files changed

+59
-44
lines changed

11 files changed

+59
-44
lines changed

kasa/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from typing import TYPE_CHECKING
1717
from warnings import warn
1818

19-
from kasa.bulb import Bulb, BulbPreset
2019
from kasa.credentials import Credentials
2120
from kasa.device import Device
2221
from kasa.device_type import DeviceType
@@ -36,6 +35,7 @@
3635
UnsupportedDeviceError,
3736
)
3837
from kasa.feature import Feature
38+
from kasa.interfaces.light import Light, LightPreset
3939
from kasa.iotprotocol import (
4040
IotProtocol,
4141
_deprecated_TPLinkSmartHomeProtocol, # noqa: F401
@@ -52,14 +52,14 @@
5252
"BaseProtocol",
5353
"IotProtocol",
5454
"SmartProtocol",
55-
"BulbPreset",
55+
"LightPreset",
5656
"TurnOnBehaviors",
5757
"TurnOnBehavior",
5858
"DeviceType",
5959
"Feature",
6060
"EmeterStatus",
6161
"Device",
62-
"Bulb",
62+
"Light",
6363
"Plug",
6464
"Module",
6565
"KasaException",
@@ -84,7 +84,7 @@
8484
"SmartLightStrip": iot.IotLightStrip,
8585
"SmartStrip": iot.IotStrip,
8686
"SmartDimmer": iot.IotDimmer,
87-
"SmartBulbPreset": BulbPreset,
87+
"SmartBulbPreset": LightPreset,
8888
}
8989
deprecated_exceptions = {
9090
"SmartDeviceException": KasaException,
@@ -124,7 +124,7 @@ def __getattr__(name):
124124
SmartLightStrip = iot.IotLightStrip
125125
SmartStrip = iot.IotStrip
126126
SmartDimmer = iot.IotDimmer
127-
SmartBulbPreset = BulbPreset
127+
SmartBulbPreset = LightPreset
128128

129129
SmartDeviceException = KasaException
130130
UnsupportedDeviceException = UnsupportedDeviceError

kasa/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from kasa import (
2020
AuthenticationError,
21-
Bulb,
2221
ConnectionType,
2322
Credentials,
2423
Device,
@@ -28,6 +27,7 @@
2827
EncryptType,
2928
Feature,
3029
KasaException,
30+
Light,
3131
UnsupportedDeviceError,
3232
)
3333
from kasa.discover import DiscoveryResult
@@ -859,7 +859,7 @@ async def usage(dev: Device, year, month, erase):
859859
@click.argument("brightness", type=click.IntRange(0, 100), default=None, required=False)
860860
@click.option("--transition", type=int, required=False)
861861
@pass_dev
862-
async def brightness(dev: Bulb, brightness: int, transition: int):
862+
async def brightness(dev: Light, brightness: int, transition: int):
863863
"""Get or set brightness."""
864864
if not dev.is_dimmable:
865865
echo("This device does not support brightness.")
@@ -879,7 +879,7 @@ async def brightness(dev: Bulb, brightness: int, transition: int):
879879
)
880880
@click.option("--transition", type=int, required=False)
881881
@pass_dev
882-
async def temperature(dev: Bulb, temperature: int, transition: int):
882+
async def temperature(dev: Light, temperature: int, transition: int):
883883
"""Get or set color temperature."""
884884
if not dev.is_variable_color_temp:
885885
echo("Device does not support color temperature")

kasa/interfaces/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Package for interfaces."""
2+
3+
from .fan import Fan
4+
from .led import Led
5+
from .light import Light, LightPreset
6+
from .lighteffect import LightEffect
7+
8+
__all__ = [
9+
"Fan",
10+
"Led",
11+
"Light",
12+
"LightEffect",
13+
"LightPreset",
14+
]

kasa/fan.py renamed to kasa/interfaces/fan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from abc import ABC, abstractmethod
66

7-
from .device import Device
7+
from ..device import Device
88

99

1010
class Fan(Device, ABC):

kasa/bulb.py renamed to kasa/interfaces/light.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from pydantic.v1 import BaseModel
99

10-
from .device import Device
10+
from ..device import Device
1111

1212

1313
class ColorTempRange(NamedTuple):
@@ -25,8 +25,8 @@ class HSV(NamedTuple):
2525
value: int
2626

2727

28-
class BulbPreset(BaseModel):
29-
"""Bulb configuration preset."""
28+
class LightPreset(BaseModel):
29+
"""Light configuration preset."""
3030

3131
index: int
3232
brightness: int
@@ -42,8 +42,8 @@ class BulbPreset(BaseModel):
4242
mode: Optional[int] # noqa: UP007
4343

4444

45-
class Bulb(Device, ABC):
46-
"""Base class for TP-Link Bulb."""
45+
class Light(Device, ABC):
46+
"""Base class for TP-Link Light."""
4747

4848
def _raise_for_invalid_brightness(self, value):
4949
if not isinstance(value, int) or not (0 <= value <= 100):
@@ -135,5 +135,5 @@ async def set_brightness(
135135

136136
@property
137137
@abstractmethod
138-
def presets(self) -> list[BulbPreset]:
138+
def presets(self) -> list[LightPreset]:
139139
"""Return a list of available bulb setting presets."""

kasa/iot/iotbulb.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
from pydantic.v1 import BaseModel, Field, root_validator
1111

12-
from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
1312
from ..device_type import DeviceType
1413
from ..deviceconfig import DeviceConfig
1514
from ..feature import Feature
15+
from ..interfaces.light import HSV, ColorTempRange, Light, LightPreset
1616
from ..module import Module
1717
from ..protocol import BaseProtocol
1818
from .iotdevice import IotDevice, KasaException, requires_update
@@ -88,7 +88,7 @@ class TurnOnBehaviors(BaseModel):
8888
_LOGGER = logging.getLogger(__name__)
8989

9090

91-
class IotBulb(IotDevice, Bulb):
91+
class IotBulb(IotDevice, Light):
9292
r"""Representation of a TP-Link Smart Bulb.
9393
9494
To initialize, you have to await :func:`update()` at least once.
@@ -170,9 +170,9 @@ class IotBulb(IotDevice, Bulb):
170170
Bulb configuration presets can be accessed using the :func:`presets` property:
171171
172172
>>> bulb.presets
173-
[BulbPreset(index=0, brightness=50, hue=0, saturation=0, color_temp=2700, custom=None, id=None, mode=None), BulbPreset(index=1, brightness=100, hue=0, saturation=75, color_temp=0, custom=None, id=None, mode=None), BulbPreset(index=2, brightness=100, hue=120, saturation=75, color_temp=0, custom=None, id=None, mode=None), BulbPreset(index=3, brightness=100, hue=240, saturation=75, color_temp=0, custom=None, id=None, mode=None)]
173+
[LightPreset(index=0, brightness=50, hue=0, saturation=0, color_temp=2700, custom=None, id=None, mode=None), LightPreset(index=1, brightness=100, hue=0, saturation=75, color_temp=0, custom=None, id=None, mode=None), LightPreset(index=2, brightness=100, hue=120, saturation=75, color_temp=0, custom=None, id=None, mode=None), LightPreset(index=3, brightness=100, hue=240, saturation=75, color_temp=0, custom=None, id=None, mode=None)]
174174
175-
To modify an existing preset, pass :class:`~kasa.smartbulb.SmartBulbPreset`
175+
To modify an existing preset, pass :class:`~kasa.smartbulb.LightPreset`
176176
instance to :func:`save_preset` method:
177177
178178
>>> preset = bulb.presets[0]
@@ -523,11 +523,11 @@ async def set_alias(self, alias: str) -> None:
523523

524524
@property # type: ignore
525525
@requires_update
526-
def presets(self) -> list[BulbPreset]:
526+
def presets(self) -> list[LightPreset]:
527527
"""Return a list of available bulb setting presets."""
528-
return [BulbPreset(**vals) for vals in self.sys_info["preferred_state"]]
528+
return [LightPreset(**vals) for vals in self.sys_info["preferred_state"]]
529529

530-
async def save_preset(self, preset: BulbPreset):
530+
async def save_preset(self, preset: LightPreset):
531531
"""Save a setting preset.
532532
533533
You can either construct a preset object manually, or pass an existing one

kasa/smart/modules/color.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from typing import TYPE_CHECKING
66

7-
from ...bulb import HSV
87
from ...feature import Feature
8+
from ...interfaces.light import HSV
99
from ..smartmodule import SmartModule
1010

1111
if TYPE_CHECKING:

kasa/smart/modules/colortemperature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import logging
66
from typing import TYPE_CHECKING
77

8-
from ...bulb import ColorTempRange
98
from ...feature import Feature
9+
from ...interfaces.light import ColorTempRange
1010
from ..smartmodule import SmartModule
1111

1212
if TYPE_CHECKING:

kasa/smart/smartdevice.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast
99

1010
from ..aestransport import AesTransport
11-
from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
1211
from ..device import Device, WifiNetwork
1312
from ..device_type import DeviceType
1413
from ..deviceconfig import DeviceConfig
1514
from ..emeterstatus import EmeterStatus
1615
from ..exceptions import AuthenticationError, DeviceError, KasaException, SmartErrorCode
17-
from ..fan import Fan
1816
from ..feature import Feature
17+
from ..interfaces.fan import Fan
18+
from ..interfaces.light import HSV, ColorTempRange, Light, LightPreset
1919
from ..module import Module
2020
from ..modulemapping import ModuleMapping, ModuleName
2121
from ..smartprotocol import SmartProtocol
@@ -39,7 +39,7 @@
3939

4040
# Device must go last as the other interfaces also inherit Device
4141
# and python needs a consistent method resolution order.
42-
class SmartDevice(Bulb, Fan, Device):
42+
class SmartDevice(Light, Fan, Device):
4343
"""Base class to represent a SMART protocol based device."""
4444

4545
def __init__(
@@ -766,7 +766,7 @@ async def set_brightness(
766766
return await self.modules[Module.Brightness].set_brightness(brightness)
767767

768768
@property
769-
def presets(self) -> list[BulbPreset]:
769+
def presets(self) -> list[LightPreset]:
770770
"""Return a list of available bulb setting presets."""
771771
return []
772772

kasa/tests/test_bulb.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Schema,
88
)
99

10-
from kasa import Bulb, BulbPreset, Device, DeviceType, KasaException
10+
from kasa import Device, DeviceType, KasaException, Light, LightPreset
1111
from kasa.iot import IotBulb, IotDimmer
1212
from kasa.smart import SmartDevice
1313

@@ -65,7 +65,7 @@ async def test_get_light_state(dev: IotBulb):
6565
@color_bulb
6666
@turn_on
6767
async def test_hsv(dev: Device, turn_on):
68-
assert isinstance(dev, Bulb)
68+
assert isinstance(dev, Light)
6969
await handle_turn_on(dev, turn_on)
7070
assert dev.is_color
7171

@@ -96,7 +96,7 @@ async def test_set_hsv_transition(dev: IotBulb, mocker):
9696

9797
@color_bulb
9898
@turn_on
99-
async def test_invalid_hsv(dev: Bulb, turn_on):
99+
async def test_invalid_hsv(dev: Light, turn_on):
100100
await handle_turn_on(dev, turn_on)
101101
assert dev.is_color
102102

@@ -116,13 +116,13 @@ async def test_invalid_hsv(dev: Bulb, turn_on):
116116
@color_bulb
117117
@pytest.mark.skip("requires color feature")
118118
async def test_color_state_information(dev: Device):
119-
assert isinstance(dev, Bulb)
119+
assert isinstance(dev, Light)
120120
assert "HSV" in dev.state_information
121121
assert dev.state_information["HSV"] == dev.hsv
122122

123123

124124
@non_color_bulb
125-
async def test_hsv_on_non_color(dev: Bulb):
125+
async def test_hsv_on_non_color(dev: Light):
126126
assert not dev.is_color
127127

128128
with pytest.raises(KasaException):
@@ -134,15 +134,15 @@ async def test_hsv_on_non_color(dev: Bulb):
134134
@variable_temp
135135
@pytest.mark.skip("requires colortemp module")
136136
async def test_variable_temp_state_information(dev: Device):
137-
assert isinstance(dev, Bulb)
137+
assert isinstance(dev, Light)
138138
assert "Color temperature" in dev.state_information
139139
assert dev.state_information["Color temperature"] == dev.color_temp
140140

141141

142142
@variable_temp
143143
@turn_on
144144
async def test_try_set_colortemp(dev: Device, turn_on):
145-
assert isinstance(dev, Bulb)
145+
assert isinstance(dev, Light)
146146
await handle_turn_on(dev, turn_on)
147147
await dev.set_color_temp(2700)
148148
await dev.update()
@@ -171,15 +171,15 @@ async def test_smart_temp_range(dev: SmartDevice):
171171

172172

173173
@variable_temp
174-
async def test_out_of_range_temperature(dev: Bulb):
174+
async def test_out_of_range_temperature(dev: Light):
175175
with pytest.raises(ValueError):
176176
await dev.set_color_temp(1000)
177177
with pytest.raises(ValueError):
178178
await dev.set_color_temp(10000)
179179

180180

181181
@non_variable_temp
182-
async def test_non_variable_temp(dev: Bulb):
182+
async def test_non_variable_temp(dev: Light):
183183
with pytest.raises(KasaException):
184184
await dev.set_color_temp(2700)
185185

@@ -193,7 +193,7 @@ async def test_non_variable_temp(dev: Bulb):
193193
@dimmable
194194
@turn_on
195195
async def test_dimmable_brightness(dev: Device, turn_on):
196-
assert isinstance(dev, (Bulb, IotDimmer))
196+
assert isinstance(dev, (Light, IotDimmer))
197197
await handle_turn_on(dev, turn_on)
198198
assert dev.is_dimmable
199199

@@ -230,7 +230,7 @@ async def test_dimmable_brightness_transition(dev: IotBulb, mocker):
230230

231231

232232
@dimmable
233-
async def test_invalid_brightness(dev: Bulb):
233+
async def test_invalid_brightness(dev: Light):
234234
assert dev.is_dimmable
235235

236236
with pytest.raises(ValueError):
@@ -241,7 +241,7 @@ async def test_invalid_brightness(dev: Bulb):
241241

242242

243243
@non_dimmable
244-
async def test_non_dimmable(dev: Bulb):
244+
async def test_non_dimmable(dev: Light):
245245
assert not dev.is_dimmable
246246

247247
with pytest.raises(KasaException):
@@ -291,7 +291,7 @@ async def test_modify_preset(dev: IotBulb, mocker):
291291
"saturation": 0,
292292
"color_temp": 0,
293293
}
294-
preset = BulbPreset(**data)
294+
preset = LightPreset(**data)
295295

296296
assert preset.index == 0
297297
assert preset.brightness == 10
@@ -305,7 +305,7 @@ async def test_modify_preset(dev: IotBulb, mocker):
305305

306306
with pytest.raises(KasaException):
307307
await dev.save_preset(
308-
BulbPreset(index=5, hue=0, brightness=0, saturation=0, color_temp=0)
308+
LightPreset(index=5, hue=0, brightness=0, saturation=0, color_temp=0)
309309
)
310310

311311

@@ -314,11 +314,11 @@ async def test_modify_preset(dev: IotBulb, mocker):
314314
("preset", "payload"),
315315
[
316316
(
317-
BulbPreset(index=0, hue=0, brightness=1, saturation=0),
317+
LightPreset(index=0, hue=0, brightness=1, saturation=0),
318318
{"index": 0, "hue": 0, "brightness": 1, "saturation": 0},
319319
),
320320
(
321-
BulbPreset(index=0, brightness=1, id="testid", mode=2, custom=0),
321+
LightPreset(index=0, brightness=1, id="testid", mode=2, custom=0),
322322
{"index": 0, "brightness": 1, "id": "testid", "mode": 2, "custom": 0},
323323
),
324324
],

kasa/tests/test_device.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def _get_subclasses(of_class):
2525
inspect.isclass(obj)
2626
and issubclass(obj, of_class)
2727
and module.__package__ != "kasa"
28+
and module.__package__ != "kasa.interfaces"
2829
):
2930
subclasses.add((module.__package__ + "." + name, obj))
3031
return subclasses

0 commit comments

Comments
 (0)
0