From ff245638d737c05e2849ae757629a1015549270a Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Tue, 17 Oct 2023 19:54:43 -0700 Subject: [PATCH] Upgrade pylint/pylint-test and address variety of pylint issues. --- custom_components/frigate/__init__.py | 13 +++-- custom_components/frigate/camera.py | 2 +- custom_components/frigate/config_flow.py | 2 +- custom_components/frigate/const.py | 4 +- custom_components/frigate/diagnostics.py | 8 +-- custom_components/frigate/media_source.py | 59 +++++++++-------------- custom_components/frigate/update.py | 2 +- pyproject.toml | 7 ++- requirements_dev.txt | 2 +- 9 files changed, 43 insertions(+), 56 deletions(-) diff --git a/custom_components/frigate/__init__.py b/custom_components/frigate/__init__.py index 88011612..585658e3 100644 --- a/custom_components/frigate/__init__.py +++ b/custom_components/frigate/__init__.py @@ -6,10 +6,11 @@ """ from __future__ import annotations +from collections.abc import Callable from datetime import timedelta import logging import re -from typing import Any, Callable, Final +from typing import Any, Final from awesomeversion import AwesomeVersion @@ -70,8 +71,7 @@ def get_frigate_device_identifier( """Get a device identifier.""" if camera_name: return (DOMAIN, f"{entry.entry_id}:{slugify(camera_name)}") - else: - return (DOMAIN, entry.entry_id) + return (DOMAIN, entry.entry_id) def get_frigate_entity_unique_id( @@ -155,10 +155,9 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool: """Set up this integration using YAML is not supported.""" integration = await async_get_integration(hass, DOMAIN) _LOGGER.info( - STARTUP_MESSAGE.format( - title=NAME, - integration_version=integration.version, - ) + STARTUP_MESSAGE, + NAME, + integration.version, ) hass.data.setdefault(DOMAIN, {}) diff --git a/custom_components/frigate/camera.py b/custom_components/frigate/camera.py index ef05a6e0..f82fa5d1 100644 --- a/custom_components/frigate/camera.py +++ b/custom_components/frigate/camera.py @@ -451,7 +451,7 @@ async def async_camera_image( return self._last_image @property - def state(self) -> str: + def state(self) -> str: # pylint: disable=overridden-final-method """Return the camera state.""" if self._last_image is None: return STATE_IDLE diff --git a/custom_components/frigate/config_flow.py b/custom_components/frigate/config_flow.py index e26c3a7f..761654a1 100644 --- a/custom_components/frigate/config_flow.py +++ b/custom_components/frigate/config_flow.py @@ -83,7 +83,7 @@ def _show_config_form( self, user_input: dict[str, Any] | None = None, errors: dict[str, Any] | None = None, - ) -> dict[str, Any]: # pylint: disable=unused-argument + ) -> dict[str, Any]: """Show the configuration form.""" if user_input is None: user_input = {} diff --git a/custom_components/frigate/const.py b/custom_components/frigate/const.py index 568793d9..f6bcb51c 100644 --- a/custom_components/frigate/const.py +++ b/custom_components/frigate/const.py @@ -50,8 +50,8 @@ STARTUP_MESSAGE = """ ------------------------------------------------------------------- -{title} -Integration Version: {integration_version} +%s +Integration Version: %s This is a custom integration! If you have any issues with this you need to open an issue here: https://github.com/blakeblackshear/frigate-hass-integration/issues diff --git a/custom_components/frigate/diagnostics.py b/custom_components/frigate/diagnostics.py index 8d6d315a..1aea4768 100644 --- a/custom_components/frigate/diagnostics.py +++ b/custom_components/frigate/diagnostics.py @@ -1,6 +1,8 @@ """Diagnostics support for Frigate.""" -from typing import Any, Dict +from __future__ import annotations + +from typing import Any from homeassistant.components.diagnostics import async_redact_data from homeassistant.config_entries import ConfigEntry @@ -11,7 +13,7 @@ REDACT_CONFIG = {CONF_PASSWORD, CONF_PATH} -def get_redacted_data(data: Dict[str, Any]) -> Any: +def get_redacted_data(data: dict[str, Any]) -> Any: """Redact sensitive vales from data.""" return async_redact_data(data, REDACT_CONFIG) @@ -19,7 +21,7 @@ def get_redacted_data(data: Dict[str, Any]) -> Any: async def async_get_config_entry_diagnostics( hass: HomeAssistant, entry: ConfigEntry, -) -> Dict[str, Any]: +) -> dict[str, Any]: """Return diagnostics for a config entry.""" config = hass.data[DOMAIN][entry.entry_id][ATTR_CONFIG] diff --git a/custom_components/frigate/media_source.py b/custom_components/frigate/media_source.py index a73e4dc1..b7573afe 100644 --- a/custom_components/frigate/media_source.py +++ b/custom_components/frigate/media_source.py @@ -131,11 +131,11 @@ def get_integration_proxy_path(self, timezone: str) -> str: @classmethod def _add_frigate_instance_id_to_parts_if_absent( - self, parts: list[str], default_frigate_instance_id: str | None = None + cls, parts: list[str], default_frigate_instance_id: str | None = None ) -> list[str]: """Add a frigate instance id if it's not specified.""" if ( - self._get_index(parts, 0) == self.get_identifier_type() + cls._get_index(parts, 0) == cls.get_identifier_type() and default_frigate_instance_id is not None ): parts.insert(0, default_frigate_instance_id) @@ -168,32 +168,28 @@ def mime_type(self) -> str: """Get mime type for this frigate media type.""" if self == FrigateMediaType.CLIPS: return "application/x-mpegURL" - else: - return "image/jpg" + return "image/jpg" @property def media_type(self) -> str: """Get media type for this frigate media type.""" if self == FrigateMediaType.CLIPS: return str(MEDIA_TYPE_VIDEO) - else: - return str(MEDIA_TYPE_IMAGE) + return str(MEDIA_TYPE_IMAGE) @property def media_class(self) -> str: """Get media class for this frigate media type.""" if self == FrigateMediaType.CLIPS: return str(MEDIA_CLASS_VIDEO) - else: - return str(MEDIA_CLASS_IMAGE) + return str(MEDIA_CLASS_IMAGE) @property def extension(self) -> str: """Get filename extension.""" if self == FrigateMediaType.CLIPS: return "m3u8" - else: - return "jpg" + return "jpg" @attr.s(frozen=True) @@ -255,8 +251,7 @@ def get_integration_proxy_path(self, timezone: str) -> str: """Get the equivalent Frigate server path.""" if self.frigate_media_type == FrigateMediaType.CLIPS: return f"vod/event/{self.id}/index.{self.frigate_media_type.extension}" - else: - return f"snapshot/{self.id}" + return f"snapshot/{self.id}" @property def mime_type(self) -> str: @@ -375,7 +370,7 @@ def _validate_year_month_day( try: dt.datetime.strptime(data, "%Y-%m-%d") except ValueError as exc: - raise ValueError("Invalid date in identifier: %s" % data) from exc + raise ValueError(f"Invalid date in identifier: {data}") from exc def _validate_hour( @@ -383,7 +378,7 @@ def _validate_hour( ) -> None: """Determine if a value is a valid hour.""" if value is not None and (int(value) < 0 or int(value) > 23): - raise ValueError("Invalid hour in identifier: %s" % value) + raise ValueError(f"Invalid hour in identifier: {value}") @attr.s(frozen=True) @@ -554,8 +549,8 @@ def _get_client(self, identifier: Identifier) -> FrigateApiClient: return client raise MediaSourceError( - "Could not find client for frigate instance id: %s" - % identifier.frigate_instance_id + "Could not find client for frigate instance " + f"id: {identifier.frigate_instance_id}" ) def _get_default_frigate_instance_id(self) -> str | None: @@ -584,7 +579,7 @@ async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia: f"/api/frigate/{identifier.frigate_instance_id}/{server_path}", identifier.mime_type, ) - raise Unresolvable("Unknown or disallowed identifier: %s" % item.identifier) + raise Unresolvable(f"Unknown or disallowed identifier: {item.identifier}") async def async_browse_media( self, @@ -672,7 +667,7 @@ async def async_browse_media( identifier.frigate_instance_id ): raise MediaSourceError( - "Forbidden media source identifier: %s" % item.identifier + f"Forbidden media source identifier: {item.identifier}" ) if isinstance(identifier, EventSearchIdentifier): @@ -719,7 +714,7 @@ async def async_browse_media( except FrigateApiClientError as exc: raise MediaSourceError from exc - raise MediaSourceError("Invalid media source identifier: %s" % item.identifier) + raise MediaSourceError(f"Invalid media source identifier: {item.identifier}") async def _get_event_summary_data( self, identifier: EventSearchIdentifier @@ -1229,19 +1224,13 @@ def _count_by( ) -> int: """Return count of events that match the identifier.""" return sum( - [ - d["count"] - for d in summary_data.data - if ( - (identifier.after is None or d["timestamp"] >= identifier.after) - and ( - identifier.before is None or d["timestamp"] < identifier.before - ) - and (identifier.camera is None or identifier.camera in d["camera"]) - and (identifier.label is None or identifier.label in d["label"]) - and (identifier.zone is None or identifier.zone in d["zones"]) - ) - ] + d["count"] + for d in summary_data.data + if (identifier.after is None or d["timestamp"] >= identifier.after) + and (identifier.before is None or d["timestamp"] < identifier.before) + and (identifier.camera is None or identifier.camera in d["camera"]) + and (identifier.label is None or identifier.label in d["label"]) + and (identifier.zone is None or identifier.zone in d["zones"]) ) def _get_recording_base_media_source( @@ -1298,8 +1287,7 @@ def _get_recording_days( dt.datetime.strptime(day_item["day"], "%Y-%m-%d") except ValueError as exc: raise MediaSourceError( - "Media source is not valid for %s %s" - % (identifier, day_item["day"]) + f"Media source is not valid for {identifier} {day_item['day']}" ) from exc base.children.append( @@ -1340,8 +1328,7 @@ def _get_recording_hours( title = dt.datetime.strptime(hour_data["hour"], "%H").strftime("%H:00") except ValueError as exc: raise MediaSourceError( - "Media source is not valid for %s %s" - % (identifier, hour_data["hour"]) + f"Media source is not valid for {identifier} {hour_data['hour']}" ) from exc base.children.append( diff --git a/custom_components/frigate/update.py b/custom_components/frigate/update.py index 2f9d43d1..6a2fe5d9 100644 --- a/custom_components/frigate/update.py +++ b/custom_components/frigate/update.py @@ -75,7 +75,7 @@ def installed_version(self) -> str | None: if not version_hash: return None - version = str(version_hash).split("-")[0] + version = str(version_hash).split("-", maxsplit=1)[0] return version diff --git a/pyproject.toml b/pyproject.toml index ceab52a9..6048ea51 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,6 @@ ignore = [ jobs = 2 load-plugins = [ "pylint.extensions.typing", - "pylint_strict_informational", "pylint_pytest", ] persistent = false @@ -104,9 +103,9 @@ expected-line-ending-format = "LF" [tool.pylint.EXCEPTIONS] overgeneral-exceptions = [ - "BaseException", - "Exception", - "HomeAssistantError", + "builtins.BaseException", + "builtins.Exception", + "homeassistant.HomeAssistantError", ] [tool.pylint.TYPING] diff --git a/requirements_dev.txt b/requirements_dev.txt index fa95629f..2b97d7d2 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -5,7 +5,7 @@ mypy==1.1.1 pre-commit pytest pytest-homeassistant-custom-component==0.12.49 -pylint-pytest +pylint-pytest==1.1.3a0 pylint pytest-aiohttp pytest-asyncio