FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add 'battery_percentage' only when it's available by rytilahti · Pull Request #906 · python-kasa/python-kasa · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
39 changes: 19 additions & 20 deletions kasa/smart/modules/battery.py
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
Expand Up @@ -2,38 +2,21 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from ...feature import Feature
from ..smartmodule import SmartModule

if TYPE_CHECKING:
from ..smartdevice import SmartDevice


class BatterySensor(SmartModule):
"""Implementation of battery module."""

REQUIRED_COMPONENT = "battery_detect"
QUERY_GETTER_NAME = "get_battery_detect_info"

def __init__(self, device: SmartDevice, module: str):
super().__init__(device, module)
def _initialize_features(self):
"""Initialize features."""
self._add_feature(
Feature(
device,
"battery_level",
"Battery level",
container=self,
attribute_getter="battery",
icon="mdi:battery",
unit="%",
category=Feature.Category.Info,
)
)
self._add_feature(
Feature(
device,
self._device,
"battery_low",
"Battery low",
container=self,
Expand All @@ -44,6 +27,22 @@ def __init__(self, device: SmartDevice, module: str):
)
)

# Some devices, like T110 contact sensor do not report the battery percentage
if "battery_percentage" in self._device.sys_info:
self._add_feature(
Feature(
self._device,
"battery_level",
"Battery level",
container=self,
attribute_getter="battery",
icon="mdi:battery",
unit="%",
category=Feature.Category.Info,
type=Feature.Type.Sensor,
)
)

@property
def battery(self):
"""Return battery level."""
Expand Down

Back | FazBrowse Home | New Git URL