2022-10-19 03:43:58 +02:00
|
|
|
from typing import TypeVar
|
2022-11-09 20:08:41 +01:00
|
|
|
from models.devices.genericdevices import GenericDevice, LightDevice, SwitchDevice
|
2022-11-04 09:03:18 +01:00
|
|
|
from models.exceptions import NoDeviceFoundError
|
2022-11-02 12:54:59 +01:00
|
|
|
from models.groups import DeviceGroup, Room
|
2022-10-19 03:43:58 +02:00
|
|
|
|
|
|
|
|
2022-11-04 09:03:53 +01:00
|
|
|
DEVICE_TYPE = TypeVar(
|
|
|
|
"DEVICE_TYPE",
|
|
|
|
type(GenericDevice),
|
|
|
|
type(SwitchDevice),
|
|
|
|
type(LightDevice),
|
|
|
|
type(Room),
|
|
|
|
type(DeviceGroup),
|
|
|
|
)
|
2022-10-19 03:43:58 +02:00
|
|
|
|
|
|
|
|
2022-11-04 09:03:53 +01:00
|
|
|
def filter_devices(
|
|
|
|
devices: list[GenericDevice], type: DEVICE_TYPE
|
|
|
|
) -> list[DEVICE_TYPE]:
|
2022-10-19 03:43:58 +02:00
|
|
|
"""Filters out devices that are not of a specific type."""
|
2022-11-04 09:07:30 +01:00
|
|
|
filtered_devices: list[DEVICE_TYPE] = [
|
2022-11-04 09:03:53 +01:00
|
|
|
device for device in devices if isinstance(device, type)
|
|
|
|
]
|
|
|
|
|
2022-11-04 09:03:18 +01:00
|
|
|
if len(filtered_devices) == 0:
|
|
|
|
raise NoDeviceFoundError(f"No devices of type {type} found.")
|
2022-11-04 09:03:53 +01:00
|
|
|
|
|
|
|
return filtered_devices
|