Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
poetry run coverage xml -i

- name: Upload coverage report
uses: codecov/codecov-action@v6.0.1
uses: codecov/codecov-action@v6.0.2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be in a separate PR

with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async def main():
# Get all data and remap v1 data to new v2 structure
print(await api.combined())

# Turn on the Energy Socket outlet
await api.state(power_on=True)
# Turn on the Energy Socket outlet (works with both v1 and v2 API)
await api.system(power_on=True)


asyncio.run(main())
Expand Down
3 changes: 3 additions & 0 deletions homewizard_energy/homewizard_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ async def telegram(self) -> str:
"""Get the latest telegram."""
raise NotImplementedError

# pylint: disable-next=too-many-arguments,too-many-positional-arguments
async def system(
self,
cloud_enabled: bool | None = None,
status_led_brightness_pct: int | None = None,
api_v1_enabled: bool | None = None,
power_on: bool | None = None,
switch_lock: bool | None = None,
) -> System:
"""Get/set the system."""
raise NotImplementedError
Expand Down
16 changes: 16 additions & 0 deletions homewizard_energy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ def __init__(
self.system = System()
self.system.status_led_brightness_pct = (self.state.brightness / 255) * 100

## state.power_on -> system.power_on
if self.state is not None and self.state.power_on is not None:
if self.system is None:
self.system = System()
self.system.power_on = self.state.power_on

## state.switch_lock -> system.switch_lock
if self.state is not None and self.state.switch_lock is not None:
if self.system is None:
self.system = System()
self.system.switch_lock = self.state.switch_lock


def get_verification_hostname(model: str, serial_number: str) -> str:
"""Helper method to convert device model and serial to identifier
Expand Down Expand Up @@ -595,6 +607,8 @@ class SystemUpdate(UpdateBaseModel):
cloud_enabled: bool | None = field(default=None)
status_led_brightness_pct: int | None = field(default=None)
api_v1_enabled: bool | None = field(default=None)
power_on: bool | None = field(default=None)
switch_lock: bool | None = field(default=None)


@dataclass(kw_only=True)
Expand All @@ -609,6 +623,8 @@ class System(BaseModel):
uptime_s: int | None = field(default=None)
status_led_brightness_pct: int | None = field(default=None)
api_v1_enabled: bool | None = field(default=None)
power_on: bool | None = field(default=None)
switch_lock: bool | None = field(default=None)

@classmethod
def __post_deserialize__(cls, obj: System) -> System:
Expand Down
30 changes: 26 additions & 4 deletions homewizard_energy/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,44 @@ async def telegram(self) -> str:
return telegram

@optional_method
# pylint: disable-next=too-many-arguments,too-many-positional-arguments
async def system(
self,
cloud_enabled: bool | None = None,
status_led_brightness_pct: int | None = None,
api_v1_enabled: bool | None = None,
power_on: bool | None = None,
switch_lock: bool | None = None,
) -> System:
"""Return the system object."""

# Legacy: raise on unsupported field
if api_v1_enabled is not None:
raise UnsupportedError("Setting api_v1_enabled is not supported in v1")

# Legacy: route 'status_led_brightness_pct' to state
if status_led_brightness_pct is not None:
state = await self.state(brightness=status_led_brightness_pct * 2.55)
return System(status_led_brightness_pct=state.brightness / 2.55)
# Legacy: route 'status_led_brightness_pct', 'power_on' and 'switch_lock'
# to state, as v1 exposes these via the state endpoint
if (
status_led_brightness_pct is not None
or power_on is not None
or switch_lock is not None
):
state = await self.state(
power_on=power_on,
switch_lock=switch_lock,
brightness=(
status_led_brightness_pct * 2.55
if status_led_brightness_pct is not None
else None
),
)
return System(
status_led_brightness_pct=(
state.brightness / 2.55 if state.brightness is not None else None
),
power_on=state.power_on,
switch_lock=state.switch_lock,
)

if cloud_enabled is not None:
# Executing the update
Expand Down
13 changes: 13 additions & 0 deletions homewizard_energy/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,31 @@ async def system(
cloud_enabled: bool | None = None,
status_led_brightness_pct: int | None = None,
api_v1_enabled: bool | None = None,
power_on: bool | None = None,
switch_lock: bool | None = None,
) -> System:
"""Return the system object."""

if (
(power_on is not None or switch_lock is not None)
and self._device is not None
and self._device.supports_state() is False
):
raise UnsupportedError("Setting power_on or switch_lock is not supported")

if (
cloud_enabled is not None
or status_led_brightness_pct is not None
or api_v1_enabled is not None
or power_on is not None
or switch_lock is not None
):
data = SystemUpdate(
cloud_enabled=cloud_enabled,
status_led_brightness_pct=status_led_brightness_pct,
api_v1_enabled=api_v1_enabled,
power_on=power_on,
switch_lock=switch_lock,
).to_dict()
status, response = await self._request(
"/api/system", method=METH_PUT, data=data
Expand Down
Loading
Loading