From b318b9b1967303fb3aef86a8ca2460594ae86cd2 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 20 Jun 2022 12:07:33 +0200 Subject: [PATCH] Improve onvif type hints (#73642) * Remove onvif from mypy ignore list * Adjust parsers * Adjust event * Adjust config_flow --- homeassistant/components/onvif/config_flow.py | 3 +- homeassistant/components/onvif/event.py | 4 +- homeassistant/components/onvif/models.py | 18 ++++---- homeassistant/components/onvif/parsers.py | 42 ++++++++++--------- mypy.ini | 15 ------- script/hassfest/mypy_config.py | 5 --- 6 files changed, 35 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/onvif/config_flow.py b/homeassistant/components/onvif/config_flow.py index d7fb46079d9..48e5163ced5 100644 --- a/homeassistant/components/onvif/config_flow.py +++ b/homeassistant/components/onvif/config_flow.py @@ -2,6 +2,7 @@ from __future__ import annotations from pprint import pformat +from typing import Any from urllib.parse import urlparse from onvif.exceptions import ONVIFError @@ -44,7 +45,7 @@ def wsdiscovery() -> list[Service]: return services -async def async_discovery(hass) -> bool: +async def async_discovery(hass) -> list[dict[str, Any]]: """Return if there are devices that can be discovered.""" LOGGER.debug("Starting ONVIF discovery") services = await hass.async_add_executor_job(wsdiscovery) diff --git a/homeassistant/components/onvif/event.py b/homeassistant/components/onvif/event.py index 3b4ae981677..3801d8081db 100644 --- a/homeassistant/components/onvif/event.py +++ b/homeassistant/components/onvif/event.py @@ -114,7 +114,7 @@ class EventManager: await self._subscription.Unsubscribe() self._subscription = None - async def async_restart(self, _now: dt = None) -> None: + async def async_restart(self, _now: dt.datetime | None = None) -> None: """Restart the subscription assuming the camera rebooted.""" if not self.started: return @@ -159,7 +159,7 @@ class EventManager: """Schedule async_pull_messages to run.""" self._unsub_refresh = async_call_later(self.hass, 1, self.async_pull_messages) - async def async_pull_messages(self, _now: dt = None) -> None: + async def async_pull_messages(self, _now: dt.datetime | None = None) -> None: """Pull messages from device.""" if self.hass.state == CoreState.running: try: diff --git a/homeassistant/components/onvif/models.py b/homeassistant/components/onvif/models.py index dea613e3c1c..6cefa6332e2 100644 --- a/homeassistant/components/onvif/models.py +++ b/homeassistant/components/onvif/models.py @@ -11,11 +11,11 @@ from homeassistant.helpers.entity import EntityCategory class DeviceInfo: """Represent device information.""" - manufacturer: str = None - model: str = None - fw_version: str = None - serial_number: str = None - mac: str = None + manufacturer: str | None = None + model: str | None = None + fw_version: str | None = None + serial_number: str | None = None + mac: str | None = None @dataclass @@ -41,7 +41,7 @@ class PTZ: continuous: bool relative: bool absolute: bool - presets: list[str] = None + presets: list[str] | None = None @dataclass @@ -52,7 +52,7 @@ class Profile: token: str name: str video: Video - ptz: PTZ = None + ptz: PTZ | None = None @dataclass @@ -71,8 +71,8 @@ class Event: uid: str name: str platform: str - device_class: str = None - unit_of_measurement: str = None + device_class: str | None = None + unit_of_measurement: str | None = None value: Any = None entity_category: EntityCategory | None = None entity_enabled: bool = True diff --git a/homeassistant/components/onvif/parsers.py b/homeassistant/components/onvif/parsers.py index 87b901d2c52..2c74f873f77 100644 --- a/homeassistant/components/onvif/parsers.py +++ b/homeassistant/components/onvif/parsers.py @@ -11,7 +11,9 @@ from homeassistant.util.decorator import Registry from .models import Event -PARSERS: Registry[str, Callable[[str, Any], Coroutine[Any, Any, Event]]] = Registry() +PARSERS: Registry[ + str, Callable[[str, Any], Coroutine[Any, Any, Event | None]] +] = Registry() def local_datetime_or_none(value: str) -> datetime.datetime | None: @@ -28,7 +30,7 @@ def local_datetime_or_none(value: str) -> datetime.datetime | None: @PARSERS.register("tns1:VideoSource/MotionAlarm") # pylint: disable=protected-access -async def async_parse_motion_alarm(uid: str, msg) -> Event: +async def async_parse_motion_alarm(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:VideoSource/MotionAlarm @@ -51,7 +53,7 @@ async def async_parse_motion_alarm(uid: str, msg) -> Event: @PARSERS.register("tns1:VideoSource/ImageTooBlurry/ImagingService") @PARSERS.register("tns1:VideoSource/ImageTooBlurry/RecordingService") # pylint: disable=protected-access -async def async_parse_image_too_blurry(uid: str, msg) -> Event: +async def async_parse_image_too_blurry(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:VideoSource/ImageTooBlurry/* @@ -75,7 +77,7 @@ async def async_parse_image_too_blurry(uid: str, msg) -> Event: @PARSERS.register("tns1:VideoSource/ImageTooDark/ImagingService") @PARSERS.register("tns1:VideoSource/ImageTooDark/RecordingService") # pylint: disable=protected-access -async def async_parse_image_too_dark(uid: str, msg) -> Event: +async def async_parse_image_too_dark(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:VideoSource/ImageTooDark/* @@ -99,7 +101,7 @@ async def async_parse_image_too_dark(uid: str, msg) -> Event: @PARSERS.register("tns1:VideoSource/ImageTooBright/ImagingService") @PARSERS.register("tns1:VideoSource/ImageTooBright/RecordingService") # pylint: disable=protected-access -async def async_parse_image_too_bright(uid: str, msg) -> Event: +async def async_parse_image_too_bright(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:VideoSource/ImageTooBright/* @@ -123,7 +125,7 @@ async def async_parse_image_too_bright(uid: str, msg) -> Event: @PARSERS.register("tns1:VideoSource/GlobalSceneChange/ImagingService") @PARSERS.register("tns1:VideoSource/GlobalSceneChange/RecordingService") # pylint: disable=protected-access -async def async_parse_scene_change(uid: str, msg) -> Event: +async def async_parse_scene_change(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:VideoSource/GlobalSceneChange/* @@ -144,7 +146,7 @@ async def async_parse_scene_change(uid: str, msg) -> Event: @PARSERS.register("tns1:AudioAnalytics/Audio/DetectedSound") # pylint: disable=protected-access -async def async_parse_detected_sound(uid: str, msg) -> Event: +async def async_parse_detected_sound(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:AudioAnalytics/Audio/DetectedSound @@ -175,7 +177,7 @@ async def async_parse_detected_sound(uid: str, msg) -> Event: @PARSERS.register("tns1:RuleEngine/FieldDetector/ObjectsInside") # pylint: disable=protected-access -async def async_parse_field_detector(uid: str, msg) -> Event: +async def async_parse_field_detector(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:RuleEngine/FieldDetector/ObjectsInside @@ -207,7 +209,7 @@ async def async_parse_field_detector(uid: str, msg) -> Event: @PARSERS.register("tns1:RuleEngine/CellMotionDetector/Motion") # pylint: disable=protected-access -async def async_parse_cell_motion_detector(uid: str, msg) -> Event: +async def async_parse_cell_motion_detector(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:RuleEngine/CellMotionDetector/Motion @@ -238,7 +240,7 @@ async def async_parse_cell_motion_detector(uid: str, msg) -> Event: @PARSERS.register("tns1:RuleEngine/MotionRegionDetector/Motion") # pylint: disable=protected-access -async def async_parse_motion_region_detector(uid: str, msg) -> Event: +async def async_parse_motion_region_detector(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:RuleEngine/MotionRegionDetector/Motion @@ -269,7 +271,7 @@ async def async_parse_motion_region_detector(uid: str, msg) -> Event: @PARSERS.register("tns1:RuleEngine/TamperDetector/Tamper") # pylint: disable=protected-access -async def async_parse_tamper_detector(uid: str, msg) -> Event: +async def async_parse_tamper_detector(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:RuleEngine/TamperDetector/Tamper @@ -301,7 +303,7 @@ async def async_parse_tamper_detector(uid: str, msg) -> Event: @PARSERS.register("tns1:Device/Trigger/DigitalInput") # pylint: disable=protected-access -async def async_parse_digital_input(uid: str, msg) -> Event: +async def async_parse_digital_input(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Device/Trigger/DigitalInput @@ -322,7 +324,7 @@ async def async_parse_digital_input(uid: str, msg) -> Event: @PARSERS.register("tns1:Device/Trigger/Relay") # pylint: disable=protected-access -async def async_parse_relay(uid: str, msg) -> Event: +async def async_parse_relay(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Device/Trigger/Relay @@ -343,7 +345,7 @@ async def async_parse_relay(uid: str, msg) -> Event: @PARSERS.register("tns1:Device/HardwareFailure/StorageFailure") # pylint: disable=protected-access -async def async_parse_storage_failure(uid: str, msg) -> Event: +async def async_parse_storage_failure(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Device/HardwareFailure/StorageFailure @@ -365,7 +367,7 @@ async def async_parse_storage_failure(uid: str, msg) -> Event: @PARSERS.register("tns1:Monitoring/ProcessorUsage") # pylint: disable=protected-access -async def async_parse_processor_usage(uid: str, msg) -> Event: +async def async_parse_processor_usage(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Monitoring/ProcessorUsage @@ -390,7 +392,7 @@ async def async_parse_processor_usage(uid: str, msg) -> Event: @PARSERS.register("tns1:Monitoring/OperatingTime/LastReboot") # pylint: disable=protected-access -async def async_parse_last_reboot(uid: str, msg) -> Event: +async def async_parse_last_reboot(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Monitoring/OperatingTime/LastReboot @@ -414,7 +416,7 @@ async def async_parse_last_reboot(uid: str, msg) -> Event: @PARSERS.register("tns1:Monitoring/OperatingTime/LastReset") # pylint: disable=protected-access -async def async_parse_last_reset(uid: str, msg) -> Event: +async def async_parse_last_reset(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Monitoring/OperatingTime/LastReset @@ -439,7 +441,7 @@ async def async_parse_last_reset(uid: str, msg) -> Event: @PARSERS.register("tns1:Monitoring/Backup/Last") # pylint: disable=protected-access -async def async_parse_backup_last(uid: str, msg) -> Event: +async def async_parse_backup_last(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Monitoring/Backup/Last @@ -465,7 +467,7 @@ async def async_parse_backup_last(uid: str, msg) -> Event: @PARSERS.register("tns1:Monitoring/OperatingTime/LastClockSynchronization") # pylint: disable=protected-access -async def async_parse_last_clock_sync(uid: str, msg) -> Event: +async def async_parse_last_clock_sync(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:Monitoring/OperatingTime/LastClockSynchronization @@ -490,7 +492,7 @@ async def async_parse_last_clock_sync(uid: str, msg) -> Event: @PARSERS.register("tns1:RecordingConfig/JobState") # pylint: disable=protected-access -async def async_parse_jobstate(uid: str, msg) -> Event: +async def async_parse_jobstate(uid: str, msg) -> Event | None: """Handle parsing event message. Topic: tns1:RecordingConfig/JobState diff --git a/mypy.ini b/mypy.ini index dcbb2839b57..c4561dea5a8 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2814,27 +2814,12 @@ ignore_errors = true [mypy-homeassistant.components.onvif.binary_sensor] ignore_errors = true -[mypy-homeassistant.components.onvif.button] -ignore_errors = true - [mypy-homeassistant.components.onvif.camera] ignore_errors = true -[mypy-homeassistant.components.onvif.config_flow] -ignore_errors = true - [mypy-homeassistant.components.onvif.device] ignore_errors = true -[mypy-homeassistant.components.onvif.event] -ignore_errors = true - -[mypy-homeassistant.components.onvif.models] -ignore_errors = true - -[mypy-homeassistant.components.onvif.parsers] -ignore_errors = true - [mypy-homeassistant.components.onvif.sensor] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 9de6e9c6ad8..6b42bcd2cdd 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -87,13 +87,8 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.omnilogic.switch", "homeassistant.components.onvif.base", "homeassistant.components.onvif.binary_sensor", - "homeassistant.components.onvif.button", "homeassistant.components.onvif.camera", - "homeassistant.components.onvif.config_flow", "homeassistant.components.onvif.device", - "homeassistant.components.onvif.event", - "homeassistant.components.onvif.models", - "homeassistant.components.onvif.parsers", "homeassistant.components.onvif.sensor", "homeassistant.components.philips_js", "homeassistant.components.philips_js.config_flow",