From 069ee75bf32c4f48d3e866a22459dce8dae9aae6 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 4 Nov 2022 09:03:18 +0100 Subject: [PATCH] Adds exception when no device is found --- src/models/exceptions.py | 2 ++ src/models/helper.py | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 src/models/exceptions.py diff --git a/src/models/exceptions.py b/src/models/exceptions.py new file mode 100644 index 0000000..57ff946 --- /dev/null +++ b/src/models/exceptions.py @@ -0,0 +1,2 @@ +class NoDeviceFoundError(Exception): + pass \ No newline at end of file diff --git a/src/models/helper.py b/src/models/helper.py index 8acc8dd..8e4f877 100644 --- a/src/models/helper.py +++ b/src/models/helper.py @@ -1,5 +1,6 @@ from typing import TypeVar from models.devices import GenericDevice, LightDevice, SwitchDevice +from models.exceptions import NoDeviceFoundError 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]: """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 \ No newline at end of file