Skip to content

Commit 31a2686

Browse files
authored
Additional refactoring to make it easier to add new devices (#62)
1 parent aca522f commit 31a2686

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

switchbot/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Library to handle connection with Switchbot."""
22
from __future__ import annotations
33

4-
from .adv_parser import parse_advertisement_data
4+
from .adv_parser import SwitchbotSupportedType, parse_advertisement_data
5+
from .const import SwitchbotModel
56
from .devices.bot import Switchbot
67
from .devices.curtain import SwitchbotCurtain
78
from .devices.device import SwitchbotDevice
@@ -17,4 +18,6 @@
1718
"SwitchbotCurtain",
1819
"Switchbot",
1920
"SwitchbotPlugMini",
21+
"SwitchbotSupportedType",
22+
"SwitchbotModel",
2023
]

switchbot/adv_parser.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,56 @@
1414
from .adv_parsers.meter import process_wosensorth
1515
from .adv_parsers.motion import process_wopresence
1616
from .adv_parsers.plug import process_woplugmini
17+
from .const import SwitchbotModel
1718
from .models import SwitchBotAdvertisement
1819

1920

2021
class SwitchbotSupportedType(TypedDict):
2122
"""Supported type of Switchbot."""
2223

23-
modelName: str
24+
modelName: SwitchbotModel
2425
modelFriendlyName: str
2526
func: Callable[[bytes, bytes | None], dict[str, bool | int]]
2627

2728

2829
SUPPORTED_TYPES: dict[str, SwitchbotSupportedType] = {
2930
"d": {
30-
"modelName": "WoContact",
31+
"modelName": SwitchbotModel.CONTACT_SENSOR,
3132
"modelFriendlyName": "Contact Sensor",
3233
"func": process_wocontact,
3334
},
34-
"H": {"modelName": "WoHand", "modelFriendlyName": "Bot", "func": process_wohand},
35+
"H": {
36+
"modelName": SwitchbotModel.BOT,
37+
"modelFriendlyName": "Bot",
38+
"func": process_wohand,
39+
},
3540
"s": {
36-
"modelName": "WoPresence",
41+
"modelName": SwitchbotModel.MOTION_SENSOR,
3742
"modelFriendlyName": "Motion Sensor",
3843
"func": process_wopresence,
3944
},
4045
"c": {
41-
"modelName": "WoCurtain",
46+
"modelName": SwitchbotModel.CURTAIN,
4247
"modelFriendlyName": "Curtain",
4348
"func": process_wocurtain,
4449
},
4550
"T": {
46-
"modelName": "WoSensorTH",
51+
"modelName": SwitchbotModel.METER,
4752
"modelFriendlyName": "Meter",
4853
"func": process_wosensorth,
4954
},
5055
"i": {
51-
"modelName": "WoSensorTH",
56+
"modelName": SwitchbotModel.METER,
5257
"modelFriendlyName": "Meter Plus",
5358
"func": process_wosensorth,
5459
},
5560
"g": {
56-
"modelName": "WoPlug",
61+
"modelName": SwitchbotModel.PLUG_MINI,
5762
"modelFriendlyName": "Plug Mini",
5863
"func": process_woplugmini,
5964
},
6065
"u": {
61-
"modelName": "WoBulb",
66+
"modelName": SwitchbotModel.COLOR_BULB,
6267
"modelFriendlyName": "Color Bulb",
6368
"func": process_color_bulb,
6469
},

switchbot/const.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,16 @@
44
DEFAULT_RETRY_COUNT = 3
55
DEFAULT_RETRY_TIMEOUT = 1
66
DEFAULT_SCAN_TIMEOUT = 5
7+
8+
from .enum import StrEnum
9+
10+
11+
class SwitchbotModel(StrEnum):
12+
13+
BOT = "WoHand"
14+
CURTAIN = "WoCurtain"
15+
PLUG_MINI = "WoPlug"
16+
CONTACT_SENSOR = "WoContact"
17+
METER = "WoSensorTH"
18+
MOTION_SENSOR = "WoPresence"
19+
COLOR_BULB = "WoBulb"

switchbot/enum.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Enum backports from standard lib."""
2+
from __future__ import annotations
3+
4+
from enum import Enum
5+
from typing import Any, TypeVar
6+
7+
_StrEnumT = TypeVar("_StrEnumT", bound="StrEnum")
8+
9+
10+
class StrEnum(str, Enum):
11+
"""Partial backport of Python 3.11's StrEnum for our basic use cases."""
12+
13+
def __new__(
14+
cls: type[_StrEnumT], value: str, *args: Any, **kwargs: Any
15+
) -> _StrEnumT:
16+
"""Create a new StrEnum instance."""
17+
if not isinstance(value, str):
18+
raise TypeError(f"{value!r} is not a string")
19+
return super().__new__(cls, value, *args, **kwargs)
20+
21+
def __str__(self) -> str:
22+
"""Return self.value."""
23+
return str(self.value)

0 commit comments

Comments
 (0)