|
1 | | -from typing import Any |
| 1 | +import logging |
| 2 | +from typing import Generic, TypeVar, cast |
2 | 3 |
|
| 4 | +from pyomnilogic_local.api.api import OmniLogicAPI |
3 | 5 | from pyomnilogic_local.models import MSPEquipmentType, Telemetry |
| 6 | +from pyomnilogic_local.models.telemetry import TelemetryType |
4 | 7 |
|
| 8 | +# Define type variables for generic equipment types |
| 9 | +MSPConfigT = TypeVar("MSPConfigT", bound=MSPEquipmentType) |
| 10 | +TelemetryT = TypeVar("TelemetryT", bound=TelemetryType | None) |
5 | 11 |
|
6 | | -class OmniEquipment: |
7 | | - """Base class for OmniLogic equipment.""" |
8 | 12 |
|
9 | | - def __init__(self, mspconfig: MSPEquipmentType, telemetry: Telemetry | None = None) -> None: |
10 | | - """Initialize the equipment with configuration and telemetry data.""" |
11 | | - # If the Equipment has subdevices, we don't store those as part of this device's config |
12 | | - # They will get parsed and stored as their own equipment instances |
| 13 | +_LOGGER = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class OmniEquipment(Generic[MSPConfigT, TelemetryT]): |
| 17 | + """Base class for OmniLogic equipment. |
| 18 | +
|
| 19 | + Generic parameters: |
| 20 | + MSPConfigT: The specific MSP configuration type (e.g., MSPBoW, MSPRelay) |
| 21 | + TelemetryT: The specific telemetry type (e.g., TelemetryBoW, TelemetryRelay, or None for equipment without telemetry) |
| 22 | + """ |
| 23 | + |
| 24 | + mspconfig: MSPConfigT |
| 25 | + telemetry: TelemetryT |
| 26 | + |
| 27 | + # Use a forward reference for the type hint to avoid issues with self-referential generics |
| 28 | + child_equipment: dict[int, "OmniEquipment[MSPConfigT, TelemetryT]"] |
| 29 | + |
| 30 | + def __init__(self, _api: OmniLogicAPI, mspconfig: MSPConfigT, telemetry: Telemetry | None) -> None: |
| 31 | + """Initialize the equipment with configuration and telemetry data. |
| 32 | +
|
| 33 | + Args: |
| 34 | + _api: The OmniLogic API instance |
| 35 | + mspconfig: The MSP configuration for this specific equipment |
| 36 | + telemetry: The full Telemetry object containing all equipment telemetry |
| 37 | + """ |
| 38 | + self._api = _api |
| 39 | + |
| 40 | + self.update_config(mspconfig) |
| 41 | + |
| 42 | + if telemetry is not None: |
| 43 | + self.update_telemetry(telemetry) |
| 44 | + |
| 45 | + @property |
| 46 | + def bow_id(self) -> int | None: |
| 47 | + """The bow ID of the equipment.""" |
| 48 | + return self.mspconfig.bow_id |
| 49 | + |
| 50 | + @property |
| 51 | + def name(self) -> str | None: |
| 52 | + """The name of the equipment.""" |
| 53 | + return self.mspconfig.name |
| 54 | + |
| 55 | + @property |
| 56 | + def system_id(self) -> int | None: |
| 57 | + """The system ID of the equipment.""" |
| 58 | + return self.mspconfig.system_id |
| 59 | + |
| 60 | + @property |
| 61 | + def omni_type(self) -> str | None: |
| 62 | + """The OmniType of the equipment.""" |
| 63 | + return self.mspconfig.omni_type |
| 64 | + |
| 65 | + def update(self, mspconfig: MSPConfigT, telemetry: Telemetry | None) -> None: |
| 66 | + """Update both the configuration and telemetry data for the equipment.""" |
| 67 | + self.update_config(mspconfig) |
| 68 | + if telemetry is not None: |
| 69 | + self.update_telemetry(telemetry) |
| 70 | + |
| 71 | + self._update_equipment(mspconfig, telemetry) |
| 72 | + |
| 73 | + def _update_equipment(self, mspconfig: MSPConfigT, telemetry: Telemetry | None) -> None: |
| 74 | + """Hook to allow classes to trigger updates of sub-equipment.""" |
| 75 | + |
| 76 | + def update_config(self, mspconfig: MSPConfigT) -> None: |
| 77 | + """Update the configuration data for the equipment.""" |
13 | 78 | try: |
14 | | - self.mspconfig = mspconfig.without_subdevices() |
| 79 | + # If the Equipment has subdevices, we don't store those as part of this device's config |
| 80 | + # They will get parsed and stored as their own equipment instances |
| 81 | + self.mspconfig = cast(MSPConfigT, mspconfig.without_subdevices()) |
15 | 82 | except AttributeError: |
16 | 83 | self.mspconfig = mspconfig |
17 | 84 |
|
18 | | - if hasattr(self, "telemetry") and telemetry is not None: |
19 | | - self.telemetry = telemetry.get_telem_by_systemid(self.mspconfig.system_id) |
20 | | - |
21 | | - # Populate fields from MSP configuration and telemetry |
22 | | - # This is some moderate magic to avoid having to manually set each field |
23 | | - # The TL;DR is that we loop over all fields defined in the MSPConfig and Telemetry models |
24 | | - # and set the corresponding attributes on this equipment instance. |
25 | | - for field in self.mspconfig.__class__.model_fields: |
26 | | - if getattr(self.mspconfig, field, None) is not None: |
27 | | - setattr(self, field, self._from_mspconfig(field)) |
28 | | - for field in self.mspconfig.__class__.model_computed_fields: |
29 | | - if getattr(self.mspconfig, field, None) is not None: |
30 | | - setattr(self, field, self._from_mspconfig(field)) |
31 | | - if hasattr(self, "telemetry") and self.telemetry is not None: |
32 | | - for field in self.telemetry.__class__.model_fields: |
33 | | - if getattr(self.telemetry, field, None) is not None: |
34 | | - setattr(self, field, self._from_telemetry(field)) |
35 | | - for field in self.telemetry.__class__.model_computed_fields: |
36 | | - if getattr(self.telemetry, field, None) is not None: |
37 | | - setattr(self, field, self._from_telemetry(field)) |
38 | | - |
39 | | - def update_config(self, mspconfig: MSPEquipmentType) -> None: |
40 | | - """Update the configuration data for the equipment.""" |
41 | | - if hasattr(self, "mspconfig"): |
42 | | - self.mspconfig = mspconfig.without_subdevices() |
43 | | - else: |
44 | | - raise NotImplementedError("This equipment does not have MSP configuration.") |
45 | | - |
46 | 85 | def update_telemetry(self, telemetry: Telemetry) -> None: |
47 | 86 | """Update the telemetry data for the equipment.""" |
48 | | - if hasattr(self, "telemetry"): |
49 | | - self.telemetry = telemetry.get_telem_by_systemid(self.mspconfig.system_id) |
| 87 | + # Only update telemetry if this equipment type has telemetry |
| 88 | + # if hasattr(self, "telemetry"): |
| 89 | + # Extract the specific telemetry for this equipment from the full telemetry object |
| 90 | + # Note: Some equipment (like sensors) don't have their own telemetry, so this may be None |
| 91 | + if specific_telemetry := telemetry.get_telem_by_systemid(self.mspconfig.system_id) is not None: |
| 92 | + self.telemetry = cast(TelemetryT, specific_telemetry) |
50 | 93 | else: |
51 | | - raise NotImplementedError("This equipment does not have telemetry data.") |
| 94 | + self.telemetry = cast(TelemetryT, None) |
| 95 | + # else: |
| 96 | + # raise NotImplementedError("This equipment does not have telemetry data.") |
52 | 97 |
|
53 | | - def _from_mspconfig(self, attribute: str) -> Any: |
54 | | - """Helper method to get a value from the MSP configuration.""" |
55 | | - return getattr(self.mspconfig, attribute, None) |
| 98 | + # def _update_equipment(self, telemetry: Telemetry) -> None: |
| 99 | + # pass |
56 | 100 |
|
57 | | - def _from_telemetry(self, attribute: str) -> Any: |
58 | | - """Helper method to get a value from the telemetry data.""" |
59 | | - if hasattr(self, "telemetry"): |
60 | | - return getattr(self.telemetry, attribute, None) |
61 | | - return None |
| 101 | + # def _update_equipment(self, telemetry: Telemetry) -> None: |
| 102 | + # """Update any child equipment based on the latest MSPConfig and Telemetry data.""" |
| 103 | + # for _, equipment_mspconfig in self.mspconfig: |
| 104 | + # system_id = equipment_mspconfig.system_id |
| 105 | + # if system_id is None: |
| 106 | + # _LOGGER.debug("Skipping equipment update: system_id is None: %s", equipment_mspconfig) |
| 107 | + # continue |
| 108 | + # if system_id in self.child_equipment: |
| 109 | + # # Update existing child equipment |
| 110 | + # child_equipment = self.child_equipment[system_id] |
| 111 | + # if child_equipment is not None: |
| 112 | + # child_equipment.update_config(equipment_mspconfig) |
| 113 | + # if hasattr(self, "telemetry"): |
| 114 | + # child_equipment.update_telemetry(telemetry) |
| 115 | + # else: |
| 116 | + # equipment = create_equipment(self, equipment_mspconfig, telemetry) |
0 commit comments