Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plugwise_usb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ class EnergyStatistics:
day_production: float | None = None
day_production_reset: datetime | None = None

@dataclass
class Temperature:
"""Temperature statistics collection."""

temperature: float | None = None

@dataclass
class Humidity:
"""Humidity statistics collection."""

humidity: float | None = None

class PlugwiseNode(Protocol):
"""Protocol definition of a Plugwise device node."""
Expand Down
6 changes: 4 additions & 2 deletions plugwise_usb/nodes/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
RelayConfig,
RelayLock,
RelayState,
Temperature,
Humidity,
)
from ..connection import StickController
from ..constants import SUPPRESS_INITIALIZATION_WARNINGS, TYPE_MODEL, UTF8
Expand Down Expand Up @@ -194,7 +196,7 @@ def features(self) -> tuple[NodeFeature, ...]:

@property
@raise_not_loaded
def humidity(self) -> float:
def humidity(self) -> Humidity:
"""Humidity state."""
if NodeFeature.HUMIDITY not in self._features:
raise FeatureError(f"Humidity state is not supported for node {self.mac}")
Expand Down Expand Up @@ -320,7 +322,7 @@ def switch(self) -> bool:

@property
@raise_not_loaded
def temperature(self) -> float:
def temperature(self) -> Temperature:
"""Temperature value."""
if NodeFeature.TEMPERATURE not in self._features:
raise FeatureError(
Expand Down
57 changes: 42 additions & 15 deletions plugwise_usb/nodes/sense.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from typing import Any, Final

from ..api import NodeEvent, NodeFeature
from ..api import NodeEvent, NodeFeature, Temperature, Humidity
from ..connection import StickController
from ..exceptions import MessageError, NodeError
from ..messages.responses import SENSE_REPORT_ID, PlugwiseResponse, SenseReportResponse
Expand Down Expand Up @@ -43,8 +43,8 @@
"""Initialize Scan Device."""
super().__init__(mac, address, controller, loaded_callback)

self._humidity: float | None = None
self._temperature: float | None = None
self._humidity = Humidity()
self._temperature = Temperature()

Check warning on line 47 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L46-L47

Added lines #L46 - L47 were not covered by tests

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

Expand All @@ -56,16 +56,17 @@
self._node_info.is_battery_powered = True
if self._cache_enabled:
_LOGGER.debug("Loading Sense node %s from cache", self._node_info.mac)
if await self._load_from_cache():
self._loaded = True
self._setup_protocol(
SENSE_FIRMWARE_SUPPORT,
(NodeFeature.INFO, NodeFeature.TEMPERATURE, NodeFeature.HUMIDITY),
)
if await self.initialize():
await self._loaded_callback(NodeEvent.LOADED, self.mac)
return True

await self._load_from_cache()

Check warning on line 59 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L59

Added line #L59 was not covered by tests
else:
self._load_defaults()
self._loaded = True
self._setup_protocol(

Check warning on line 63 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L61-L63

Added lines #L61 - L63 were not covered by tests
SENSE_FIRMWARE_SUPPORT,
(NodeFeature.INFO, NodeFeature.TEMPERATURE, NodeFeature.HUMIDITY),
)
if await self.initialize():
await self._loaded_callback(NodeEvent.LOADED, self.mac)
return True

Check warning on line 69 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L67-L69

Added lines #L67 - L69 were not covered by tests
_LOGGER.debug("Loading of Sense node %s failed", self._node_info.mac)
return False

Expand All @@ -90,6 +91,32 @@
self._sense_subscription()
await super().unload()

def _load_defaults(self) -> None:
"""Load default configuration settings."""
super()._load_defaults()
self._temperature = Temperature(

Check warning on line 97 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L96-L97

Added lines #L96 - L97 were not covered by tests
temperature = 0.0,
)
self._humidity = Humidity(

Check warning on line 100 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L100

Added line #L100 was not covered by tests
humidity = 0.0,
)
# region properties

@property
@raise_not_loaded
def temperature(self) -> Temperature:
"""Temperature. """
return self._temperature

Check warning on line 109 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L109

Added line #L109 was not covered by tests


@property
@raise_not_loaded
def humidity(self) -> Humidity:
"""Humidity. """
return self._humidity

Check warning on line 116 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L116

Added line #L116 was not covered by tests

# end region

async def _sense_report(self, response: PlugwiseResponse) -> bool:
"""Process sense report message to extract current temperature and humidity values."""
if not isinstance(response, SenseReportResponse):
Expand All @@ -99,7 +126,7 @@
report_received = False
await self._available_update_state(True, response.timestamp)
if response.temperature.value != 65535:
self._temperature = int(
self._temperature.temperature = float(

Check warning on line 129 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L129

Added line #L129 was not covered by tests
SENSE_TEMPERATURE_MULTIPLIER * (response.temperature.value / 65536)
- SENSE_TEMPERATURE_OFFSET
)
Expand All @@ -109,7 +136,7 @@
report_received = True

if response.humidity.value != 65535:
self._humidity = int(
self._humidity.humidity = float(

Check warning on line 139 in plugwise_usb/nodes/sense.py

View check run for this annotation

Codecov / codecov/patch

plugwise_usb/nodes/sense.py#L139

Added line #L139 was not covered by tests
SENSE_HUMIDITY_MULTIPLIER * (response.humidity.value / 65536)
- SENSE_HUMIDITY_OFFSET
)
Expand Down
Loading