-
-
Notifications
You must be signed in to change notification settings - Fork 228
Add alarm module for smartcamera hubs #1258
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97a9f8a
Add alarm module for smartcamera hubs
sdb9696 6eeb181
Merge remote-tracking branch 'upstream/master' into feat/smartcam_siren
sdb9696 e85425d
Update post review
sdb9696 a4130eb
Update post review
sdb9696 da2803a
Add more test coverage
sdb9696 3075bb2
Merge branch 'master' into feat/smartcam_siren
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
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,166 @@ | ||
"""Implementation of alarm module.""" | ||
|
||
from __future__ import annotations | ||
|
||
from ...feature import Feature | ||
from ..smartcameramodule import SmartCameraModule | ||
|
||
DURATION_MIN = 0 | ||
DURATION_MAX = 6000 | ||
|
||
VOLUME_MIN = 0 | ||
VOLUME_MAX = 10 | ||
|
||
|
||
class Alarm(SmartCameraModule): | ||
"""Implementation of alarm module.""" | ||
|
||
# Needs a different name to avoid clashing with SmartAlarm | ||
NAME = "SmartCameraAlarm" | ||
|
||
REQUIRED_COMPONENT = "siren" | ||
QUERY_GETTER_NAME = "getSirenStatus" | ||
QUERY_MODULE_NAME = "siren" | ||
|
||
def query(self) -> dict: | ||
"""Query to execute during the update cycle.""" | ||
q = super().query() | ||
q["getSirenConfig"] = {self.QUERY_MODULE_NAME: {}} | ||
q["getSirenTypeList"] = {self.QUERY_MODULE_NAME: {}} | ||
|
||
return q | ||
|
||
def _initialize_features(self) -> None: | ||
"""Initialize features. | ||
|
||
This is implemented as some features depend on device responses. | ||
sdb9696 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
device = self._device | ||
self._add_feature( | ||
Feature( | ||
device, | ||
id="alarm", | ||
name="Alarm", | ||
container=self, | ||
attribute_getter="active", | ||
icon="mdi:bell", | ||
category=Feature.Category.Debug, | ||
type=Feature.Type.BinarySensor, | ||
sdb9696 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
) | ||
self._add_feature( | ||
Feature( | ||
device, | ||
id="alarm_sound", | ||
name="Alarm sound", | ||
container=self, | ||
attribute_getter="alarm_sound", | ||
attribute_setter="set_alarm_sound", | ||
category=Feature.Category.Config, | ||
type=Feature.Type.Choice, | ||
choices_getter="alarm_sounds", | ||
) | ||
) | ||
self._add_feature( | ||
Feature( | ||
device, | ||
id="alarm_volume", | ||
name="Alarm volume", | ||
container=self, | ||
attribute_getter="alarm_volume", | ||
attribute_setter="set_alarm_volume", | ||
category=Feature.Category.Config, | ||
type=Feature.Type.Number, | ||
range_getter=lambda: (VOLUME_MIN, VOLUME_MAX), | ||
) | ||
) | ||
self._add_feature( | ||
Feature( | ||
device, | ||
id="alarm_duration", | ||
name="Alarm duration", | ||
container=self, | ||
attribute_getter="alarm_duration", | ||
attribute_setter="set_alarm_duration", | ||
category=Feature.Category.Config, | ||
type=Feature.Type.Number, | ||
range_getter=lambda: (DURATION_MIN, DURATION_MAX), | ||
) | ||
) | ||
self._add_feature( | ||
Feature( | ||
device, | ||
id="test_alarm", | ||
name="Test alarm", | ||
container=self, | ||
attribute_setter="play", | ||
type=Feature.Type.Action, | ||
) | ||
) | ||
self._add_feature( | ||
Feature( | ||
device, | ||
id="stop_alarm", | ||
name="Stop alarm", | ||
container=self, | ||
attribute_setter="stop", | ||
type=Feature.Type.Action, | ||
) | ||
) | ||
|
||
@property | ||
def alarm_sound(self) -> str: | ||
"""Return current alarm sound.""" | ||
return self.data["getSirenConfig"]["siren_type"] | ||
|
||
async def set_alarm_sound(self, sound: str) -> dict: | ||
"""Set alarm sound. | ||
|
||
See *alarm_sounds* for list of available sounds. | ||
""" | ||
if sound not in self.alarm_sounds: | ||
msg = f"sound must be one of {', '.join(self.alarm_sounds)}: {sound}" | ||
raise ValueError(msg) | ||
return await self.call("setSirenConfig", {"siren": {"siren_type": sound}}) | ||
|
||
@property | ||
def alarm_sounds(self) -> list[str]: | ||
"""Return list of available alarm sounds.""" | ||
return self.data["getSirenTypeList"]["siren_type_list"] | ||
|
||
@property | ||
def alarm_volume(self) -> int: | ||
"""Return alarm volume.""" | ||
return int(self.data["getSirenConfig"]["volume"]) # type: ignore[return-value] | ||
sdb9696 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async def set_alarm_volume(self, volume: int) -> dict: | ||
"""Set alarm volume.""" | ||
if volume < VOLUME_MIN or volume > VOLUME_MAX: | ||
msg = f"volume must be between {VOLUME_MIN} and {VOLUME_MAX}" | ||
raise ValueError(msg) | ||
sdb9696 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return await self.call("setSirenConfig", {"siren": {"volume": str(volume)}}) | ||
|
||
@property | ||
def alarm_duration(self) -> int: | ||
"""Return alarm duration.""" | ||
return self.data["getSirenConfig"]["duration"] | ||
s
8000
db9696 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async def set_alarm_duration(self, duration: int) -> dict: | ||
"""Set alarm volume.""" | ||
if duration < DURATION_MIN or duration > DURATION_MAX: | ||
msg = f"duration must be between {DURATION_MIN} and {DURATION_MAX}" | ||
raise ValueError(msg) | ||
return await self.call("setSirenConfig", {"siren": {"duration": duration}}) | ||
|
||
@property | ||
def active(self) -> bool: | ||
"""Return true if alarm is active.""" | ||
return self.data["getSirenStatus"]["status"] != "off" | ||
|
||
async def play(self) -> dict: | ||
"""Play alarm.""" | ||
return await self.call("setSirenStatus", {"siren": {"status": "on"}}) | ||
|
||
async def stop(self) -> dict: | ||
"""Stop alarm.""" | ||
return await self.call("setSirenStatus", {"siren": {"status": "off"}}) |
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
Empty file.
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,40 @@ | ||
"""Tests for smart camera devices.""" | ||
|
||
from __future__ import annotations | ||
|
||
from kasa import Device | ||
from kasa.smartcamera.smartcameramodule import SmartCameraModule | ||
|
||
from ...conftest import hub_smartcamera | ||
|
||
|
||
@hub_smartcamera | ||
async def test_alarm(dev: Device): | ||
"""Test device alarm.""" | ||
alarm = dev.modules.get(SmartCameraModule.SmartCameraAlarm) | ||
|
||
assert alarm | ||
original_duration = alarm.alarm_duration | ||
assert original_duration is not None | ||
original_volume = alarm.alarm_volume | ||
assert original_volume is not None | ||
|
||
try: | ||
# test volume | ||
new_volume = original_volume - 1 if original_volume > 1 else original_volume + 1 | ||
await alarm.set_alarm_volume(new_volume) # type: ignore[arg-type] | ||
await dev.update() | ||
assert alarm.alarm_volume == new_volume | ||
|
||
# test duration | ||
new_duration = ( | ||
original_duration - 1 if original_duration > 1 else original_duration + 1 | ||
) | ||
await alarm.set_alarm_duration(new_duration) | ||
await dev.update() | ||
assert alarm.alarm_duration == new_duration | ||
|
||
finally: | ||
await alarm.set_alarm_volume(original_volume) | ||
await alarm.set_alarm_duration(original_duration) | ||
await dev.update() |
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.