Skip to content

Commit

Permalink
feature: expose sensor_id in sensors and alias in all features (#176)
Browse files Browse the repository at this point in the history
* feature: expose sensor_id in sensors and alias in all features

* feature: expose sensor_id in sensors and alias in all features (fixup)
  • Loading branch information
swistakm authored Aug 20, 2024
1 parent 3f9bb4c commit b2b185d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
4 changes: 4 additions & 0 deletions blebox_uniapi/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def device_class(self) -> str:
def product(self) -> "Box":
return self._product

@property
def alias(self):
return self._alias

# TODO: (cleanup) move to product/box ?
def raw_value(self, name: str) -> Any:
product = self._product
Expand Down
45 changes: 38 additions & 7 deletions blebox_uniapi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ def many_from_config(cls, product, box_type_config, extended_state):
}

feature = constructor(
product=product, alias=alias, methods=materialized_methods
product=product,
alias=alias,
methods=materialized_methods,
sensor_id=sensor_id,
)
object_list.append(feature)

Expand All @@ -98,11 +101,18 @@ class BaseSensor(Feature):
_device_class: str
_native_value: Union[float, int, str]
_sensor_type: Optional[str]
_sensor_id: Optional[int]

def __init__(
self, product: "Box", alias: str, methods: dict, sensor_type: str = None
self,
product: "Box",
alias: str,
methods: dict,
sensor_type: str = None,
sensor_id: Optional[int] = None,
):
self._sensor_type = sensor_type
self._sensor_id = sensor_id
super().__init__(product, alias, methods)

@property
Expand All @@ -117,6 +127,14 @@ def device_class(self) -> str:
def native_value(self):
return self._native_value

@property
def sensor_id(self):
return self._sensor_id

@property
def probe_id(self):
return self.sensor_id

@classmethod
def many_from_config(cls, product, box_type_config, extended_state):
raise NotImplementedError("Please use SensorFactory")
Expand All @@ -143,14 +161,15 @@ def __init__(
product: "Box",
alias: str,
methods: dict,
sensor_id: Optional[int],
*,
# generalization params
sensor_type: str,
unit: str,
scale: float = 1,
precision: Optional[int] = None,
):
super().__init__(product, alias, methods)
super().__init__(product, alias, methods, sensor_id=sensor_id)
self._unit = unit
self._scale = scale
self._precision = precision
Expand Down Expand Up @@ -200,8 +219,14 @@ def _read_period_of_measurement(self) -> int:
class Temperature(BaseSensor):
_current: Union[float, int, None]

def __init__(self, product: "Box", alias: str, methods: dict):
super().__init__(product, alias, methods)
def __init__(
self,
product: "Box",
alias: str,
methods: dict,
sensor_id: Optional[int] = None,
):
super().__init__(product, alias, methods, sensor_id=sensor_id)
self._unit = "celsius"
self._device_class = "temperature"

Expand All @@ -227,8 +252,14 @@ def after_update(self) -> None:
class AirQuality(BaseSensor):
_pm: Optional[int]

def __init__(self, product: "Box", alias: str, methods: dict):
super().__init__(product, alias, methods)
def __init__(
self,
product: "Box",
alias: str,
methods: dict,
sensor_id: Optional[str] = None,
):
super().__init__(product, alias, methods, sensor_id)
self._unit = "concentration_of_mp"
self._device_class = alias

Expand Down

0 comments on commit b2b185d

Please sign in to comment.