mash-server/src/models/helper.py

29 lines
749 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-04 09:03:53 +01:00
DEVICE_TYPE = TypeVar(
"DEVICE_TYPE",
type(GenericDevice),
type(SwitchDevice),
type(LightDevice),
type(Room),
type(DeviceGroup),
)
2022-11-04 09:03:53 +01:00
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:53 +01:00
filtered_devices: list[type] = [
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