Skip to content

Commit

Permalink
fix(light_controller): add transition to off and toggle #58
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Mar 15, 2020
1 parent bdc6188 commit 454ff93
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
18 changes: 13 additions & 5 deletions apps/controllerx/core/type/light_controller.py
Expand Up @@ -255,16 +255,24 @@ async def on(self, **attributes):
if "transition" not in attributes:
attributes["transition"] = self.transition / 1000
self.call_service(
"homeassistant/turn_on", entity_id=self.light["name"], **attributes,
"homeassistant/turn_on", entity_id=self.light["name"], **attributes
)

@action
async def off(self):
self.call_service("homeassistant/turn_off", entity_id=self.light["name"])
async def off(self, **attributes):
if "transition" not in attributes:
attributes["transition"] = self.transition / 1000
self.call_service(
"homeassistant/turn_off", entity_id=self.light["name"], **attributes
)

@action
async def toggle(self):
self.call_service("homeassistant/toggle", entity_id=self.light["name"])
async def toggle(self, **attributes):
if "transition" not in attributes:
attributes["transition"] = self.transition / 1000
self.call_service(
"homeassistant/toggle", entity_id=self.light["name"], **attributes
)

@action
async def set_value(self, attribute, fraction):
Expand Down
30 changes: 24 additions & 6 deletions tests/core/type/light_controller_test.py
Expand Up @@ -191,21 +191,39 @@ async def test_on(sut, mocker, attributes_input, transition, attributes_expected
)


@pytest.mark.parametrize(
"attributes_input, transition, attributes_expected",
[
({"test": "test"}, 300, {"test": "test", "transition": 0.3}),
({"test": "test", "transition": 0.5}, 300, {"test": "test", "transition": 0.5}),
({}, 1000, {"transition": 1}),
],
)
@pytest.mark.asyncio
async def test_off(sut, mocker):
async def test_off(sut, mocker, attributes_input, transition, attributes_expected):
called_service_patch = mocker.patch.object(sut, "call_service")
await sut.off()
sut.transition = transition
await sut.off(**attributes_input)
called_service_patch.assert_called_once_with(
"homeassistant/turn_off", entity_id=sut.light["name"]
"homeassistant/turn_off", entity_id=sut.light["name"], **attributes_expected
)


@pytest.mark.parametrize(
"attributes_input, transition, attributes_expected",
[
({"test": "test"}, 300, {"test": "test", "transition": 0.3}),
({"test": "test", "transition": 0.5}, 300, {"test": "test", "transition": 0.5}),
({}, 1000, {"transition": 1}),
],
)
@pytest.mark.asyncio
async def test_toggle(sut, mocker):
async def test_toggle(sut, mocker, attributes_input, transition, attributes_expected):
called_service_patch = mocker.patch.object(sut, "call_service")
await sut.toggle()
sut.transition = transition
await sut.toggle(**attributes_input)
called_service_patch.assert_called_once_with(
"homeassistant/toggle", entity_id=sut.light["name"]
"homeassistant/toggle", entity_id=sut.light["name"], **attributes_expected
)


Expand Down

0 comments on commit 454ff93

Please sign in to comment.