-
-
Notifications
You must be signed in to change notification settings - Fork 223
Add Time module to SmartCamera devices #1182
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
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
0df0f4a
Add initial test framework changes for SmartCamera
sdb9696 2fab6dc
Add basic child initialization
sdb9696 2b80075
Provide https to get_device_family
sdb9696 1c684d1
Add call to initialise children
sdb9696 71d7537
Log stack trace on child init
sdb9696 5f62f83
Fix device_type error
sdb9696 5ec48de
Do not double wrap child device protocol
sdb9696 d6740fe
Enable sending empty params
sdb9696 2eebb53
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 fb174cf
Add extra params to control child request
sdb9696 039ccea
Revert "Add extra params to control child request"
sdb9696 9591521
Add time and camera modules
sdb9696 52a8219
Improve REGISTERED_MODULES restriction
sdb9696 7f958ff
Handle child devices not supporting time
sdb9696 56a5e33
Fix tests
sdb9696 7a4a4c6
Add modules to init
sdb9696 e66c13d
Fix clock status
sdb9696 8dbe65c
Fix camera disabled
sdb9696 84fc93c
Fix cloud_connect missing error
sdb9696 72deb4c
Fix intentionally broken camera getter name
sdb9696 972e0d2
Enable experimental as default
sdb9696 8115554
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 58bfa57
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 99a4315
Update post review
sdb9696 67db948
Simplify module auto registration
sdb9696 4fd38bb
Apply suggestions from code review
sdb9696 d3dd5cb
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 4e7ef06
Update docstring for SmartChildDevice.create
sdb9696 2410c43
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 a19784b
Revert fixture change
sdb9696 e3513f0
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 777fe74
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 45b59b0
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 779e468
Remove seperate SmartErrorCode handling
sdb9696 dd5dc95
Raise errors on missing module data
sdb9696 1cbbab6
Merge remote-tracking branch 'upstream/master' into feat/camera_hub
sdb9696 ee32c40
Add test for set_time
sdb9696 edf9a96
Fix whitespace and disable experimental
sdb9696 16cdd8c
Include local time in time set
sdb9696 2445eb7
Remove unnecessary NAME
sdb9696 205e890
Fix tests
sdb9696 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"""Implementation of time module.""" | ||
|
||
from __future__ import annotations | ||
|
||
from datetime import datetime, timezone, tzinfo | ||
from typing import cast | ||
|
||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError | ||
|
||
from ...cachedzoneinfo import CachedZoneInfo | ||
from ...feature import Feature | ||
from ...interfaces import Time as TimeInterface | ||
from ..smartcameramodule import SmartCameraModule | ||
|
||
|
||
class Time(SmartCameraModule, TimeInterface): | ||
"""Implementation of device_local_time.""" | ||
|
||
QUERY_GETTER_NAME = "getTimezone" | ||
QUERY_MODULE_NAME = "system" | ||
QUERY_SECTION_NAMES = "basic" | ||
|
||
_timezone: tzinfo = timezone.utc | ||
_time: datetime | ||
|
||
def _initialize_features(self) -> None: | ||
"""Initialize features after the initial update.""" | ||
self._add_feature( | ||
Feature( | ||
device=self._device, | ||
id="device_time", | ||
name="Device time", | ||
attribute_getter="time", | ||
container=self, | ||
category=Feature.Category.Debug, | ||
type=Feature.Type.Sensor, | ||
) | ||
) | ||
|
||
def query(self) -> dict: | ||
"""Query to execute during the update cycle.""" | ||
q = super().query() | ||
q["getClockStatus"] = {self.QUERY_MODULE_NAME: {"name": "clock_status"}} | ||
|
||
return q | ||
|
||
async def _post_update_hook(self) -> None: | ||
"""Perform actions after a device update.""" | ||
time_data = self.data["getClockStatus"]["system"]["clock_status"] | ||
timezone_data = self.data["getTimezone"]["system"]["basic"] | ||
zone_id = timezone_data["zone_id"] | ||
timestamp = time_data["seconds_from_1970"] | ||
try: | ||
# Zoneinfo will return a DST aware object | ||
tz: tzinfo = await CachedZoneInfo.get_cached_zone_info(zone_id) | ||
except ZoneInfoNotFoundError: | ||
# timezone string like: UTC+10:00 | ||
timezone_str = timezone_data["timezone"] | ||
tz = cast(tzinfo, datetime.strptime(timezone_str[-6:], "%z").tzinfo) | ||
|
||
self._timezone = tz | ||
self._time = datetime.fromtimestamp( | ||
cast(float, timestamp), | ||
tz=tz, | ||
) | ||
|
||
@property | ||
def timezone(self) -> tzinfo: | ||
"""Return current timezone.""" | ||
return self._timezone | ||
|
||
@property | ||
def time(self) -> datetime: | ||
"""Return device's current datetime.""" | ||
return self._time | ||
|
||
async def set_time(self, dt: datetime) -> dict: | ||
"""Set device time.""" | ||
if not dt.tzinfo: | ||
timestamp = dt.replace(tzinfo=self.timezone).timestamp() | ||
else: | ||
timestamp = dt.timestamp() | ||
|
||
lt = datetime.fromtimestamp(timestamp).isoformat().replace("T", " ") | ||
params = {"seconds_from_1970": int(timestamp), "local_time": lt} | ||
# Doesn't seem to update the time, perhaps because timing_mode is ntp | ||
res = await self.call("setTimezone", {"system": {"clock_status": params}}) | ||
if (zinfo := dt.tzinfo) and isinstance(zinfo, ZoneInfo): | ||
tz_params = {"zone_id": zinfo.key} | ||
res = await self.call("setTimezone", {"system": {"basic": tz_params}}) | ||
return res |
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
Oops, something went wrong.
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.