Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Update sense parameter handling and initialisation#256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
45eadb4
Update sense parameter handling to resemble the switch/scan api
dirixmjm 7bdc697
fix double propertie entry
dirixmjm 7d2dec2
CodeRabbit advice, fix assigning propertie object
dirixmjm aebfd88
nitpick fixes
dirixmjm 585a203
consolidate sense data into single propertie
dirixmjm 2efaa55
try 1; fix test
dirixmjm 0e2381e
try 2; fix test
dirixmjm 9039c59
nitpick comment
dirixmjm 8b34bc8
Add NodeFeature SENSE
bouwew 38808f9
And implement
bouwew df3fe4a
Apply refactor suggestions
dirixmjm 61021e8
re-add self._loaded
dirixmjm 00288e1
Fix publish_feature
bouwew 6be4c4c
move self._loaded because initialize requires this to be set
dirixmjm 429cf36
report_received improvements
bouwew 9676a39
fix publication of NodeFeature.SENSE
dirixmjm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -52,6 +52,7 @@ class NodeFeature(str, Enum): | ||
| RELAY_INIT = "relay_init" | ||
| RELAY_LOCK = "relay_lock" | ||
| SWITCH = "switch" | ||
| SENSE = "sense" | ||
| TEMPERATURE = "temperature" | ||
| @@ -80,6 +81,7 @@ class NodeType(Enum): | ||
| NodeFeature.MOTION, | ||
| NodeFeature.MOTION_CONFIG, | ||
| NodeFeature.TEMPERATURE, | ||
| NodeFeature.SENSE, | ||
| NodeFeature.SWITCH, | ||
| ) | ||
| @@ -229,6 +231,12 @@ class EnergyStatistics: | ||
| day_production: float | None = None | ||
| day_production_reset: datetime | None = None | ||
| @dataclass | ||
| class SenseStatistics: | ||
| """Sense statistics collection.""" | ||
| temperature: float | None = None | ||
| humidity: float | None = None | ||
dirixmjm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| class PlugwiseNode(Protocol): | ||
| """Protocol definition of a Plugwise device node.""" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,7 +6,7 @@ | ||
| import logging | ||
| from typing import Any, Final | ||
| from ..api import NodeEvent, NodeFeature | ||
| from ..api import NodeEvent, NodeFeature, SenseStatistics | ||
| from ..connection import StickController | ||
| from ..exceptions import MessageError, NodeError | ||
| from ..messages.responses import SENSE_REPORT_ID, PlugwiseResponse, SenseReportResponse | ||
| @@ -25,8 +25,7 @@ | ||
| SENSE_FEATURES: Final = ( | ||
| NodeFeature.INFO, | ||
| NodeFeature.TEMPERATURE, | ||
| NodeFeature.HUMIDITY, | ||
| NodeFeature.SENSE, | ||
| ) | ||
| @@ -43,8 +42,7 @@ def __init__( | ||
| """Initialize Scan Device.""" | ||
| super().__init__(mac, address, controller, loaded_callback) | ||
| self._humidity: float | None = None | ||
| self._temperature: float | None = None | ||
| self._sense_statistics = SenseStatistics() | ||
| self._sense_subscription: Callable[[], None] | None = None | ||
| @@ -56,16 +54,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.SENSE), | ||
| ) | ||
| if await self.initialize(): | ||
| await self._loaded_callback(NodeEvent.LOADED, self.mac) | ||
| return True | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| _LOGGER.debug("Loading of Sense node %s failed", self._node_info.mac) | ||
| return False | ||
| @@ -90,6 +89,24 @@ async def unload(self) -> None: | ||
| self._sense_subscription() | ||
| await super().unload() | ||
| def _load_defaults(self) -> None: | ||
| """Load default configuration settings.""" | ||
| super()._load_defaults() | ||
| self._sense_statistics = SenseStatistics( | ||
| temperature=0.0, | ||
| humidity=0.0, | ||
| ) | ||
| # region properties | ||
| @property | ||
| @raise_not_loaded | ||
| def sense_statistics(self) -> SenseStatistics: | ||
| """Sense Statistics.""" | ||
| return self._sense_statistics | ||
| # 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): | ||
| @@ -99,25 +116,24 @@ 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._sense_statistics.temperature = float( | ||
| SENSE_TEMPERATURE_MULTIPLIER * (response.temperature.value / 65536) | ||
| - SENSE_TEMPERATURE_OFFSET | ||
| ) | ||
| await self.publish_feature_update_to_subscribers( | ||
| NodeFeature.TEMPERATURE, self._temperature | ||
| ) | ||
| report_received = True | ||
| if response.humidity.value != 65535: | ||
| self._humidity = int( | ||
| self._sense_statistics.humidity = float( | ||
| SENSE_HUMIDITY_MULTIPLIER * (response.humidity.value / 65536) | ||
| - SENSE_HUMIDITY_OFFSET | ||
| ) | ||
| report_received = True | ||
| if report_received: | ||
| await self.publish_feature_update_to_subscribers( | ||
| NodeFeature.HUMIDITY, self._humidity | ||
| NodeFeature.SENSE, self._sense_statistics | ||
| ) | ||
| report_received = True | ||
| return report_received | ||
| @raise_not_loaded | ||
| @@ -136,12 +152,10 @@ async def get_state(self, features: tuple[NodeFeature]) -> dict[NodeFeature, Any | ||
| ) | ||
| match feature: | ||
| case NodeFeature.TEMPERATURE: | ||
| states[NodeFeature.TEMPERATURE] = self._temperature | ||
| case NodeFeature.HUMIDITY: | ||
| states[NodeFeature.HUMIDITY] = self._humidity | ||
| case NodeFeature.PING: | ||
| states[NodeFeature.PING] = await self.ping_update() | ||
| case NodeFeature.SENSE: | ||
| states[NodeFeature.SENSE] = self._sense_statistics | ||
| case _: | ||
| state_result = await super().get_state((feature,)) | ||
| states[feature] = state_result[feature] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.