mash-server/src/models/helper.py

17 lines
709 B
Python
Raw Normal View History

from typing import TypeVar
from models.devices 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-11-02 12:54:59 +01:00
DEVICE_TYPE = TypeVar("DEVICE_TYPE", type(GenericDevice), type(SwitchDevice), type(LightDevice), type(Room), type(DeviceGroup))
def filter_devices(devices: list[GenericDevice], type: DEVICE_TYPE) -> list[DEVICE_TYPE]:
"""Filters out devices that are not of a specific type."""
2022-11-04 09:03:18 +01:00
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