Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Orru <simone.orru@secomind.com>
  • Loading branch information
sorru94 committed Sep 22, 2023
1 parent 37555b9 commit 41827d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
7 changes: 4 additions & 3 deletions astarte/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from datetime import datetime
from collections.abc import Callable
import logging
import asyncio

from astarte.device.interface import Interface
from astarte.device.introspection import Introspection
Expand Down Expand Up @@ -69,7 +70,7 @@ class Device(ABC):
"""

@abstractmethod
def __init__(self, loop):
def __init__(self, loop: asyncio.AbstractEventLoop | None = None):
"""
Parameters
----------
Expand All @@ -85,7 +86,7 @@ def __init__(self, loop):
self.on_data_received: Callable[[Device, str, str, object], None] | None = None

@abstractmethod
def add_interface_from_json(self, interface_json: object):
def add_interface_from_json(self, interface_json: dict):
"""
Adds an interface to the device.
Expand All @@ -94,7 +95,7 @@ def add_interface_from_json(self, interface_json: object):
Parameters
----------
interface_json : object
interface_json : dict
Description of the interface obtained through `json.loads()` or similar methods.
"""

Expand Down
17 changes: 11 additions & 6 deletions astarte/device/device_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from astarte.device import crypto, pairing_handler
from astarte.device.interface import Interface
from astarte.device.database import AstarteDatabaseSQLite, AstarteDatabase
from astarte.device.exceptions import ValidationError, PersistencyDirectoryNotFoundError
from astarte.device.exceptions import ValidationError, PersistencyDirectoryNotFoundError, APIError

from astarte.device.device import Device

Expand Down Expand Up @@ -150,7 +150,7 @@ def __init__(
self.__is_connected = False
self.__ignore_ssl_errors = ignore_ssl_errors

self.on_connected: Callable[DeviceMqtt, None] | None = None
self.on_connected: Callable[[DeviceMqtt], None] | None = None
self.on_disconnected: Callable[[DeviceMqtt, int], None] | None = None

self.__setup_mqtt_client()
Expand All @@ -164,13 +164,13 @@ def __setup_mqtt_client(self) -> None:
self.__mqtt_client.on_disconnect = self.__on_disconnect
self.__mqtt_client.on_message = self.__on_message

def add_interface_from_json(self, interface_json: object):
def add_interface_from_json(self, interface_json: dict):
"""
See parent class.
Parameters
----------
interface_json : object
interface_json : dict
See parent class.
"""
self._introspection.add_interface(interface_json)
Expand Down Expand Up @@ -264,6 +264,8 @@ def connect(self) -> None:

# Grab the URL components we care about
parsed_url = urlparse(broker_url)
if (parsed_url.hostname == None) or (parsed_url.port == None):
raise APIError("Received invalid broker URL.")

Check warning on line 268 in astarte/device/device_mqtt.py

View check run for this annotation

Codecov / codecov/patch

astarte/device/device_mqtt.py#L268

Added line #L268 was not covered by tests

self.__mqtt_client.connect_async(parsed_url.hostname, parsed_url.port)
self.__mqtt_client.loop_start()
Expand Down Expand Up @@ -584,8 +586,11 @@ def __purge_server_properties(self, payload) -> None:

# Delete all the properties not in the received list.
for interface_name, _, interface_path, _ in self.__prop_database.load_all_props():
if (
self._introspection.get_interface(interface_name).is_server_owned()
interface = self._introspection.get_interface(interface_name)
if (interface == None):
logging.debug("Missing interface %s from introspection.", interface_name)
self.__prop_database.delete_prop(interface_name, interface_path)

Check warning on line 592 in astarte/device/device_mqtt.py

View check run for this annotation

Codecov / codecov/patch

astarte/device/device_mqtt.py#L591-L592

Added lines #L591 - L592 were not covered by tests
if (interface.is_server_owned()
and (interface_name, interface_path) not in allowed_properties
):
logging.debug("Removing the property at: %s/%s.", interface_name, interface_path)
Expand Down

0 comments on commit 41827d7

Please sign in to comment.