66import logging
77from typing import Any , Final
88
9- from ..api import NodeEvent , NodeFeature
9+ from ..api import NodeEvent , NodeFeature , SenseStatistics
1010from ..connection import StickController
1111from ..exceptions import MessageError , NodeError
1212from ..messages .responses import SENSE_REPORT_ID , PlugwiseResponse , SenseReportResponse
2525
2626SENSE_FEATURES : Final = (
2727 NodeFeature .INFO ,
28- NodeFeature .TEMPERATURE ,
29- NodeFeature .HUMIDITY ,
28+ NodeFeature .SENSE ,
3029)
3130
3231
@@ -43,8 +42,7 @@ def __init__(
4342 """Initialize Scan Device."""
4443 super ().__init__ (mac , address , controller , loaded_callback )
4544
46- self ._humidity : float | None = None
47- self ._temperature : float | None = None
45+ self ._sense_statistics = SenseStatistics ()
4846
4947 self ._sense_subscription : Callable [[], None ] | None = None
5048
@@ -56,16 +54,17 @@ async def load(self) -> bool:
5654 self ._node_info .is_battery_powered = True
5755 if self ._cache_enabled :
5856 _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-
57+ await self ._load_from_cache ()
58+ else :
59+ self ._load_defaults ()
60+ self ._loaded = True
61+ self ._setup_protocol (
62+ SENSE_FIRMWARE_SUPPORT ,
63+ (NodeFeature .INFO , NodeFeature .SENSE ),
64+ )
65+ if await self .initialize ():
66+ await self ._loaded_callback (NodeEvent .LOADED , self .mac )
67+ return True
6968 _LOGGER .debug ("Loading of Sense node %s failed" , self ._node_info .mac )
7069 return False
7170
@@ -90,6 +89,24 @@ async def unload(self) -> None:
9089 self ._sense_subscription ()
9190 await super ().unload ()
9291
92+ def _load_defaults (self ) -> None :
93+ """Load default configuration settings."""
94+ super ()._load_defaults ()
95+ self ._sense_statistics = SenseStatistics (
96+ temperature = 0.0 ,
97+ humidity = 0.0 ,
98+ )
99+
100+ # region properties
101+
102+ @property
103+ @raise_not_loaded
104+ def sense_statistics (self ) -> SenseStatistics :
105+ """Sense Statistics."""
106+ return self ._sense_statistics
107+
108+ # end region
109+
93110 async def _sense_report (self , response : PlugwiseResponse ) -> bool :
94111 """Process sense report message to extract current temperature and humidity values."""
95112 if not isinstance (response , SenseReportResponse ):
@@ -99,25 +116,24 @@ async def _sense_report(self, response: PlugwiseResponse) -> bool:
99116 report_received = False
100117 await self ._available_update_state (True , response .timestamp )
101118 if response .temperature .value != 65535 :
102- self ._temperature = int (
119+ self ._sense_statistics . temperature = float (
103120 SENSE_TEMPERATURE_MULTIPLIER * (response .temperature .value / 65536 )
104121 - SENSE_TEMPERATURE_OFFSET
105122 )
106- await self .publish_feature_update_to_subscribers (
107- NodeFeature .TEMPERATURE , self ._temperature
108- )
109123 report_received = True
110124
111125 if response .humidity .value != 65535 :
112- self ._humidity = int (
126+ self ._sense_statistics . humidity = float (
113127 SENSE_HUMIDITY_MULTIPLIER * (response .humidity .value / 65536 )
114128 - SENSE_HUMIDITY_OFFSET
115129 )
130+ report_received = True
131+
132+ if report_received :
116133 await self .publish_feature_update_to_subscribers (
117- NodeFeature .HUMIDITY , self ._humidity
134+ NodeFeature .SENSE , self ._sense_statistics
118135 )
119- report_received = True
120-
136+
121137 return report_received
122138
123139 @raise_not_loaded
@@ -136,12 +152,10 @@ async def get_state(self, features: tuple[NodeFeature]) -> dict[NodeFeature, Any
136152 )
137153
138154 match feature :
139- case NodeFeature .TEMPERATURE :
140- states [NodeFeature .TEMPERATURE ] = self ._temperature
141- case NodeFeature .HUMIDITY :
142- states [NodeFeature .HUMIDITY ] = self ._humidity
143155 case NodeFeature .PING :
144156 states [NodeFeature .PING ] = await self .ping_update ()
157+ case NodeFeature .SENSE :
158+ states [NodeFeature .SENSE ] = self ._sense_statistics
145159 case _:
146160 state_result = await super ().get_state ((feature ,))
147161 states [feature ] = state_result [feature ]
0 commit comments