Skip to content

Commit a22d663

Browse files
committed
mqtt: extract api into a separate package
1 parent 6b1b83c commit a22d663

File tree

10 files changed

+28
-27
lines changed

10 files changed

+28
-27
lines changed

enapter/mqtt/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1+
from . import api
12
from .client import Client
2-
from .command import CommandRequest, CommandResponse, CommandState
33
from .config import Config
4-
from .device_channel import DeviceChannel, DeviceLogSeverity
54

65
__all__ = [
6+
"api",
77
"Client",
8-
"CommandRequest",
9-
"CommandResponse",
10-
"CommandState",
118
"Config",
12-
"DeviceChannel",
13-
"DeviceLogSeverity",
149
]

enapter/mqtt/api/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .command import CommandRequest, CommandResponse, CommandState
2+
from .device_channel import DeviceChannel, DeviceLogSeverity
3+
4+
__all__ = [
5+
"CommandRequest",
6+
"CommandResponse",
7+
"CommandState",
8+
"DeviceChannel",
9+
"DeviceLogSeverity",
10+
]
File renamed without changes.
File renamed without changes.

enapter/mqtt/client.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import enapter
1111

12-
from .device_channel import DeviceChannel
13-
1412
LOGGER = logging.getLogger(__name__)
1513

1614

@@ -32,11 +30,6 @@ def _new_logger(config):
3230
def config(self):
3331
return self._config
3432

35-
def device_channel(self, hardware_id, channel_id):
36-
return DeviceChannel(
37-
client=self, hardware_id=hardware_id, channel_id=channel_id
38-
)
39-
4033
async def publish(self, *args, **kwargs):
4134
client = await self._wait_client()
4235
await client.publish(*args, **kwargs)

enapter/vucm/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def _run(self):
3636

3737
device = await self._stack.enter_async_context(
3838
self._device_factory(
39-
channel=mqtt_client.device_channel(
39+
channel=enapter.mqtt.api.DeviceChannel(
4040
hardware_id=self._config.hardware_id,
4141
channel_id=self._config.channel_id,
4242
)

enapter/vucm/device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ async def __execute_command(self, req):
109109
try:
110110
cmd = getattr(self, self.__cmd_prefix + req.name)
111111
except AttributeError:
112-
return enapter.mqtt.CommandState.ERROR, {"reason": "unknown command"}
112+
return enapter.mqtt.api.CommandState.ERROR, {"reason": "unknown command"}
113113

114114
try:
115-
return enapter.mqtt.CommandState.COMPLETED, await cmd(**req.args)
115+
return enapter.mqtt.api.CommandState.COMPLETED, await cmd(**req.args)
116116
except:
117-
return enapter.mqtt.CommandState.ERROR, {
117+
return enapter.mqtt.api.CommandState.ERROR, {
118118
"traceback": traceback.format_exc()
119119
}

enapter/vucm/logger.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,32 @@ def _new_logger(hardware_id, channel_id):
1818
async def debug(self, msg: str, persist: bool = False):
1919
self._logger.debug(msg)
2020
await self.log(
21-
msg, severity=enapter.mqtt.DeviceLogSeverity.DEBUG, persist=persist
21+
msg, severity=enapter.mqtt.api.DeviceLogSeverity.DEBUG, persist=persist
2222
)
2323

2424
async def info(self, msg: str, persist: bool = False):
2525
self._logger.info(msg)
2626
await self.log(
27-
msg, severity=enapter.mqtt.DeviceLogSeverity.INFO, persist=persist
27+
msg, severity=enapter.mqtt.api.DeviceLogSeverity.INFO, persist=persist
2828
)
2929

3030
async def warning(self, msg: str, persist: bool = False):
3131
self._logger.warning(msg)
3232
await self.log(
33-
msg, severity=enapter.mqtt.DeviceLogSeverity.WARNING, persist=persist
33+
msg, severity=enapter.mqtt.api.DeviceLogSeverity.WARNING, persist=persist
3434
)
3535

3636
async def error(self, msg: str, persist: bool = False):
3737
self._logger.error(msg)
3838
await self.log(
39-
msg, severity=enapter.mqtt.DeviceLogSeverity.ERROR, persist=persist
39+
msg, severity=enapter.mqtt.api.DeviceLogSeverity.ERROR, persist=persist
4040
)
4141

4242
async def log(
43-
self, msg: str, severity: enapter.mqtt.DeviceLogSeverity, persist: bool = False
43+
self,
44+
msg: str,
45+
severity: enapter.mqtt.api.DeviceLogSeverity,
46+
persist: bool = False,
4447
):
4548
await self._channel.publish_logs(msg=msg, severity=severity, persist=persist)
4649

tests/unit/test_mqtt/__init__.py

Whitespace-only changes.

tests/unit/test_mqtt.py renamed to tests/unit/test_mqtt/test_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async def test_publish_telemetry(self, fake):
99
channel_id = fake.channel_id()
1010
timestamp = fake.timestamp()
1111
mock_client = mock.Mock()
12-
device_channel = enapter.mqtt.DeviceChannel(
12+
device_channel = enapter.mqtt.api.DeviceChannel(
1313
client=mock_client, hardware_id=hardware_id, channel_id=channel_id
1414
)
1515
await device_channel.publish_telemetry({"timestamp": timestamp})
@@ -23,7 +23,7 @@ async def test_publish_telemetry_without_timestamp(self, fake):
2323
channel_id = fake.channel_id()
2424
timestamp = fake.timestamp()
2525
mock_client = mock.Mock()
26-
device_channel = enapter.mqtt.DeviceChannel(
26+
device_channel = enapter.mqtt.api.DeviceChannel(
2727
client=mock_client, hardware_id=hardware_id, channel_id=channel_id
2828
)
2929
with mock.patch("time.time") as mock_time:
@@ -39,7 +39,7 @@ async def test_publish_properties(self, fake):
3939
channel_id = fake.channel_id()
4040
timestamp = fake.timestamp()
4141
mock_client = mock.Mock()
42-
device_channel = enapter.mqtt.DeviceChannel(
42+
device_channel = enapter.mqtt.api.DeviceChannel(
4343
client=mock_client, hardware_id=hardware_id, channel_id=channel_id
4444
)
4545
await device_channel.publish_properties({"timestamp": timestamp})
@@ -53,7 +53,7 @@ async def test_publish_properties_without_timestamp(self, fake):
5353
channel_id = fake.channel_id()
5454
timestamp = fake.timestamp()
5555
mock_client = mock.Mock()
56-
device_channel = enapter.mqtt.DeviceChannel(
56+
device_channel = enapter.mqtt.api.DeviceChannel(
5757
client=mock_client, hardware_id=hardware_id, channel_id=channel_id
5858
)
5959
with mock.patch("time.time") as mock_time:

0 commit comments

Comments
 (0)