Skip to content

Commit

Permalink
refactor: rename module switchbot_mqtt/_actors/{_base->base} (cause…
Browse files Browse the repository at this point in the history
… accessed by outer `switchbot_mqtt` & `switchbot_mqtt._cli`)
  • Loading branch information
fphammerle committed Apr 3, 2022
1 parent 4416a97 commit 680dc5e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion switchbot_mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import paho.mqtt.client

from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
from switchbot_mqtt._actors._base import _MQTTCallbackUserdata
from switchbot_mqtt._actors.base import _MQTTCallbackUserdata

_LOGGER = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion switchbot_mqtt/_actors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import paho.mqtt.client
import switchbot

from switchbot_mqtt._actors._base import _MQTTCallbackUserdata, _MQTTControlledActor
from switchbot_mqtt._actors.base import _MQTTCallbackUserdata, _MQTTControlledActor
from switchbot_mqtt._utils import (
_join_mqtt_topic_levels,
_MQTTTopicLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

# > Even with __all__ set appropriately, internal interfaces (packages,
# > modules, classes, functions, attributes or other names) should still be
# > prefixed with a single leading underscore. An interface is also considered
# > internal if any containing namespace (package, module or class) is
# > considered internal.
# https://peps.python.org/pep-0008/#public-and-internal-interfaces

from __future__ import annotations # PEP563 (default in python>=3.10)

import abc
Expand Down
17 changes: 6 additions & 11 deletions switchbot_mqtt/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import switchbot

import switchbot_mqtt
import switchbot_mqtt._actors._base # rename {_->}base ?
from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
from switchbot_mqtt._actors.base import _MQTTCallbackUserdata

_MQTT_DEFAULT_PORT = 1883
_MQTT_DEFAULT_TLS_PORT = 8883
Expand Down Expand Up @@ -81,35 +81,30 @@ def _main() -> None:
help="Maximum number of attempts to send a command to a SwitchBot device"
" (default: %(default)d)",
)
_MQTTCallbackUserdata = (
switchbot_mqtt._actors._base._MQTTCallbackUserdata # pylint: disable=protected-access; internal module & class
)
mqtt_topic_prefix = _MQTTCallbackUserdata.mqtt_topic_prefix
argparser.add_argument(
"--fetch-device-info",
action="store_true",
help="Report devices' battery level on topic "
# pylint: disable=protected-access; internal
+ _ButtonAutomator.get_mqtt_battery_percentage_topic(
prefix=mqtt_topic_prefix, mac_address="MAC_ADDRESS"
prefix=_MQTTCallbackUserdata.mqtt_topic_prefix, mac_address="MAC_ADDRESS"
)
+ " or, respectively,"
+ _CurtainMotor.get_mqtt_battery_percentage_topic(
prefix=mqtt_topic_prefix, mac_address="MAC_ADDRESS"
prefix=_MQTTCallbackUserdata.mqtt_topic_prefix, mac_address="MAC_ADDRESS"
)
+ " after every command. Additionally report curtain motors' position on topic "
+ _CurtainMotor.get_mqtt_position_topic(
prefix=mqtt_topic_prefix, mac_address="MAC_ADDRESS"
prefix=_MQTTCallbackUserdata.mqtt_topic_prefix, mac_address="MAC_ADDRESS"
)
+ " after executing stop commands."
" When this option is enabled, the mentioned reports may also be requested"
" by sending a MQTT message to the topic "
+ _ButtonAutomator.get_mqtt_update_device_info_topic(
prefix=mqtt_topic_prefix, mac_address="MAC_ADDRESS"
prefix=_MQTTCallbackUserdata.mqtt_topic_prefix, mac_address="MAC_ADDRESS"
)
+ " or "
+ _CurtainMotor.get_mqtt_update_device_info_topic(
prefix=mqtt_topic_prefix, mac_address="MAC_ADDRESS"
prefix=_MQTTCallbackUserdata.mqtt_topic_prefix, mac_address="MAC_ADDRESS"
)
+ ". This option can also be enabled by assigning a non-empty value to the"
" environment variable FETCH_DEVICE_INFO.",
Expand Down
6 changes: 3 additions & 3 deletions tests/test_actor_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
import pytest
import switchbot

import switchbot_mqtt._actors._base
import switchbot_mqtt._actors.base

# pylint: disable=protected-access


def test_abstract() -> None:
with pytest.raises(TypeError, match=r"\babstract class\b"):
# pylint: disable=abstract-class-instantiated
switchbot_mqtt._actors._base._MQTTControlledActor( # type: ignore
switchbot_mqtt._actors.base._MQTTControlledActor( # type: ignore
mac_address="dummy", retry_count=21, password=None
)


def test_execute_command_abstract() -> None:
class _ActorMock(switchbot_mqtt._actors._base._MQTTControlledActor):
class _ActorMock(switchbot_mqtt._actors.base._MQTTControlledActor):
def __init__(
self, mac_address: str, retry_count: int, password: typing.Optional[str]
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_actor_base_device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import pytest

from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
from switchbot_mqtt._actors._base import _MQTTControlledActor
from switchbot_mqtt._actors.base import _MQTTControlledActor

# pylint: disable=protected-access

Expand Down
30 changes: 15 additions & 15 deletions tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import switchbot_mqtt
import switchbot_mqtt._actors
from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
from switchbot_mqtt._actors._base import _MQTTCallbackUserdata, _MQTTControlledActor
from switchbot_mqtt._actors.base import _MQTTCallbackUserdata, _MQTTControlledActor
from switchbot_mqtt._utils import _MQTTTopicLevel, _MQTTTopicPlaceholder

# pylint: disable=protected-access
Expand Down Expand Up @@ -116,12 +116,12 @@ def test__run(
]
assert len(caplog.record_tuples) == (7 if fetch_device_info else 5)
assert (
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.INFO,
"subscribing to MQTT topic 'homeassistant/switch/switchbot/+/set'",
) in caplog.record_tuples
assert (
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.INFO,
"subscribing to MQTT topic 'homeassistant/cover/switchbot-curtain/+/set'",
) in caplog.record_tuples
Expand Down Expand Up @@ -281,7 +281,7 @@ def test__mqtt_update_device_info_callback(
)
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.DEBUG,
f"received topic={topic.decode()} payload={payload!r}",
)
Expand Down Expand Up @@ -315,11 +315,11 @@ def test__mqtt_update_device_info_callback_ignore_retained(
execute_command_mock.assert_not_called()
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.DEBUG,
"received topic=aa:bb:cc:dd:ee:ff/request payload=b''",
),
("switchbot_mqtt._actors._base", logging.INFO, "ignoring retained message"),
("switchbot_mqtt._actors.base", logging.INFO, "ignoring retained message"),
]


Expand Down Expand Up @@ -423,7 +423,7 @@ def test__mqtt_command_callback(
)
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.DEBUG,
f"received topic={topic.decode()} payload={payload!r}",
)
Expand Down Expand Up @@ -507,12 +507,12 @@ def test__mqtt_command_callback_unexpected_topic(
execute_command_mock.assert_not_called()
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.DEBUG,
f"received topic={topic.decode()} payload={payload!r}",
),
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.WARNING,
f"unexpected topic {topic.decode()}",
),
Expand Down Expand Up @@ -547,12 +547,12 @@ def test__mqtt_command_callback_invalid_mac_address(
execute_command_mock.assert_not_called()
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.DEBUG,
f"received topic={topic.decode()} payload={payload!r}",
),
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.WARNING,
f"invalid mac address {mac_address}",
),
Expand Down Expand Up @@ -590,11 +590,11 @@ def test__mqtt_command_callback_ignore_retained(
execute_command_mock.assert_not_called()
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.DEBUG,
f"received topic={topic.decode()} payload={payload!r}",
),
("switchbot_mqtt._actors._base", logging.INFO, "ignoring retained message"),
("switchbot_mqtt._actors.base", logging.INFO, "ignoring retained message"),
]


Expand Down Expand Up @@ -664,7 +664,7 @@ def _get_device(self) -> None:
topic=expected_topic, payload=state, retain=True
)
assert caplog.record_tuples[0] == (
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.DEBUG,
f"publishing topic={expected_topic} payload={state!r}",
)
Expand All @@ -673,7 +673,7 @@ def _get_device(self) -> None:
else:
assert caplog.record_tuples[1:] == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.ERROR,
f"Failed to publish MQTT message on topic {expected_topic} (rc={return_code})",
)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_switchbot_curtain_motor_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from paho.mqtt.client import MQTTMessage

from switchbot_mqtt._actors import _CurtainMotor
from switchbot_mqtt._actors._base import _MQTTCallbackUserdata
from switchbot_mqtt._actors.base import _MQTTCallbackUserdata

# pylint: disable=protected-access

Expand Down Expand Up @@ -147,7 +147,7 @@ def test__mqtt_set_position_callback_unexpected_topic(
device_init_mock.assert_not_called()
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.WARN,
"unexpected topic switchbot-curtain/aa:bb:cc:dd:ee:ff/position/set",
),
Expand Down Expand Up @@ -175,7 +175,7 @@ def test__mqtt_set_position_callback_invalid_mac_address(
device_init_mock.assert_not_called()
assert caplog.record_tuples == [
(
"switchbot_mqtt._actors._base",
"switchbot_mqtt._actors.base",
logging.WARN,
"invalid mac address aa:bb:cc:dd:ee",
),
Expand Down

0 comments on commit 680dc5e

Please sign in to comment.