-
-
Notifications
You must be signed in to change notification settings - Fork 221
Add battery module to smartcam devices #1452
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
4 commits
Select commit
Hold shift + click to select a range
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,113 @@ | ||
"""Implementation of baby cry detection module.""" | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from ...feature import Feature | ||
from ..smartcammodule import SmartCamModule | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Battery(SmartCamModule): | ||
"""Implementation of a battery module.""" | ||
|
||
REQUIRED_COMPONENT = "battery" | ||
|
||
def _initialize_features(self) -> None: | ||
"""Initialize features.""" | ||
self._add_feature( | ||
Feature( | ||
self._device, | ||
"battery_low", | ||
"Battery low", | ||
container=self, | ||
attribute_getter="battery_low", | ||
icon="mdi:alert", | ||
type=Feature.Type.BinarySensor, | ||
category=Feature.Category.Debug, | ||
) | ||
) | ||
|
||
self._add_feature( | ||
Feature( | ||
self._device, | ||
"battery_level", | ||
"Battery level", | ||
container=self, | ||
attribute_getter="battery_percent", | ||
icon="mdi:battery", | ||
unit_getter=lambda: "%", | ||
category=Feature.Category.Info, | ||
type=Feature.Type.Sensor, | ||
) | ||
) | ||
|
||
self._add_feature( | ||
Feature( | ||
self._device, | ||
"battery_temperature", | ||
"Battery temperature", | ||
container=self, | ||
attribute_getter="battery_temperature", | ||
icon="mdi:battery", | ||
unit_getter=lambda: "celsius", | ||
category=Feature.Category.Debug, | ||
type=Feature.Type.Sensor, | ||
) | ||
) | ||
self._add_feature( | ||
Feature( | ||
self._device, | ||
"battery_voltage", | ||
"Battery voltage", | ||
container=self, | ||
attribute_getter="battery_voltage", | ||
icon="mdi:battery", | ||
unit_getter=lambda: "V", | ||
category=Feature.Category.Debug, | ||
type=Feature.Type.Sensor, | ||
) | ||
) | ||
self._add_feature( | ||
Feature( | ||
self._device, | ||
"battery_charging", | ||
"Battery charging", | ||
container=self, | ||
attribute_getter="battery_charging", | ||
icon="mdi:alert", | ||
type=Feature.Type.BinarySensor, | ||
category=Feature.Category.Debug, | ||
) | ||
) | ||
|
||
def query(self) -> dict: | ||
"""Query to execute during the update cycle.""" | ||
return {} | ||
|
||
@property | ||
def battery_percent(self) -> int: | ||
"""Return battery level.""" | ||
return self._device.sys_info["battery_percent"] | ||
|
||
@property | ||
def battery_low(self) -> bool: | ||
"""Return True if battery is low.""" | ||
return self._device.sys_info["low_battery"] | ||
|
||
@property | ||
def battery_temperature(self) -> bool: | ||
"""Return battery voltage in C.""" | ||
return self._device.sys_info["battery_temperature"] | ||
|
||
@property | ||
def battery_voltage(self) -> bool: | ||
"""Return battery voltage in V.""" | ||
return self._device.sys_info["battery_voltage"] / 1_000 | ||
|
||
@property | ||
def battery_charging(self) -> bool: | ||
"""Return True if battery is charging.""" | ||
return self._device.sys_info["battery_voltage"] != "NO" |
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
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
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,33 @@ | ||
"""Tests for smartcam battery module.""" | ||
|
||
from __future__ import annotations | ||
|
||
from kasa import Device | ||
from kasa.smartcam.smartcammodule import SmartCamModule | ||
|
||
from ...device_fixtures import parametrize | ||
|
||
battery_smartcam = parametrize( | ||
"has battery", | ||
component_filter="battery", | ||
protocol_filter={"SMARTCAM", "SMARTCAM.CHILD"}, | ||
) | ||
|
||
|
||
@battery_smartcam | ||
async def test_battery(dev: Device): | ||
"""Test device battery.""" | ||
battery = dev.modules.get(SmartCamModule.SmartCamBattery) | ||
assert battery | ||
|
||
feat_ids = { | ||
"battery_level", | ||
"battery_low", | ||
"battery_temperature", | ||
"battery_voltage", | ||
"battery_charging", | ||
} | ||
for feat_id in feat_ids: | ||
feat = dev.features.get(feat_id) | ||
assert feat | ||
assert feat.value is not None |
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.
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.
Was this change intentional?
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.
Yes, the previous mapping logic only mapped a fix set of keys but didn't leave the extra keys behind which we need to access other properties of sysinfo.