Skip to content

Commit

Permalink
refactor(controller): log ControllerX version first
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Jan 9, 2021
1 parent e5074e2 commit 5f04958
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 58 deletions.
10 changes: 2 additions & 8 deletions apps/controllerx/cx_core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class Controller(Hass, Mqtt):

async def initialize(self) -> None:
self.log(f"🎮 ControllerX {cx_version.__version__}", ascii_encode=False)
self.check_ad_version()
await self.init()

# Get arguments
async def init(self) -> None:
controllers_ids: List[str] = self.get_list(self.args["controller"])
self.integration = self.get_integration(self.args["integration"])

Expand Down Expand Up @@ -192,12 +192,6 @@ def get_integration(self, integration: Union[str, Dict[str, Any]]) -> Integratio
)
return next(i for i in integrations if i.name == integration_argument)

def check_ad_version(self) -> None:
ad_version = self.get_ad_version()
major, _, _ = ad_version.split(".")
if int(major) < 4:
raise ValueError("Please upgrade to AppDaemon 4.x")

def get_default_actions_mapping(
self, integration: Integration
) -> DefaultActionsMapping:
Expand Down
20 changes: 10 additions & 10 deletions apps/controllerx/cx_core/custom_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,50 @@


class CustomLightController(LightController):
async def initialize(self) -> None:
await super().initialize()
async def init(self) -> None:
self.log(
"⚠️ `CustomLightController` is deprecated and will be removed. Use `LightController` instead",
level="WARNING",
ascii_encode=False,
)
await super().init()


class CustomMediaPlayerController(MediaPlayerController):
async def initialize(self) -> None:
await super().initialize()
async def init(self) -> None:
self.log(
"⚠️ `CustomMediaPlayerController` is deprecated and will be removed. Use `MediaPlayerController` instead",
level="WARNING",
ascii_encode=False,
)
await super().init()


class CustomSwitchController(SwitchController):
async def initialize(self) -> None:
await super().initialize()
async def init(self) -> None:
self.log(
"⚠️ `CustomSwitchController` is deprecated and will be removed. Use `SwitchController` instead",
level="WARNING",
ascii_encode=False,
)
await super().init()


class CustomCoverController(CoverController):
async def initialize(self) -> None:
await super().initialize()
async def init(self) -> None:
self.log(
"⚠️ `CustomCoverController` is deprecated and will be removed. Use `CoverController` instead",
level="WARNING",
ascii_encode=False,
)
await super().init()


class CallServiceController(Controller):
async def initialize(self) -> None:
await super().initialize()
async def init(self) -> None:
self.log(
"⚠️ `CallServiceController` is deprecated and will be removed. Use `Controller` instead",
level="WARNING",
ascii_encode=False,
)
await super().init()
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/release_hold_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
class ReleaseHoldController(Controller, abc.ABC):
DEFAULT_MAX_LOOPS = 50

async def initialize(self):
async def init(self):
self.on_hold = False
self.delay = self.args.get("delay", self.default_delay())
self.max_loops = self.args.get(
"max_loops", ReleaseHoldController.DEFAULT_MAX_LOOPS
)
self.hold_release_toggle: bool = self.args.get("hold_release_toggle", False)
await super().initialize()
await super().init()

@action
async def release(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/type/cover_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class CoverController(TypeController[Entity]):
open_position: int
close_position: int

async def initialize(self) -> None:
async def init(self) -> None:
self.open_position = self.args.get("open_position", 100)
self.close_position = self.args.get("close_position", 0)
if self.open_position < self.close_position:
raise ValueError("`open_position` must be higher than `close_position`")
await super().initialize()
await super().init()

def _get_entity_type(self) -> Type[Entity]:
return Entity
Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/type/light_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class LightController(TypeController[LightEntity], ReleaseHoldController):
domains = ["light"]
entity_arg = "light"

async def initialize(self) -> None:
async def init(self) -> None:
manual_steps = self.args.get("manual_steps", DEFAULT_MANUAL_STEPS)
automatic_steps = self.args.get("automatic_steps", DEFAULT_AUTOMATIC_STEPS)
self.min_brightness = self.args.get("min_brightness", DEFAULT_MIN_BRIGHTNESS)
Expand Down Expand Up @@ -115,7 +115,7 @@ async def initialize(self) -> None:
self.add_transition_turn_toggle = self.args.get(
"add_transition_turn_toggle", DEFAULT_TRANSITION_TURN_TOGGLE
)
await super().initialize()
await super().init()

def _get_entity_type(self) -> Type[LightEntity]:
return LightEntity
Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/type/media_player_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class MediaPlayerController(TypeController[Entity], ReleaseHoldController):
domains = ["media_player"]
entity_arg = "media_player"

async def initialize(self) -> None:
async def init(self) -> None:
volume_steps = self.args.get("volume_steps", DEFAULT_VOLUME_STEPS)
self.volume_stepper = MinMaxStepper(0, 1, volume_steps)
self.volume_level = 0.0
await super().initialize()
await super().init()

def _get_entity_type(self) -> Type[Entity]:
return Entity
Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/type_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TypeController(Controller, abc.ABC, Generic[EntityType]):
entity: EntityType
feature_support: FeatureSupport

async def initialize(self) -> None:
async def init(self) -> None:
if self.entity_arg not in self.args:
raise ValueError(
f"{self.__class__.__name__} class needs the `{self.entity_arg}` attribute"
Expand All @@ -36,7 +36,7 @@ async def initialize(self) -> None:
self.feature_support = FeatureSupport(
self.entity.name, self, update_supported_features
)
await super().initialize()
await super().init()

@abc.abstractmethod
def _get_entity_type(self) -> Type[Entity]:
Expand Down
7 changes: 0 additions & 7 deletions tests/unit_tests/cx_core/controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,6 @@ def test_get_integration(
assert integration.name == integration_name_expected


def test_check_ad_version_throwing_error(sut: Controller, mocker: MockerFixture):
mocker.patch.object(sut, "get_ad_version", return_value="3.0.0")
with pytest.raises(ValueError) as e:
sut.check_ad_version()
assert str(e.value) == "Please upgrade to AppDaemon 4.x"


def test_get_default_actions_mapping_happyflow(sut, monkeypatch, mocker):
integration_mock = IntegrationMock("integration-test", sut, mocker)
monkeypatch.setattr(
Expand Down
13 changes: 6 additions & 7 deletions tests/unit_tests/cx_core/release_hold_controller_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from _pytest.monkeypatch import MonkeyPatch
from cx_core import Controller, ReleaseHoldController
from cx_core import ReleaseHoldController
from cx_core.controller import Controller
from pytest_mock import MockerFixture

from tests.test_utils import fake_fn
Expand All @@ -18,23 +19,21 @@ def default_delay(self) -> int:
def sut_before_init(mocker: MockerFixture) -> FakeReleaseHoldController:
controller = FakeReleaseHoldController() # type: ignore
controller.args = {}
mocker.patch.object(Controller, "initialize")
mocker.patch.object(Controller, "init")
mocker.patch.object(controller, "sleep")
return controller


@pytest.fixture
@pytest.mark.asyncio
async def sut(sut_before_init: FakeReleaseHoldController) -> FakeReleaseHoldController:
await sut_before_init.initialize()
await sut_before_init.init()
return sut_before_init


@pytest.mark.asyncio
async def test_initialize(
sut_before_init: FakeReleaseHoldController, mocker: MockerFixture
):
await sut_before_init.initialize()
async def test_init(sut_before_init: FakeReleaseHoldController, mocker: MockerFixture):
await sut_before_init.init()
assert sut_before_init.delay == 500


Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/cx_core/type/cover_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
@pytest.mark.asyncio
async def sut_before_init(mocker: MockerFixture) -> CoverController:
controller = CoverController() # type: ignore
mocker.patch.object(TypeController, "initialize")
mocker.patch.object(TypeController, "init")
return controller


@pytest.fixture
@pytest.mark.asyncio
async def sut(mocker: MockerFixture) -> CoverController:
controller = CoverController() # type: ignore
mocker.patch.object(Controller, "initialize")
mocker.patch.object(Controller, "init")
controller.args = {"cover": ENTITY_NAME}
await controller.initialize()
await controller.init()
return controller


Expand All @@ -42,7 +42,7 @@ async def sut(mocker: MockerFixture) -> CoverController:
],
)
@pytest.mark.asyncio
async def test_initialize(
async def test_init(
sut_before_init: CoverController,
open_position: int,
close_position: int,
Expand All @@ -54,7 +54,7 @@ async def test_initialize(
}

with wrap_exetuction(error_expected=error_expected, exception=ValueError):
await sut_before_init.initialize()
await sut_before_init.init()

if not error_expected:
assert sut_before_init.open_position == open_position
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_tests/cx_core/type/light_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
async def sut_before_init(mocker: MockerFixture) -> LightController:
controller = LightController() # type: ignore
controller.args = {}
mocker.patch.object(Controller, "initialize")
mocker.patch.object(Controller, "init")
return controller


@pytest.fixture
@pytest.mark.asyncio
async def sut(mocker: MockerFixture) -> LightController:
controller = LightController() # type: ignore
mocker.patch.object(Controller, "initialize")
mocker.patch.object(Controller, "init")
controller.args = {"light": ENTITY_NAME}
await controller.initialize()
await controller.init()
return controller


Expand Down Expand Up @@ -62,7 +62,7 @@ async def sut(mocker: MockerFixture) -> LightController:
],
)
@pytest.mark.asyncio
async def test_initialize(
async def test_init(
sut_before_init: LightController,
light_input: Union[str, Dict[str, str]],
expected_name: str,
Expand All @@ -73,7 +73,7 @@ async def test_initialize(

# SUT
with wrap_exetuction(error_expected=error_expected, exception=ValueError):
await sut_before_init.initialize()
await sut_before_init.init()

# Checks
if not error_expected:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/cx_core/type/media_player_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
@pytest.mark.asyncio
async def sut(mocker: MockerFixture) -> MediaPlayerController:
controller = MediaPlayerController() # type: ignore
mocker.patch.object(Controller, "initialize")
mocker.patch.object(Controller, "init")
controller.args = {"media_player": ENTITY_NAME}
await controller.initialize()
await controller.init()
return controller


Expand Down
8 changes: 4 additions & 4 deletions tests/unit_tests/cx_core/type_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def _get_entity_type(self) -> Type[MyEntity]:
def sut_before_init(mocker: MockerFixture) -> MyTypeController:
controller = MyTypeController() # type: ignore
controller.args = {ENTITY_ARG: ENTITY_NAME}
mocker.patch.object(Controller, "initialize")
mocker.patch.object(Controller, "init")
return controller


@pytest.fixture
@pytest.mark.asyncio
async def sut(sut_before_init: MyTypeController) -> MyTypeController:
await sut_before_init.initialize()
await sut_before_init.init()
return sut_before_init


Expand All @@ -56,13 +56,13 @@ async def sut(sut_before_init: MyTypeController) -> MyTypeController:
({}, True),
],
)
async def test_initialize(
async def test_init(
sut_before_init: MyTypeController, args: Dict[str, Any], error_expected: bool
):
sut_before_init.args = args

with wrap_exetuction(error_expected=error_expected, exception=ValueError):
await sut_before_init.initialize()
await sut_before_init.init()

if not error_expected:
assert sut_before_init.entity.name == ENTITY_NAME
Expand Down

0 comments on commit 5f04958

Please sign in to comment.