Skip to content
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

Add common power configuration cluster #168

Merged
merged 1 commit into from
Sep 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions zhaquirks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,37 @@ def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)


class PowerConfigurationCluster(CustomCluster, PowerConfiguration):
"""Common use power configuration cluster."""

cluster_id = PowerConfiguration.cluster_id
BATTERY_VOLTAGE_ATTR = 0x0020
BATTERY_PERCENTAGE_REMAINING = 0x0021
MIN_VOLTS = 2.1
MAX_VOLTS = 3.2

def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == self.BATTERY_VOLTAGE_ATTR:
super()._update_attribute(
self.BATTERY_PERCENTAGE_REMAINING,
self._calculate_battery_percentage(value),
)

def _calculate_battery_percentage(self, raw_value):
if raw_value in (0, 255):
return -1
volts = raw_value / 10
if volts < self.MIN_VOLTS:
volts = self.MIN_VOLTS
elif volts > self.MAX_VOLTS:
volts = self.MAX_VOLTS

percent = ((volts - self.MIN_VOLTS) / (self.MAX_VOLTS - self.MIN_VOLTS)) * 200

return round(min(200, percent), 2)


NAME = __name__
PATH = __path__
for importer, modname, ispkg in pkgutil.walk_packages(path=PATH, prefix=NAME + "."):
Expand Down