Skip to content

feat: time based power plug #833

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

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/aioswitcher/api/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ def __post_init__(self) -> None:
parser = StateMessageParser(self.unparsed_response)

self.state = parser.get_state()
self.time_left = parser.get_time_left()
self.time_left = (
parser.get_time_left()
if self.state == DeviceState.ON and parser.get_auto_shutdown() != "00:00:00"
else "00:00:00"
)
self.time_on = parser.get_time_on()
self.auto_shutdown = parser.get_auto_shutdown()
self.power_consumption = parser.get_power_consumption()
Expand Down
7 changes: 7 additions & 0 deletions src/aioswitcher/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ def _parse_device_from_datagram(
device_type.token_needed,
power_consumption,
electric_current,
(
parser.get_remaining()
if device_state == DeviceState.ON
and parser.get_auto_shutdown() != "00:00:00"
else "00:00:00"
),
parser.get_auto_shutdown(),
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/aioswitcher/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ class SwitcherLightBase(ABC):

@final
@dataclass
class SwitcherPowerPlug(SwitcherPowerBase, SwitcherBase):
class SwitcherPowerPlug(SwitcherTimedBase, SwitcherPowerBase, SwitcherBase):
"""Implementation of the Switcher Power Plug device.

Please Note the order of the inherited classes to understand the order of the
Expand Down
2 changes: 2 additions & 0 deletions src/aioswitcher/device/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def seconds_to_iso_time(all_seconds: int) -> str:
"""
minutes, seconds = divmod(int(all_seconds), 60)
hours, minutes = divmod(minutes, 60)
if hours > 24:
hours = 0

return datetime.time(hour=hours, minute=minutes, second=seconds).isoformat()

Expand Down
4 changes: 3 additions & 1 deletion tests/test_api_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ def test_switcher_state_response_dataclass(get_power_consumption, get_auto_shutd
assert_that(sut.power_consumption).is_equal_to(1640)
assert_that(sut.electric_current).is_equal_to(7.5)

for mock_method in [get_power_consumption, get_auto_shutdown, get_time_on, get_time_left, get_state]:
for mock_method in [get_power_consumption, get_time_on, get_time_left, get_state]:
mock_method.assert_called_once()
get_auto_shutdown.assert_called()
assert get_auto_shutdown.call_count == 2


@patch(messages.__name__ + ".get_schedules", return_value={Mock(), Mock()})
Expand Down
7 changes: 7 additions & 0 deletions tests/test_device_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def test_given_a_device_of_type_power_plug_when_instantiating_as_a_power_plug_sh
fake_data.token_needed,
fake_data.power_consumption,
fake_data.electric_current,
fake_data.remaining_time,
fake_data.auto_shutdown,
)

assert_that(sut.device_type).is_equal_to(DeviceType.POWER_PLUG)
Expand All @@ -125,6 +127,9 @@ def test_given_a_device_of_type_power_plug_when_instantiating_as_a_power_plug_sh
assert_that(sut.name).is_equal_to(fake_data.name)
assert_that(sut.power_consumption).is_equal_to(fake_data.power_consumption)
assert_that(sut.electric_current).is_equal_to(fake_data.electric_current)
assert_that(sut.remaining_time).is_equal_to(fake_data.remaining_time)
assert_that(sut.auto_shutdown).is_equal_to(fake_data.auto_shutdown)
assert_that(sut.auto_off_set).is_equal_to(fake_data.auto_shutdown)


def test_given_a_device_of_type_thermostat_when_instantiating_as_a_thermostat_should_be_instatiated_properly(fake_data):
Expand Down Expand Up @@ -276,6 +281,8 @@ def test_given_a_device_of_type_water_heater_when_instantiating_as_a_power_plug_
fake_data.token_needed,
fake_data.power_consumption,
fake_data.electric_current,
fake_data.remaining_time,
fake_data.auto_shutdown,
).is_equal_to("only power plugs are allowed")


Expand Down