Adds exception when no device is found

This commit is contained in:
Maximilian Giller 2022-11-04 09:03:18 +01:00
parent 0b247755d3
commit 069ee75bf3
2 changed files with 9 additions and 1 deletions

2
src/models/exceptions.py Normal file
View file

@ -0,0 +1,2 @@
class NoDeviceFoundError(Exception):
pass

View file

@ -1,5 +1,6 @@
from typing import TypeVar from typing import TypeVar
from models.devices import GenericDevice, LightDevice, SwitchDevice from models.devices import GenericDevice, LightDevice, SwitchDevice
from models.exceptions import NoDeviceFoundError
from models.groups import DeviceGroup, Room from models.groups import DeviceGroup, Room
@ -8,4 +9,9 @@ DEVICE_TYPE = TypeVar("DEVICE_TYPE", type(GenericDevice), type(SwitchDevice), ty
def filter_devices(devices: list[GenericDevice], type: DEVICE_TYPE) -> list[DEVICE_TYPE]: def filter_devices(devices: list[GenericDevice], type: DEVICE_TYPE) -> list[DEVICE_TYPE]:
"""Filters out devices that are not of a specific type.""" """Filters out devices that are not of a specific type."""
return [device for device in devices if isinstance(device, DEVICE_TYPE)] filtered_devices: list[type] = [device for device in devices if isinstance(device, type)]
if len(filtered_devices) == 0:
raise NoDeviceFoundError(f"No devices of type {type} found.")
return filtered_devices