From ff9ddfe40e6c9c2818af455cd0ee6cfec18980b9 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Tue, 26 Oct 2021 18:28:02 +0200 Subject: [PATCH] use WaterAmount in WaterInfoEventDto (#95) * use WaterAmount in WaterInfoEventDto * fix equal of DisplayNameIntEnum * fix pylint --- deebotozmo/cli.py | 15 ++++++++------- deebotozmo/commands/__init__.py | 2 +- deebotozmo/commands/water_info.py | 22 +++++----------------- deebotozmo/events/__init__.py | 13 ++----------- deebotozmo/events/base.py | 5 +++++ deebotozmo/events/water_info.py | 23 +++++++++++++++++++++++ deebotozmo/util.py | 16 ++++++++++++++-- setup.cfg | 2 ++ tests/commands/test_fan_speed.py | 3 +-- tests/commands/test_water_info.py | 10 ---------- tests/events/test_water_info.py | 7 +++++++ tests/{commands => }/helpers.py | 0 12 files changed, 68 insertions(+), 50 deletions(-) create mode 100644 deebotozmo/events/base.py create mode 100644 deebotozmo/events/water_info.py delete mode 100644 tests/commands/test_water_info.py create mode 100644 tests/events/test_water_info.py rename tests/{commands => }/helpers.py (100%) diff --git a/deebotozmo/cli.py b/deebotozmo/cli.py index 17da2df..668fe8d 100755 --- a/deebotozmo/cli.py +++ b/deebotozmo/cli.py @@ -1,5 +1,12 @@ #!/usr/bin/env python3 """Cli module.""" +import sys + +try: + import click +except ModuleNotFoundError: + sys.exit('Dependencies missing!! Please run "pip install deebotozmo[cli]"') + import asyncio import base64 import configparser @@ -9,7 +16,6 @@ import mimetypes import os import platform -import sys import time from dataclasses import asdict from functools import wraps @@ -17,11 +23,6 @@ import aiohttp -try: - import click -except ModuleNotFoundError: - sys.exit('Dependencies missing!! Please run "pip install deebotozmo[cli]"') - from deebotozmo import create_instances from deebotozmo.commands import Charge, Clean, PlaySound, SetFanSpeed, SetWaterInfo from deebotozmo.commands.clean import CleanAction, CleanArea, CleanMode @@ -333,7 +334,7 @@ async def on_fan_event(fan_speed_event: FanSpeedEventDto) -> None: fan_speed_listener.unsubscribe() async def on_water_level(water_info_event: WaterInfoEventDto) -> None: - print(f"Water Level: {water_info_event.amount}") + print(f"Water Level: {water_info_event.amount.display_name}") event.set() event.clear() diff --git a/deebotozmo/commands/__init__.py b/deebotozmo/commands/__init__.py index a48ca59..cf5c7a6 100644 --- a/deebotozmo/commands/__init__.py +++ b/deebotozmo/commands/__init__.py @@ -22,7 +22,7 @@ from .play_sound import PlaySound from .relocation import SetRelocationState from .stats import GetStats -from .water_info import GetWaterInfo, SetWaterInfo, WaterLevel +from .water_info import GetWaterInfo, SetWaterInfo # fmt: off # ordered by file asc diff --git a/deebotozmo/commands/water_info.py b/deebotozmo/commands/water_info.py index a2571e9..6ae1175 100644 --- a/deebotozmo/commands/water_info.py +++ b/deebotozmo/commands/water_info.py @@ -2,22 +2,12 @@ import logging from typing import Any, Dict, Mapping, Union -from ..events import WaterInfoEventDto -from ..util import DisplayNameIntEnum +from ..events import WaterAmount, WaterInfoEventDto from .common import EventBus, SetCommand, _NoArgsCommand _LOGGER = logging.getLogger(__name__) -class WaterLevel(DisplayNameIntEnum): - """Enum class for all possible water levels.""" - - LOW = 1 - MEDIUM = 2 - HIGH = 3 - ULTRAHIGH = 4 - - class GetWaterInfo(_NoArgsCommand): """Get water info command.""" @@ -35,9 +25,7 @@ def _handle_body_data_dict(cls, event_bus: EventBus, data: Dict[str, Any]) -> bo if amount is not None: try: event_bus.notify( - WaterInfoEventDto( - mop_attached, WaterLevel(int(amount)).display_name - ) + WaterInfoEventDto(mop_attached, WaterAmount(int(amount))) ) return True except ValueError: @@ -54,13 +42,13 @@ class SetWaterInfo(SetCommand): get_command = GetWaterInfo def __init__( - self, amount: Union[str, int, WaterLevel], **kwargs: Mapping[str, Any] + self, amount: Union[str, int, WaterAmount], **kwargs: Mapping[str, Any] ) -> None: # removing "enable" as we don't can set it remove_from_kwargs = ["enable"] if isinstance(amount, str): - amount = WaterLevel.get(amount) - if isinstance(amount, WaterLevel): + amount = WaterAmount.get(amount) + if isinstance(amount, WaterAmount): amount = amount.value super().__init__({"amount": amount, "enable": 0}, remove_from_kwargs, **kwargs) diff --git a/deebotozmo/events/__init__.py b/deebotozmo/events/__init__.py index 700f192..2e6d0f0 100644 --- a/deebotozmo/events/__init__.py +++ b/deebotozmo/events/__init__.py @@ -4,12 +4,11 @@ from enum import Enum, unique from typing import Any, Dict, List, Optional +from deebotozmo.events.base import EventDto from deebotozmo.models import Room, VacuumState from deebotozmo.util import DisplayNameIntEnum - -class EventDto: - """Event base class.""" +from .water_info import WaterAmount, WaterInfoEventDto @dataclass(frozen=True) @@ -136,11 +135,3 @@ class StatusEventDto(EventDto): available: bool state: Optional[VacuumState] - - -@dataclass(frozen=True) -class WaterInfoEventDto(EventDto): - """Water info event representation.""" - - mop_attached: bool - amount: str diff --git a/deebotozmo/events/base.py b/deebotozmo/events/base.py new file mode 100644 index 0000000..d7934ef --- /dev/null +++ b/deebotozmo/events/base.py @@ -0,0 +1,5 @@ +"""Base event module.""" + + +class EventDto: + """Event base class.""" diff --git a/deebotozmo/events/water_info.py b/deebotozmo/events/water_info.py new file mode 100644 index 0000000..ebb889c --- /dev/null +++ b/deebotozmo/events/water_info.py @@ -0,0 +1,23 @@ +"""Water info event module.""" +from dataclasses import dataclass + +from deebotozmo.util import DisplayNameIntEnum + +from .base import EventDto + + +class WaterAmount(DisplayNameIntEnum): + """Enum class for all possible water amounts.""" + + LOW = 1 + MEDIUM = 2 + HIGH = 3 + ULTRAHIGH = 4 + + +@dataclass(frozen=True) +class WaterInfoEventDto(EventDto): + """Water info event representation.""" + + mop_attached: bool + amount: WaterAmount diff --git a/deebotozmo/util.py b/deebotozmo/util.py index b13b4e1..89f786d 100644 --- a/deebotozmo/util.py +++ b/deebotozmo/util.py @@ -37,13 +37,14 @@ class DisplayNameIntEnum(IntEnum): """Int enum with a property "display_name".""" def __new__(cls, *args: Tuple, **_: Mapping) -> "DisplayNameIntEnum": - """Create new enum.""" + """Create new DisplayNameIntEnum.""" obj = int.__new__(cls) obj._value_ = args[0] return obj - def __init__(self, _: int, display_name: Optional[str] = None): + def __init__(self, value: int, display_name: Optional[str] = None): super().__init__() + self._value_ = value self._display_name = display_name @property @@ -66,3 +67,14 @@ def get(cls, value: str) -> "DisplayNameIntEnum": return member raise ValueError(f"'{value}' is not a valid {cls.__name__} member") + + def __eq__(self, x: object) -> bool: + if not isinstance(x, type(self)): + return False + return bool(self._value_ == x._value_) + + def __ne__(self, x: object) -> bool: + return not self.__eq__(x) + + def __hash__(self) -> int: + return hash(self._value_) diff --git a/setup.cfg b/setup.cfg index fd74405..67524cb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,12 +5,14 @@ max-line-length = 88 # W503: Line break occurred before a binary operator # E203: Whitespace before ':' # D202 No blank lines allowed after function docstring +# D105 Missing docstring in magic method # D107 Missing docstring in __init__ ignore = E501, W503, E203, D202, + D105, D107 # Disable unused imports for __init__.py diff --git a/tests/commands/test_fan_speed.py b/tests/commands/test_fan_speed.py index 8b0688d..0e2a284 100644 --- a/tests/commands/test_fan_speed.py +++ b/tests/commands/test_fan_speed.py @@ -1,6 +1,5 @@ from deebotozmo.commands import FanSpeedLevel - -from .helpers import verify_DisplayNameEnum_unique +from tests.helpers import verify_DisplayNameEnum_unique def test_FanSpeedLevel_unique(): diff --git a/tests/commands/test_water_info.py b/tests/commands/test_water_info.py deleted file mode 100644 index c2f913b..0000000 --- a/tests/commands/test_water_info.py +++ /dev/null @@ -1,10 +0,0 @@ -from typing import Set - -from deebotozmo.commands import WaterLevel - -from .helpers import verify_DisplayNameEnum_unique - - -def test_WaterLevel_unique(): - verify_DisplayNameEnum_unique(WaterLevel) - diff --git a/tests/events/test_water_info.py b/tests/events/test_water_info.py new file mode 100644 index 0000000..aab8c78 --- /dev/null +++ b/tests/events/test_water_info.py @@ -0,0 +1,7 @@ +from deebotozmo.events import WaterAmount +from tests.helpers import verify_DisplayNameEnum_unique + + +def test_WaterLevel_unique(): + verify_DisplayNameEnum_unique(WaterAmount) + diff --git a/tests/commands/helpers.py b/tests/helpers.py similarity index 100% rename from tests/commands/helpers.py rename to tests/helpers.py