Skip to content

Commit 45eadb4

Browse files
committed
Update sense parameter handling to resemble the switch/scan api
Update sense initialisation logic matching how the switch works.
1 parent 0dd7182 commit 45eadb4

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

plugwise_usb/api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ class EnergyStatistics:
229229
day_production: float | None = None
230230
day_production_reset: datetime | None = None
231231

232+
@dataclass
233+
class Temperature:
234+
"""Temperature statistics collection."""
235+
236+
temperature: float | None = None
237+
238+
@dataclass
239+
class Humidity:
240+
"""Humidity statistics collection."""
241+
242+
humidity: float | None = None
232243

233244
class PlugwiseNode(Protocol):
234245
"""Protocol definition of a Plugwise device node."""

plugwise_usb/nodes/node.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
RelayConfig,
2626
RelayLock,
2727
RelayState,
28+
Temperature,
29+
Humidity,
2830
)
2931
from ..connection import StickController
3032
from ..constants import SUPPRESS_INITIALIZATION_WARNINGS, TYPE_MODEL, UTF8
@@ -263,6 +265,26 @@ def motion_state(self) -> MotionState:
263265
raise FeatureError(f"Motion state is not supported for node {self.mac}")
264266
raise NotImplementedError()
265267

268+
@property
269+
@raise_not_loaded
270+
def temperature(self) -> Temperature:
271+
"""Temperature configuration settings."""
272+
if NodeFeature.TEMPERATURE not in self._features:
273+
raise FeatureError(
274+
f"Temperature configuration is not supported for node {self.mac}"
275+
)
276+
raise NotImplementedError()
277+
278+
@property
279+
@raise_not_loaded
280+
def humidity(self) -> Humidity:
281+
"""Humidity configuration settings."""
282+
if NodeFeature.HUMIDITY not in self._features:
283+
raise FeatureError(
284+
f"Humidity configuration is not supported for node {self.mac}"
285+
)
286+
raise NotImplementedError()
287+
266288
@property
267289
def ping_stats(self) -> NetworkStatistics:
268290
"""Ping statistics."""

plugwise_usb/nodes/sense.py

Lines changed: 42 additions & 15 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
9+
from ..api import NodeEvent, NodeFeature, Temperature, Humidity
1010
from ..connection import StickController
1111
from ..exceptions import MessageError, NodeError
1212
from ..messages.responses import SENSE_REPORT_ID, PlugwiseResponse, SenseReportResponse
@@ -43,8 +43,8 @@ def __init__(
4343
"""Initialize Scan Device."""
4444
super().__init__(mac, address, controller, loaded_callback)
4545

46-
self._humidity: float | None = None
47-
self._temperature: float | None = None
46+
self._humidity: Humidity()
47+
self._temperature: Temperature()
4848

4949
self._sense_subscription: Callable[[], None] | None = None
5050

@@ -56,16 +56,17 @@ async def load(self) -> bool:
5656
self._node_info.is_battery_powered = True
5757
if self._cache_enabled:
5858
_LOGGER.debug("Loading Sense node %s from cache", self._node_info.mac)
59-
if await self._load_from_cache():
60-
self._loaded = True
61-
self._setup_protocol(
62-
SENSE_FIRMWARE_SUPPORT,
63-
(NodeFeature.INFO, NodeFeature.TEMPERATURE, NodeFeature.HUMIDITY),
64-
)
65-
if await self.initialize():
66-
await self._loaded_callback(NodeEvent.LOADED, self.mac)
67-
return True
68-
59+
await self._load_from_cache()
60+
else:
61+
self._load_defaults()
62+
self._loaded = True
63+
self._setup_protocol(
64+
SENSE_FIRMWARE_SUPPORT,
65+
(NodeFeature.INFO, NodeFeature.TEMPERATURE, NodeFeature.HUMIDITY),
66+
)
67+
if await self.initialize():
68+
await self._loaded_callback(NodeEvent.LOADED, self.mac)
69+
return True
6970
_LOGGER.debug("Loading of Sense node %s failed", self._node_info.mac)
7071
return False
7172

@@ -90,6 +91,32 @@ async def unload(self) -> None:
9091
self._sense_subscription()
9192
await super().unload()
9293

94+
def _load_defaults(self) -> None:
95+
"""Load default configuration settings."""
96+
super()._load_defaults()
97+
self._temperature = Temperature(
98+
temperature = 0.0,
99+
)
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+
111+
112+
@property
113+
@raise_not_loaded
114+
def humidity(self) -> Humidity:
115+
"""Humidity. """
116+
return self._humidity
117+
118+
# end region
119+
93120
async def _sense_report(self, response: PlugwiseResponse) -> bool:
94121
"""Process sense report message to extract current temperature and humidity values."""
95122
if not isinstance(response, SenseReportResponse):
@@ -99,7 +126,7 @@ async def _sense_report(self, response: PlugwiseResponse) -> bool:
99126
report_received = False
100127
await self._available_update_state(True, response.timestamp)
101128
if response.temperature.value != 65535:
102-
self._temperature = int(
129+
self._temperature.temperature = float(
103130
SENSE_TEMPERATURE_MULTIPLIER * (response.temperature.value / 65536)
104131
- SENSE_TEMPERATURE_OFFSET
105132
)
@@ -109,7 +136,7 @@ async def _sense_report(self, response: PlugwiseResponse) -> bool:
109136
report_received = True
110137

111138
if response.humidity.value != 65535:
112-
self._humidity = int(
139+
self._humidity.humidity = float(
113140
SENSE_HUMIDITY_MULTIPLIER * (response.humidity.value / 65536)
114141
- SENSE_HUMIDITY_OFFSET
115142
)

0 commit comments

Comments
 (0)