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

Add support for feature units by rytilahti · Pull Request #841 · python-kasa/python-kasa · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) 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
8 changes: 2 additions & 6 deletions kasa/cli.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 @@ -614,12 +614,8 @@ async def state(ctx, dev: Device):

echo("\n\t[bold]== Device-specific information == [/bold]")
for id_, feature in dev.features.items():
echo(f"\t{feature.name} ({id_}): {feature.value}")

if dev.has_emeter:
echo("\n\t[bold]== Current State ==[/bold]")
emeter_status = dev.emeter_realtime
echo(f"\t{emeter_status}")
unit = f" {feature.unit}" if feature.unit else ""
echo(f"\t{feature.name} ({id_}): {feature.value}{unit}")

echo("\n\t[bold]== Modules ==[/bold]")
for module in dev.modules.values():
Expand Down
2 changes: 2 additions & 0 deletions kasa/feature.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 @@ -36,6 +36,8 @@ class Feature:
container: Any = None
#: Icon suggestion
icon: str | None = None
#: Unit, if applicable
unit: str | None = None
#: Type of the feature
type: FeatureType = FeatureType.Sensor

Expand Down
29 changes: 14 additions & 15 deletions kasa/smart/modules/energymodule.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 @@ -25,24 +25,27 @@ def __init__(self, device: SmartDevice, module: str):
name="Current consumption",
attribute_getter="current_power",
container=self,
unit="W",
)
) # W or mW?
)
self._add_feature(
Feature(
device,
name="Today's consumption",
attribute_getter="emeter_today",
container=self,
unit="Wh",
)
) # Wh or kWh?
)
self._add_feature(
Feature(
device,
name="This month's consumption",
attribute_getter="emeter_this_month",
container=self,
unit="Wh",
)
) # Wh or kWH?
)

def query(self) -> dict:
"""Query to execute during the update cycle."""
Expand All @@ -54,9 +57,11 @@ def query(self) -> dict:
return req

@property
def current_power(self):
"""Current power."""
return self.emeter_realtime.power
def current_power(self) -> float | None:
"""Current power in watts."""
if power := self.energy.get("current_power"):
return power / 1_000
return None

@property
def energy(self):
Expand All @@ -72,22 +77,16 @@ def emeter_realtime(self):
return EmeterStatus(
{
"power_mw": self.energy.get("current_power"),
"total": self._convert_energy_data(
self.energy.get("today_energy"), 1 / 1000
),
"total": self.energy.get("today_energy") / 1_000,
}
)

@property
def emeter_this_month(self) -> float | None:
"""Get the emeter value for this month."""
return self._convert_energy_data(self.energy.get("month_energy"), 1 / 1000)
return self.energy.get("month_energy")

@property
def emeter_today(self) -> float | None:
"""Get the emeter value for today."""
return self._convert_energy_data(self.energy.get("today_energy"), 1 / 1000)

def _convert_energy_data(self, data, scale) -> float | None:
"""Return adjusted emeter information."""
return data if not data else data * scale
return self.energy.get("today_energy")

Back | FazBrowse Home | New Git URL