Skip to content

Commit 585a203

Browse files
committed
consolidate sense data into single propertie
1 parent aebfd88 commit 585a203

File tree

3 files changed

+23
-44
lines changed

3 files changed

+23
-44
lines changed

plugwise_usb/api.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,10 @@ class EnergyStatistics:
230230
day_production_reset: datetime | None = None
231231

232232
@dataclass
233-
class Temperature:
234-
"""Temperature statistics collection."""
233+
class SenseStatistics:
234+
"""Sense statistics collection."""
235235

236236
temperature: float | None = None
237-
238-
@dataclass
239-
class Humidity:
240-
"""Humidity statistics collection."""
241-
242237
humidity: float | None = None
243238

244239
class PlugwiseNode(Protocol):

plugwise_usb/nodes/node.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
RelayConfig,
2626
RelayLock,
2727
RelayState,
28-
Temperature,
29-
Humidity,
28+
SenseStatistics,
3029
)
3130
from ..connection import StickController
3231
from ..constants import SUPPRESS_INITIALIZATION_WARNINGS, TYPE_MODEL, UTF8
@@ -194,14 +193,6 @@ def features(self) -> tuple[NodeFeature, ...]:
194193
"""Supported feature types of node."""
195194
return self._features
196195

197-
@property
198-
@raise_not_loaded
199-
def humidity(self) -> Humidity:
200-
"""Humidity state."""
201-
if NodeFeature.HUMIDITY not in self._features:
202-
raise FeatureError(f"Humidity state is not supported for node {self.mac}")
203-
raise NotImplementedError()
204-
205196
@property
206197
def is_battery_powered(self) -> bool:
207198
"""Return if node is battery powered."""
@@ -322,12 +313,14 @@ def switch(self) -> bool:
322313

323314
@property
324315
@raise_not_loaded
325-
def temperature(self) -> Temperature:
326-
"""Temperature value."""
316+
def sense(self) -> SenseStatistics:
317+
"""Sense statistics."""
327318
if NodeFeature.TEMPERATURE not in self._features:
328319
raise FeatureError(
329320
f"Temperature state is not supported for node {self.mac}"
330321
)
322+
if NodeFeature.HUMIDITY not in self._features:
323+
raise FeatureError(f"Humidity state is not supported for node {self.mac}")
331324
raise NotImplementedError()
332325

333326
# endregion

plugwise_usb/nodes/sense.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
from typing import Any, Final
88

9-
from ..api import NodeEvent, NodeFeature, Temperature, Humidity
9+
from ..api import NodeEvent, NodeFeature, SenseStatistics
1010
from ..connection import StickController
1111
from ..exceptions import MessageError, NodeError
1212
from ..messages.responses import SENSE_REPORT_ID, PlugwiseResponse, SenseReportResponse
@@ -43,8 +43,7 @@ def __init__(
4343
"""Initialize Scan Device."""
4444
super().__init__(mac, address, controller, loaded_callback)
4545

46-
self._humidity = Humidity()
47-
self._temperature = Temperature()
46+
self._sense_statistics = SenseStatistics()
4847

4948
self._sense_subscription: Callable[[], None] | None = None
5049

@@ -94,26 +93,18 @@ async def unload(self) -> None:
9493
def _load_defaults(self) -> None:
9594
"""Load default configuration settings."""
9695
super()._load_defaults()
97-
self._temperature = Temperature(
98-
temperature = 0.0,
96+
self._sense_statistics = SenseStatistics(
97+
temperature=0.0,
98+
humidity=0.0,
9999
)
100-
self._humidity = Humidity(
101-
humidity = 0.0,
102-
)
103-
# region properties
104-
105-
@property
106-
@raise_not_loaded
107-
def temperature(self) -> Temperature:
108-
"""Temperature."""
109-
return self._temperature
110-
111100

101+
# region properties
102+
112103
@property
113104
@raise_not_loaded
114-
def humidity(self) -> Humidity:
115-
"""Humidity."""
116-
return self._humidity
105+
def sense_statistics(self) -> SenseStatistics:
106+
"""Sense Statistics."""
107+
return self._sense_statistics
117108

118109
# end region
119110

@@ -126,22 +117,22 @@ async def _sense_report(self, response: PlugwiseResponse) -> bool:
126117
report_received = False
127118
await self._available_update_state(True, response.timestamp)
128119
if response.temperature.value != 65535:
129-
self._temperature.temperature = float(
120+
self._sense_statistics.temperature = float(
130121
SENSE_TEMPERATURE_MULTIPLIER * (response.temperature.value / 65536)
131122
- SENSE_TEMPERATURE_OFFSET
132123
)
133124
await self.publish_feature_update_to_subscribers(
134-
NodeFeature.TEMPERATURE, self._temperature
125+
NodeFeature.TEMPERATURE, self._sense_statistics
135126
)
136127
report_received = True
137128

138129
if response.humidity.value != 65535:
139-
self._humidity.humidity = float(
130+
self._sense_statistics.humidity = float(
140131
SENSE_HUMIDITY_MULTIPLIER * (response.humidity.value / 65536)
141132
- SENSE_HUMIDITY_OFFSET
142133
)
143134
await self.publish_feature_update_to_subscribers(
144-
NodeFeature.HUMIDITY, self._humidity
135+
NodeFeature.HUMIDITY, self._sense_statistics
145136
)
146137
report_received = True
147138

@@ -164,9 +155,9 @@ async def get_state(self, features: tuple[NodeFeature]) -> dict[NodeFeature, Any
164155

165156
match feature:
166157
case NodeFeature.TEMPERATURE:
167-
states[NodeFeature.TEMPERATURE] = self._temperature
158+
states[NodeFeature.TEMPERATURE] = self._sense_statistics
168159
case NodeFeature.HUMIDITY:
169-
states[NodeFeature.HUMIDITY] = self._humidity
160+
states[NodeFeature.HUMIDITY] = self._sense_statistics
170161
case NodeFeature.PING:
171162
states[NodeFeature.PING] = await self.ping_update()
172163
case _:

0 commit comments

Comments
 (0)