From 8a5b86f7f355728d9dedc6778a2eaba2683307cb Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Wed, 9 Nov 2022 20:08:41 +0100 Subject: [PATCH] Restructures devices, and breaks group implementation --- src/models/colors.py | 23 ++++++++++++++ src/models/devices.py | 47 ---------------------------- src/models/devices/__init__.py | 9 ++++++ src/models/devices/colordevices.py | 25 +++++++++++++++ src/models/devices/genericdevices.py | 5 +++ src/models/devices/switchdevices.py | 43 +++++++++++++++++++++++++ src/models/groups.py | 4 +-- src/models/helper.py | 2 +- src/models/light.py | 24 -------------- 9 files changed, 108 insertions(+), 74 deletions(-) create mode 100644 src/models/colors.py delete mode 100644 src/models/devices.py create mode 100644 src/models/devices/__init__.py create mode 100644 src/models/devices/colordevices.py create mode 100644 src/models/devices/genericdevices.py create mode 100644 src/models/devices/switchdevices.py delete mode 100644 src/models/light.py diff --git a/src/models/colors.py b/src/models/colors.py new file mode 100644 index 0000000..34a7d8a --- /dev/null +++ b/src/models/colors.py @@ -0,0 +1,23 @@ +class RGBColor: + """Represents a color in the RGB color space.""" + + r: int + """Red value of the color.""" + + g: int + """Green value of the color.""" + + b: int + """Blue value of the color.""" + + def __init__(self, r: int, g: int, b: int): + """Initialize the color. + + Args: + r (int): Red value of the color. + g (int): Green value of the color. + b (int): Blue value of the color. + """ + self.r = r + self.g = g + self.b = b diff --git a/src/models/devices.py b/src/models/devices.py deleted file mode 100644 index 4016d66..0000000 --- a/src/models/devices.py +++ /dev/null @@ -1,47 +0,0 @@ -import abc - -from models.light import LightColor - - -class GenericDevice: - """A generic device.""" - - def __init__(self, name: str): - self.name = name - - -class SwitchDevice(GenericDevice): - """Abstract device that can be turned on and off.""" - - def toggle(self): - self.is_on = not self.is_on - - @property - @abc.abstractmethod - def is_on(self) -> bool: - raise NotImplementedError - - @is_on.setter - @abc.abstractmethod - def is_on(self, is_on: bool): - raise NotImplementedError - - -class LightDevice(SwitchDevice): - """Abstract device that can be turned on and off and has a color.""" - - @property - @abc.abstractmethod - def color(self) -> LightColor: - raise NotImplementedError - - @color.setter - @abc.abstractmethod - def color(self, color: LightColor): - raise NotImplementedError - - -class AllDevice(LightDevice): - """Abstract device class that offers all device operations.""" - - pass diff --git a/src/models/devices/__init__.py b/src/models/devices/__init__.py new file mode 100644 index 0000000..2ab87ac --- /dev/null +++ b/src/models/devices/__init__.py @@ -0,0 +1,9 @@ +from models.devices.colordevices import * +from models.devices.switchdevices import * +from models.devices.genericdevices import * + + +class AllDevice(SetSwitchDevice, SetColorDevice): + """Inherits all intefaces from all devices.""" + + pass diff --git a/src/models/devices/colordevices.py b/src/models/devices/colordevices.py new file mode 100644 index 0000000..dd79539 --- /dev/null +++ b/src/models/devices/colordevices.py @@ -0,0 +1,25 @@ +from models.colors import RGBColor +from models.devices.genericdevices import GenericDevice + + +class ColorDevice(GenericDevice): + """Abstract device that has a color.""" + + _color: RGBColor + """Color of device.""" + + +class GetColorDevice(ColorDevice): + """Implement color getter for a color device.""" + + @property + def color(self) -> RGBColor: + return self._color + + +class SetColorDevice(GetColorDevice): + """Implements color setter for a color device.""" + + @GetColorDevice.color.setter + def color(self, color: RGBColor): + self._color = color diff --git a/src/models/devices/genericdevices.py b/src/models/devices/genericdevices.py new file mode 100644 index 0000000..a8eca85 --- /dev/null +++ b/src/models/devices/genericdevices.py @@ -0,0 +1,5 @@ +class GenericDevice: + """A generic device.""" + + def __init__(self, name: str): + self.name = name diff --git a/src/models/devices/switchdevices.py b/src/models/devices/switchdevices.py new file mode 100644 index 0000000..73b25df --- /dev/null +++ b/src/models/devices/switchdevices.py @@ -0,0 +1,43 @@ +from models.devices.genericdevices import GenericDevice + + +class SwitchDevice(GenericDevice): + """Abstract device that can be turned on and off.""" + + _is_on: bool + """Current state of the device.""" + + +class ToggleSwitchDevice(SwitchDevice): + """Implements toggle functionality for a switch device.""" + + def toggle(self): + self._is_on = not self._is_on + + +class TurnOffSwitchDevice(SwitchDevice): + """Implements turn off functionality for a switch device.""" + + def turn_off(self): + self._is_on = False + + +class TurnOnSwitchDevice(SwitchDevice): + """Implements turn on functionality for a switch device.""" + + def turn_on(self): + self._is_on = True + + +class GetSwitchDevice(SwitchDevice): + """Implements is_on state getter for a switch device.""" + + @property + def is_on(self) -> bool: + return self._is_on + + +class SetSwitchDevice(TurnOffSwitchDevice, TurnOnSwitchDevice, ToggleSwitchDevice): + @GetSwitchDevice.is_on.setter + def is_on(self, set_on: bool): + self._is_on = set_on diff --git a/src/models/groups.py b/src/models/groups.py index 104f8a4..3439793 100644 --- a/src/models/groups.py +++ b/src/models/groups.py @@ -1,6 +1,6 @@ from models.helper import filter_devices -from models.devices import GenericDevice, LightDevice, SwitchDevice, AllDevice -from models.light import LightColor, LightScene +from models.devices import AllDevice +from models.colors import RGBColor class DeviceGroup(AllDevice): diff --git a/src/models/helper.py b/src/models/helper.py index e167049..9ccbafe 100644 --- a/src/models/helper.py +++ b/src/models/helper.py @@ -1,5 +1,5 @@ from typing import TypeVar -from models.devices import GenericDevice, LightDevice, SwitchDevice +from models.devices.genericdevices import GenericDevice, LightDevice, SwitchDevice from models.exceptions import NoDeviceFoundError from models.groups import DeviceGroup, Room diff --git a/src/models/light.py b/src/models/light.py deleted file mode 100644 index 0dd0e2e..0000000 --- a/src/models/light.py +++ /dev/null @@ -1,24 +0,0 @@ -from models.devices import LightDevice - - -class LightColor: - """The color of a light source.""" - - def __init__(self, red: int, green: int, blue: int, is_on: bool = True): - self.red = red - self.green = green - self.blue = blue - self.is_on = is_on - - -class LightScene: - """A scene describing a state of a collection of light sources.""" - - def __init__(self, name: str, device_colors: dict[LightDevice, LightColor]): - self.name = name - self.device_colors = device_colors - - def set_scene(self): - """Sets the scene on all devices.""" - for device, color in self.device_colors.items(): - device.color = color