Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 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 @@ -263,6 +265,26 @@ def motion_state(self) -> MotionState:
raise FeatureError(f"Motion state is not supported for node {self.mac}")
raise NotImplementedError()

@property
@raise_not_loaded
def temperature(self) -> Temperature:
"""Temperature configuration settings."""
if NodeFeature.TEMPERATURE not in self._features:
raise FeatureError(
f"Temperature configuration is not supported for node {self.mac}"
)
raise NotImplementedError()

@property
@raise_not_loaded
def humidity(self) -> Humidity:
"""Humidity configuration settings."""
if NodeFeature.HUMIDITY not in self._features:
raise FeatureError(
f"Humidity configuration is not supported for node {self.mac}"
)
raise NotImplementedError()

@property
def ping_stats(self) -> NetworkStatistics:
"""Ping statistics."""
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 @@ def __init__(
"""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()

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

Expand All @@ -56,16 +56,17 @@ async def load(self) -> bool:
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()
else:
self._load_defaults()
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
_LOGGER.debug("Loading of Sense node %s failed", self._node_info.mac)
return False

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

def _load_defaults(self) -> None:
"""Load default configuration settings."""
super()._load_defaults()
self._temperature = Temperature(
temperature = 0.0,
)
self._humidity = Humidity(
humidity = 0.0,
)
# region properties

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


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

# 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 @@ async def _sense_report(self, response: PlugwiseResponse) -> bool:
report_received = False
await self._available_update_state(True, response.timestamp)
if response.temperature.value != 65535:
self._temperature = int(
self._temperature.temperature = float(
SENSE_TEMPERATURE_MULTIPLIER * (response.temperature.value / 65536)
- SENSE_TEMPERATURE_OFFSET
)
Expand All @@ -109,7 +136,7 @@ async def _sense_report(self, response: PlugwiseResponse) -> bool:
report_received = True

if response.humidity.value != 65535:
self._humidity = int(
self._humidity.humidity = float(
SENSE_HUMIDITY_MULTIPLIER * (response.humidity.value / 65536)
- SENSE_HUMIDITY_OFFSET
)
Expand Down
Loading