Files
pyMC_Repeater/repeater/sensors/registry.py
T
Lloyd 45a44eb47b Refactor test cases and base code for consistency and readability
- Updated byte representations in tests to use lowercase hex format for consistency.
- Reformatted code for better readability, including line breaks and indentation adjustments.
- Consolidated multiple lines into single lines where appropriate to enhance clarity.
- Ensured that all test cases maintain consistent formatting and style across the test suite.
2026-05-27 20:15:10 +01:00

41 lines
1.3 KiB
Python

from __future__ import annotations
from typing import Any, Callable, Dict, Optional
from .base import SensorBase
class SensorRegistry:
_factories: Dict[str, Callable[..., SensorBase]] = {}
@classmethod
def register(cls, sensor_type: str, factory: Optional[Callable[..., SensorBase]] = None):
"""Register a sensor factory or class under a sensor type."""
key = str(sensor_type).strip().lower()
def _decorator(target):
cls._factories[key] = target
return target
if factory is not None:
cls._factories[key] = factory
return factory
return _decorator
@classmethod
def create(
cls, sensor_type: str, name: str, config: Optional[Dict[str, Any]] = None, **kwargs
) -> SensorBase:
key = str(sensor_type).strip().lower()
factory = cls._factories.get(key)
if factory is None:
raise ValueError(f"Unknown sensor type: {sensor_type}")
if isinstance(factory, type) and issubclass(factory, SensorBase):
return factory(name=name, config=config, **kwargs)
return factory(name=name, config=config, **kwargs)
@classmethod
def available_types(cls) -> list[str]:
return sorted(cls._factories)