-
Notifications
You must be signed in to change notification settings - Fork 54
Add leak sensor and temperature/humidity sensor support #342
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
base: main
Are you sure you want to change the base?
Changes from all commits
54d9ac7
f44cc16
90e624e
75850ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| # katie@mulliken.net to receive a copy | ||
| import asyncio | ||
| import logging | ||
| import time | ||
| from threading import Thread | ||
| from typing import List, Callable, Tuple, Optional | ||
|
|
||
|
|
@@ -24,11 +25,35 @@ class Sensor(Device): | |
| class SensorService(BaseService): | ||
| _updater_thread: Optional[Thread] = None | ||
| _subscribers: List[Tuple[Sensor, Callable[[Sensor], None]]] = [] | ||
| _worker_loop_interval = 5 # seconds between full passes; actual API | ||
| # calls are separately bounded by BaseService._min_update_time | ||
|
|
||
| async def update(self, sensor: Sensor) -> Sensor: | ||
| # Get updated device_params | ||
| async with BaseService._update_lock: | ||
| sensor.device_params = await self.get_updated_params(sensor.mac) | ||
|
|
||
| if sensor.type is DeviceTypes.LEAK_SENSOR: | ||
| sensor.detected = sensor.device_params.get("ws_detect_state") == 1 | ||
| return sensor | ||
|
|
||
| if sensor.type is DeviceTypes.TEMPERATURE_HUMIDITY: | ||
| return sensor | ||
|
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a leak or temperature/humidity sensor is registered through Useful? React with 👍 / 👎. |
||
|
|
||
| if sensor.type is DeviceTypes.CONTACT_SENSOR: | ||
| sensor.detected = sensor.device_params.get("open_close_state") == 1 | ||
| return sensor | ||
|
|
||
| if sensor.type is DeviceTypes.MOTION_SENSOR: | ||
| sensor.detected = sensor.device_params.get("motion_state") == 1 | ||
| return sensor | ||
|
|
||
| # Fallback for any sensor type not explicitly handled above. | ||
| # Currently unused by MOTION_SENSOR, CONTACT_SENSOR, LEAK_SENSOR, and | ||
| # TEMPERATURE_HUMIDITY, which all read state from the already-cached | ||
| # device_params via get_updated_params() instead of making a separate | ||
| # per-sensor API call. Kept for forward compatibility with future | ||
| # sensor types that may still need PropertyIDs-based property lookup. | ||
| properties = await self._get_device_info(sensor) | ||
|
|
||
| for property in properties["data"]["property_list"]: | ||
|
|
@@ -88,6 +113,8 @@ def update_worker(self, loop): | |
| except ContentTypeError as e: | ||
| _LOGGER.error(f"Server returned unexpected ContentType: {e}") | ||
|
|
||
| time.sleep(self._worker_loop_interval) | ||
|
|
||
| async def get_sensors(self) -> List[Sensor]: | ||
| if self._devices is None: | ||
| self._devices = await self.get_object_list() | ||
|
|
@@ -97,5 +124,7 @@ async def get_sensors(self) -> List[Sensor]: | |
| for device in self._devices | ||
| if device.type is DeviceTypes.MOTION_SENSOR | ||
| or device.type is DeviceTypes.CONTACT_SENSOR | ||
| or device.type is DeviceTypes.LEAK_SENSOR | ||
| or device.type is DeviceTypes.TEMPERATURE_HUMIDITY | ||
| ] | ||
| return [Sensor(sensor.raw_dict) for sensor in sensors] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a leak starts or stops within 1,200 seconds of the previous object-list refresh,
get_updated_params()returns the cachedBaseService._devicesparameters, and this new early return skips the live property request. Consequently, normal polls and callbacks can report the previous wet/dry state for up to 20 minutes; temperature and humidity values have the same staleness problem. These parameter-only sensors need a suitably fresh API source or explicit cache invalidation.Useful? React with 👍 / 👎.