Skip to content

Commit

Permalink
refactor(import): sort imports by its importance
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Apr 26, 2020
1 parent 98636a5 commit 362059a
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 45 deletions.
2 changes: 2 additions & 0 deletions apps/controllerx/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
)
from core.type.light_controller import LightController
from core.type.media_player_controller import MediaPlayerController
from core.type.switch_controller import SwitchController

__all__ = [
"Controller",
"ReleaseHoldController",
"LightController",
"MediaPlayerController",
"SwitchController",
"CustomLightController",
"CustomMediaPlayerController",
"CallServiceController",
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/core/custom_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
from typing import Dict, List, Tuple, Union
from const import TypeAction, TypeActionsMapping

from const import TypeAction, TypeActionsMapping
from core.controller import Controller, action
from core.type.light_controller import LightController
from core.type.media_player_controller import MediaPlayerController
Expand Down
1 change: 1 addition & 0 deletions apps/controllerx/core/integration/deconz.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional

from core.integration import Integration, TypeActionsMapping


Expand Down
1 change: 1 addition & 0 deletions apps/controllerx/core/integration/state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional

from const import TypeActionsMapping
from core.integration import Integration

Expand Down
1 change: 1 addition & 0 deletions apps/controllerx/core/integration/zha.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional

from const import TypeActionsMapping
from core.integration import Integration

Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/core/stepper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import abc
from typing import Tuple, Union
from collections import namedtuple
from typing import Tuple, Union

MinMax = namedtuple("MinMax", "min max")

Expand Down
3 changes: 2 additions & 1 deletion apps/controllerx/core/stepper/circular_stepper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Tuple
from core.stepper import Stepper, MinMax

from core.stepper import MinMax, Stepper


class CircularStepper(Stepper):
Expand Down
1 change: 1 addition & 0 deletions apps/controllerx/core/stepper/minmax_stepper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Tuple

from core.stepper import MinMax, Stepper


Expand Down
27 changes: 19 additions & 8 deletions apps/controllerx/core/type/light_controller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from typing import Any, Dict, List, Tuple, Union

from const import Light, TypeActionsMapping
from core import light_features
from core.controller import (
ReleaseHoldController,
TypeController,
action,
)
from core.controller import ReleaseHoldController, TypeController, action
from core.stepper import Stepper
from core.stepper.circular_stepper import CircularStepper
from core.stepper.minmax_stepper import MinMaxStepper
Expand Down Expand Up @@ -344,14 +341,28 @@ def get_attribute(self, attribute: str) -> str:
async def get_value_attribute(self, attribute: str) -> Union[float, int]:
if attribute == LightController.ATTRIBUTE_XY_COLOR:
return 0
else:
elif (
attribute == LightController.ATTRIBUTE_BRIGHTNESS
or attribute == LightController.ATTRIBUTE_COLOR_TEMP
):
value = await self.get_entity_state(self.light["name"], attribute)
if value is None:
raise ValueError(
f"Value for `{attribute}` attribute could not be retrieved"
f"Value for `{attribute}` attribute could not be retrieved "
f"from `{self.light['name']}`. "
"Check the FAQ to know more about this error: "
"https://xaviml.github.io/controllerx/faq"
)
else:
return float(value)
try:
return float(value)
except ValueError:
raise ValueError(
f"Attribute `{attribute}` with `{value}` as a value "
"could not be converted to float"
)
else:
raise ValueError(f"Attribute `{attribute}` not expected")

def check_smooth_power_on(
self, attribute: str, direction: str, light_state: str
Expand Down
6 changes: 1 addition & 5 deletions apps/controllerx/core/type/media_player_controller.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from const import MediaPlayer, TypeActionsMapping
from core.controller import (
ReleaseHoldController,
TypeController,
action,
)
from core.controller import ReleaseHoldController, TypeController, action
from core.stepper import Stepper
from core.stepper.circular_stepper import CircularStepper
from core.stepper.minmax_stepper import MinMaxStepper
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/devices/ikea.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from core import LightController, MediaPlayerController, action
from const import Light, MediaPlayer, TypeActionsMapping
from core import LightController, MediaPlayerController, action
from core.type.switch_controller import SwitchController


Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/devices/lutron.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from core import LightController, MediaPlayerController
from const import Light, MediaPlayer, TypeActionsMapping
from core import LightController, MediaPlayerController


class LutronCasetaProPicoLightController(LightController):
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/devices/philips.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from core import LightController
from const import Light, TypeActionsMapping
from core import LightController


class HueDimmerController(LightController):
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/devices/smartthings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from core import LightController, MediaPlayerController
from const import Light, MediaPlayer, TypeActionsMapping
from core import LightController, MediaPlayerController


class SmartThingsButtonLightController(LightController):
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/devices/trust.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from core import LightController, MediaPlayerController
from const import Light, MediaPlayer, TypeActionsMapping
from core import LightController, MediaPlayerController


class ZYCT202LightController(LightController):
Expand Down
2 changes: 1 addition & 1 deletion docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ HA offers different ways to group lights, even each Light integration might have

This does not mean that any other integration will not work, but they might not work as expected, this is why [Group](https://www.home-assistant.io/integrations/group/) integration should be used if you want the expected ControllerX behaviour.

#### 5. Error: "Value for X attribute could not be retrieved"
#### 5. Error: "Value for X attribute could not be retrieved from light Y"

This error is shown when the light has support for the X attribute (e.g. brightness or color_temp) and the attribute is not in the state attribute of the entity. You can check whether the attribute X is shown in the state attributes from the "Developer Tools > States".
40 changes: 24 additions & 16 deletions tests/core/type/light_controller_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from core.light_features import FEATURES, SUPPORT_COLOR, SUPPORT_COLOR_TEMP
import pytest

from core import LightController, ReleaseHoldController
from tests.test_utils import hass_mock, fake_async_function
from core import LightController, ReleaseHoldController, light_features
from core.light_features import SUPPORT_COLOR, SUPPORT_COLOR_TEMP
from core.stepper import Stepper
from core.stepper.minmax_stepper import MinMaxStepper
from core.stepper.circular_stepper import CircularStepper
from core import light_features
from core.stepper.minmax_stepper import MinMaxStepper
from tests.test_utils import fake_async_function, hass_mock


@pytest.fixture
Expand Down Expand Up @@ -115,10 +114,14 @@ def test_get_attribute(
[
("xy_color", 0, False),
("brightness", 3.0, False),
("brightness", "3.0", False),
("brightness", "3", False),
("brightness", "error", True),
("color_temp", 1, False),
("xy_color", 0, False),
("brightness", None, True),
("color_temp", None, True),
("not_a_valid_attribute", None, True),
],
)
@pytest.mark.asyncio
Expand All @@ -138,7 +141,7 @@ async def fake_get_entity_state(entity, attribute):
output = await sut.get_value_attribute(attribute_input)

# Checks
assert output == expected_output
assert output == float(expected_output)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -282,28 +285,33 @@ async def test_toggle(sut, mocker, monkeypatch):


@pytest.mark.parametrize(
"min_max, fraction, expected_value",
"stepper_cls, min_max, fraction, expected_calls, expected_value",
[
((1, 255), 0, 1),
((1, 255), 1, 255),
((0, 10), 0.5, 5),
((0, 100), 0.2, 20),
((0, 100), -1, 0),
((0, 100), 1.5, 100),
(MinMaxStepper, (1, 255), 0, 1, 1),
(MinMaxStepper, (1, 255), 1, 1, 255),
(MinMaxStepper, (0, 10), 0.5, 1, 5),
(MinMaxStepper, (0, 100), 0.2, 1, 20),
(MinMaxStepper, (0, 100), -1, 1, 0),
(MinMaxStepper, (0, 100), 1.5, 1, 100),
(CircularStepper, (0, 100), 0, 0, None),
],
)
@pytest.mark.asyncio
async def test_set_value(sut, mocker, min_max, fraction, expected_value):
async def test_set_value(
sut, mocker, stepper_cls, min_max, fraction, expected_calls, expected_value
):
attribute = "test_attribute"
on_patch = mocker.patch.object(sut, "on")
stepper = MinMaxStepper(min_max[0], min_max[1], 1)
stepper = stepper_cls(min_max[0], min_max[1], 1)
sut.automatic_steppers = {attribute: stepper}

# SUT
await sut.set_value(attribute, fraction)

# Checks
on_patch.assert_called_once_with(**{attribute: expected_value})
assert on_patch.call_count == expected_calls
if expected_calls > 0:
on_patch.assert_called_with(**{attribute: expected_value})


@pytest.mark.asyncio
Expand Down
2 changes: 1 addition & 1 deletion tests/core/type/switch_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from core.controller import TypeController
from tests.test_utils import hass_mock
from core.type.switch_controller import SwitchController
from core import SwitchController


@pytest.fixture
Expand Down
13 changes: 7 additions & 6 deletions tests/devices_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from core import Controller


def check_mapping(mapping, all_possible_actions, device):
def check_mapping(mapping, all_possible_actions, device_name):
if mapping is None:
return
for k, v in mapping.items():
Expand All @@ -12,12 +12,12 @@ def check_mapping(mapping, all_possible_actions, device):
"The value from the mapping should be a string, matching "
+ "one of the actions from the controller. "
+ f"The possible actions are: {all_possible_actions}. "
+ f"Device class: {device.__class__.__name__}"
+ f"Device class: {device_name}"
)

if v not in all_possible_actions:
raise ValueError(
f"{v} not found in the list of possible action from the controller. "
f"{device_name}: `{v}` not found in the list of possible action from the controller. "
+ f"The possible actions are: {all_possible_actions}"
)

Expand All @@ -27,13 +27,14 @@ def test_devices(hass_mock):
devices_module.__file__, devices_module.__package__, Controller
)
for device in devices:
device_name = device.__class__.__name__
type_actions_mapping = device.get_type_actions_mapping()
if type_actions_mapping is None:
continue
possible_actions = list(type_actions_mapping.keys())
mappings = device.get_z2m_actions_mapping()
check_mapping(mappings, possible_actions, device)
check_mapping(mappings, possible_actions, device_name)
mappings = device.get_deconz_actions_mapping()
check_mapping(mappings, possible_actions, device)
check_mapping(mappings, possible_actions, device_name)
mappings = device.get_zha_actions_mapping()
check_mapping(mappings, possible_actions, device)
check_mapping(mappings, possible_actions, device_name)

0 comments on commit 362059a

Please sign in to comment.