Skip to content

Commit

Permalink
Make the type a class attribute (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipediel committed Jan 29, 2021
1 parent 586d444 commit e12fd6f
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 56 deletions.
7 changes: 2 additions & 5 deletions broadlink/alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
class S1C(device):
"""Controls a Broadlink S1C."""

TYPE = "S1C"

_SENSORS_TYPES = {
0x31: "Door Sensor", # 49 as hex
0x91: "Key Fob", # 145 as hex, as serial on fob corpse
0x21: "Motion Sensor", # 33 as hex
}

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "S1C"

def get_sensors_status(self) -> dict:
"""Return the state of the sensors."""
packet = bytearray(16)
Expand Down
5 changes: 1 addition & 4 deletions broadlink/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
class hysen(device):
"""Controls a Hysen HVAC."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "Hysen heating controller"
TYPE = "Hysen heating controller"

# Send a request
# input_payload should be a bytearray, usually 6 bytes, e.g. bytearray([0x01,0x06,0x00,0x02,0x10,0x00])
Expand Down
5 changes: 1 addition & 4 deletions broadlink/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
class dooya(device):
"""Controls a Dooya curtain motor."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "Dooya DT360E"
TYPE = "Dooya DT360E"

def _send(self, magic1: int, magic2: int) -> int:
"""Send a packet to the device."""
Expand Down
4 changes: 3 additions & 1 deletion broadlink/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def ping(address: str, port: int = 80) -> None:
class device:
"""Controls a Broadlink device."""

TYPE = "Unknown"

def __init__(
self,
host: Tuple[str, int],
Expand All @@ -110,7 +112,7 @@ def __init__(
self.count = random.randint(0x8000, 0xFFFF)
self.iv = bytes.fromhex("562e17996d093d28ddb3ba695a2e6f58")
self.id = 0
self.type = "Unknown"
self.type = self.TYPE # For backwards compatibility.
self.lock = threading.Lock()

self.aes = None
Expand Down
7 changes: 2 additions & 5 deletions broadlink/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
class lb1(device):
"""Controls a Broadlink LB1."""

TYPE = "LB1"

@enum.unique
class ColorMode(enum.IntEnum):
"""Enumerates color modes."""
RGB = 0
WHITE = 1
SCENE = 2

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "LB1"

def get_state(self) -> dict:
"""Return the power state of the device.
Expand Down
10 changes: 2 additions & 8 deletions broadlink/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
class rm(device):
"""Controls a Broadlink RM."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "RM2"
TYPE = "RM2"

def _send(self, command: int, data: bytes = b'') -> bytes:
"""Send a packet to the device."""
Expand Down Expand Up @@ -65,10 +62,7 @@ def check_sensors(self) -> dict:
class rm4(rm):
"""Controls a Broadlink RM4."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "RM4"
TYPE = "RM4"

def _send(self, command: int, data: bytes = b'') -> bytes:
"""Send a packet to the device."""
Expand Down
7 changes: 2 additions & 5 deletions broadlink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
class a1(device):
"""Controls a Broadlink A1."""

TYPE = "A1"

_SENSORS_AND_LEVELS = (
("light", ("dark", "dim", "normal", "bright")),
("air_quality", ("excellent", "good", "normal", "bad")),
("noise", ("quiet", "normal", "noisy")),
)

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "A1"

def check_sensors(self) -> dict:
"""Return the state of the sensors."""
data = self.check_sensors_raw()
Expand Down
30 changes: 6 additions & 24 deletions broadlink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
class mp1(device):
"""Controls a Broadlink MP1."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "MP1"
TYPE = "MP1"

def set_power_mask(self, sid_mask: int, state: bool) -> None:
"""Set the power state of the device."""
Expand Down Expand Up @@ -70,10 +67,7 @@ def check_power(self) -> dict:
class bg1(device):
"""Controls a BG Electrical smart outlet."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "BG1"
TYPE = "BG1"

def get_state(self) -> dict:
"""Return the power state of the device.
Expand Down Expand Up @@ -151,10 +145,7 @@ def _decode(self, response: bytes) -> dict:
class sp1(device):
"""Controls a Broadlink SP1."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the device."""
device.__init__(self, *args, **kwargs)
self.type = "SP1"
TYPE = "SP1"

def set_power(self, state: bool) -> None:
"""Set the power state of the device."""
Expand All @@ -167,10 +158,7 @@ def set_power(self, state: bool) -> None:
class sp2(device):
"""Controls a Broadlink SP2."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "SP2"
TYPE = "SP2"

def set_power(self, state: bool) -> None:
"""Set the power state of the device."""
Expand Down Expand Up @@ -225,10 +213,7 @@ def get_energy(self) -> float:
class sp4(device):
"""Controls a Broadlink SP4."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "SP4"
TYPE = "SP4"

def set_power(self, state: bool) -> None:
"""Set the power state of the device."""
Expand Down Expand Up @@ -307,10 +292,7 @@ def _decode(self, response: bytes) -> dict:
class sp4b(sp4):
"""Controls a Broadlink SP4 (type B)."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "SP4B"
TYPE = "SP4B"

def get_state(self) -> dict:
"""Get full state of device."""
Expand Down

0 comments on commit e12fd6f

Please sign in to comment.