Add type hints to setup_scanner (#63825)

* Add type hints to setup_scanner

* Fix aprs tests

* Revert "Add type hints to setup_scanner"

This reverts commit 3e8b2954843ee978d356e01ae5976528c74e54dc.

* Revert "Fix aprs tests"

This reverts commit 854b37aee8394e3e59ee48122226a50b8ddcf8bb.

* Add type hints to setup_scanner

* Fix aprs tests

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2022-01-11 17:29:04 +01:00 committed by GitHub
parent 4eae888546
commit c6416955c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 14 deletions

View File

@ -1,5 +1,7 @@
"""Support for APRS device tracking.""" """Support for APRS device tracking."""
from __future__ import annotations
from collections.abc import Callable
import logging import logging
import threading import threading
@ -21,7 +23,9 @@ from homeassistant.const import (
CONF_USERNAME, CONF_USERNAME,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
) )
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify from homeassistant.util import slugify
DOMAIN = "aprs" DOMAIN = "aprs"
@ -80,15 +84,20 @@ def gps_accuracy(gps, posambiguity: int) -> int:
return accuracy return accuracy
def setup_scanner(hass, config, see, discovery_info=None): def setup_scanner(
hass: HomeAssistant,
config: ConfigType,
see: Callable[..., None],
discovery_info: DiscoveryInfoType | None = None,
) -> bool:
"""Set up the APRS tracker.""" """Set up the APRS tracker."""
callsigns = config.get(CONF_CALLSIGNS) callsigns = config[CONF_CALLSIGNS]
server_filter = make_filter(callsigns) server_filter = make_filter(callsigns)
callsign = config.get(CONF_USERNAME) callsign = config[CONF_USERNAME]
password = config.get(CONF_PASSWORD) password = config[CONF_PASSWORD]
host = config.get(CONF_HOST) host = config[CONF_HOST]
timeout = config.get(CONF_TIMEOUT) timeout = config[CONF_TIMEOUT]
aprs_listener = AprsListenerThread(callsign, password, host, server_filter, see) aprs_listener = AprsListenerThread(callsign, password, host, server_filter, see)
def aprs_disconnect(event): def aprs_disconnect(event):
@ -100,11 +109,11 @@ def setup_scanner(hass, config, see, discovery_info=None):
if not aprs_listener.start_event.wait(timeout): if not aprs_listener.start_event.wait(timeout):
_LOGGER.error("Timeout waiting for APRS to connect") _LOGGER.error("Timeout waiting for APRS to connect")
return return False
if not aprs_listener.start_success: if not aprs_listener.start_success:
_LOGGER.error(aprs_listener.start_message) _LOGGER.error(aprs_listener.start_message)
return return False
_LOGGER.debug(aprs_listener.start_message) _LOGGER.debug(aprs_listener.start_message)
return True return True

View File

@ -1,14 +1,21 @@
"""Demo platform for the Device tracker component.""" """Demo platform for the Device tracker component."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
import random import random
from homeassistant.core import ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import DOMAIN, SERVICE_RANDOMIZE_DEVICE_TRACKER_DATA from .const import DOMAIN, SERVICE_RANDOMIZE_DEVICE_TRACKER_DATA
def setup_scanner(hass, config, see, discovery_info=None): def setup_scanner(
hass: HomeAssistant,
config: ConfigType,
see: Callable[..., None],
discovery_info: DiscoveryInfoType | None = None,
) -> bool:
"""Set up the demo tracker.""" """Set up the demo tracker."""
def offset(): def offset():

View File

@ -1,4 +1,7 @@
"""Support for FleetGO Platform.""" """Support for FleetGO Platform."""
from __future__ import annotations
from collections.abc import Callable
import logging import logging
import requests import requests
@ -15,8 +18,10 @@ from homeassistant.const import (
CONF_PASSWORD, CONF_PASSWORD,
CONF_USERNAME, CONF_USERNAME,
) )
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_utc_time_change from homeassistant.helpers.event import track_utc_time_change
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -31,7 +36,12 @@ PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
) )
def setup_scanner(hass, config: dict, see, discovery_info=None): def setup_scanner(
hass: HomeAssistant,
config: ConfigType,
see: Callable[..., None],
discovery_info: DiscoveryInfoType | None = None,
) -> bool:
"""Set up the DeviceScanner and check if login is valid.""" """Set up the DeviceScanner and check if login is valid."""
scanner = FleetGoDeviceScanner(config, see) scanner = FleetGoDeviceScanner(config, see)
if not scanner.login(hass): if not scanner.login(hass):

View File

@ -1,6 +1,7 @@
"""Support for Google Maps location sharing.""" """Support for Google Maps location sharing."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from datetime import timedelta from datetime import timedelta
import logging import logging
@ -19,9 +20,10 @@ from homeassistant.const import (
CONF_SCAN_INTERVAL, CONF_SCAN_INTERVAL,
CONF_USERNAME, CONF_USERNAME,
) )
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_time_interval from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util, slugify from homeassistant.util import dt as dt_util, slugify
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -45,7 +47,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA_BASE.extend(
) )
def setup_scanner(hass, config: ConfigType, see, discovery_info=None): def setup_scanner(
hass: HomeAssistant,
config: ConfigType,
see: Callable[..., None],
discovery_info: DiscoveryInfoType | None = None,
) -> bool:
"""Set up the Google Maps Location sharing scanner.""" """Set up the Google Maps Location sharing scanner."""
scanner = GoogleMapsScanner(hass, config, see) scanner = GoogleMapsScanner(hass, config, see)
return scanner.success_init return scanner.success_init

View File

@ -1,4 +1,7 @@
"""Support for Life360 device tracking.""" """Support for Life360 device tracking."""
from __future__ import annotations
from collections.abc import Callable
from datetime import timedelta from datetime import timedelta
import logging import logging
@ -20,8 +23,10 @@ from homeassistant.const import (
LENGTH_MILES, LENGTH_MILES,
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_time_interval from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.async_ import run_callback_threadsafe from homeassistant.util.async_ import run_callback_threadsafe
from homeassistant.util.distance import convert from homeassistant.util.distance import convert
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -86,7 +91,12 @@ def _dump_filter(filter_dict, desc, func=lambda x: x):
) )
def setup_scanner(hass, config, see, discovery_info=None): def setup_scanner(
hass: HomeAssistant,
config: ConfigType,
see: Callable[..., None],
discovery_info: DiscoveryInfoType | None = None,
) -> bool:
"""Set up device scanner.""" """Set up device scanner."""
config = hass.data[DOMAIN]["config"] config = hass.data[DOMAIN]["config"]
apis = hass.data[DOMAIN]["apis"] apis = hass.data[DOMAIN]["apis"]

View File

@ -312,6 +312,7 @@ def test_setup_scanner():
"password": TEST_PASSWORD, "password": TEST_PASSWORD,
"host": TEST_HOST, "host": TEST_HOST,
"callsigns": ["XX0FOO*", "YY0BAR-1"], "callsigns": ["XX0FOO*", "YY0BAR-1"],
"timeout": device_tracker.DEFAULT_TIMEOUT,
} }
see = Mock() see = Mock()