Restructures devices, and breaks group implementation

This commit is contained in:
Maximilian Giller 2022-11-09 20:08:41 +01:00
parent 54eb509d4f
commit 8a5b86f7f3
9 changed files with 108 additions and 74 deletions

23
src/models/colors.py Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
class GenericDevice:
"""A generic device."""
def __init__(self, name: str):
self.name = name

View file

@ -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

View file

@ -1,6 +1,6 @@
from models.helper import filter_devices from models.helper import filter_devices
from models.devices import GenericDevice, LightDevice, SwitchDevice, AllDevice from models.devices import AllDevice
from models.light import LightColor, LightScene from models.colors import RGBColor
class DeviceGroup(AllDevice): class DeviceGroup(AllDevice):

View file

@ -1,5 +1,5 @@
from typing import TypeVar 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.exceptions import NoDeviceFoundError
from models.groups import DeviceGroup, Room from models.groups import DeviceGroup, Room

View file

@ -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