[core] Enable ruff DTZ (flake8-datetimez) lint family (#16660)
This commit is contained in:
1 parent
bac62cb7de
commit
96816e2491
12 files changed
+40
-28
No files matched your search
+1
-1
@@ -639,7 +639,7 @@ def run_miniterm(config: ConfigType, port: str, args) -> int:
|
||||
chunk = ser.read(ser.in_waiting or 1)
|
||||
if not chunk:
|
||||
continue
|
||||
time_ = datetime.now()
|
||||
time_ = datetime.now().astimezone()
|
||||
milliseconds = time_.microsecond // 1000
|
||||
time_str = f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}.{milliseconds:03}]"
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ async def async_run_logs(
|
||||
|
||||
def on_log(msg: SubscribeLogsResponse) -> None:
|
||||
"""Handle a new log message."""
|
||||
time_ = datetime.now()
|
||||
time_ = datetime.now().astimezone()
|
||||
message: bytes = msg.message
|
||||
text = message.decode("utf8", "backslashreplace")
|
||||
nanoseconds = time_.microsecond // 1000
|
||||
|
||||
@@ -161,7 +161,10 @@ async def _attr_to_code(config: ConfigType) -> None:
|
||||
zigbee_set_string(basic_attrs.mf_name, "esphome"),
|
||||
zigbee_set_string(basic_attrs.model_id, config[CONF_MODEL]),
|
||||
zigbee_set_string(
|
||||
basic_attrs.date_code, datetime.datetime.now().strftime("%Y%m%d %H%M%S")
|
||||
basic_attrs.date_code,
|
||||
# Local build time, matching the esp32 implementation
|
||||
# (App.get_build_time() in C++).
|
||||
datetime.datetime.now().astimezone().strftime("%Y%m%d %H%M%S"),
|
||||
),
|
||||
zigbee_assign(
|
||||
basic_attrs.power_source,
|
||||
|
||||
@@ -1183,7 +1183,9 @@ def date_time(date: bool, time: bool):
|
||||
format += "%p"
|
||||
|
||||
try:
|
||||
date_obj = datetime.strptime(value, format)
|
||||
# The generated format never includes %z/%Z, so this parses a
|
||||
# naive wall-clock date/time by design.
|
||||
date_obj = datetime.strptime(value, format) # noqa: DTZ007
|
||||
except ValueError as err:
|
||||
raise Invalid(f"Invalid {exc_message}: {err}") from err
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ from datetime import UTC, datetime
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
@@ -141,9 +142,11 @@ def has_remote_file_changed(
|
||||
|
||||
def is_file_recent(file_path: Path, refresh: TimePeriodSeconds) -> bool:
|
||||
if file_path.exists():
|
||||
creation_time = file_path.stat().st_ctime
|
||||
current_time = datetime.now().timestamp()
|
||||
return current_time - creation_time <= refresh.total_seconds
|
||||
# st_mtime, not st_ctime: ctime is inode-change time on POSIX
|
||||
# (bumped by chmod/chown/rename) so a metadata touch would make
|
||||
# the file look fresh.
|
||||
modification_time = file_path.stat().st_mtime
|
||||
return time.time() - modification_time <= refresh.total_seconds
|
||||
return False
|
||||
|
||||
|
||||
|
||||
+4
-4
@@ -1,12 +1,12 @@
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
|
||||
import esphome.config_validation as cv
|
||||
@@ -247,11 +247,11 @@ def clone_or_update(
|
||||
return repo_dir, None
|
||||
|
||||
file_timestamp = Path(repo_dir / ".git" / "FETCH_HEAD")
|
||||
# On first clone, FETCH_HEAD does not exists
|
||||
# On first clone, FETCH_HEAD does not exist
|
||||
if not file_timestamp.exists():
|
||||
file_timestamp = Path(repo_dir / ".git" / "HEAD")
|
||||
age = datetime.now() - datetime.fromtimestamp(file_timestamp.stat().st_mtime)
|
||||
if refresh is None or age.total_seconds() > refresh.total_seconds:
|
||||
age_seconds = time.time() - file_timestamp.stat().st_mtime
|
||||
if refresh is None or age_seconds > refresh.total_seconds:
|
||||
# Try to update the repository, recovering from broken state if needed
|
||||
old_sha: str | None = None
|
||||
try:
|
||||
|
||||
+3
-3
@@ -139,7 +139,7 @@ def show_discover(config, username=None, password=None, client_id=None):
|
||||
_LOGGER.info("Starting log output from %s", topic)
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
time_ = datetime.now().time().strftime("[%H:%M:%S]")
|
||||
time_ = datetime.now().astimezone().time().strftime("[%H:%M:%S]")
|
||||
payload = msg.payload.decode(errors="backslashreplace")
|
||||
if len(payload) > 0:
|
||||
message = time_ + " " + payload
|
||||
@@ -184,7 +184,7 @@ def get_esphome_device_ip(
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
nonlocal dev_ip
|
||||
time_ = datetime.now().time().strftime("[%H:%M:%S]")
|
||||
time_ = datetime.now().astimezone().time().strftime("[%H:%M:%S]")
|
||||
payload = msg.payload.decode(errors="backslashreplace")
|
||||
if len(payload) > 0:
|
||||
message = time_ + " " + payload
|
||||
@@ -253,7 +253,7 @@ def show_logs(config, topic=None, username=None, password=None, client_id=None):
|
||||
_LOGGER.info("Starting log output from %s", topic)
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
time_ = datetime.now().time().strftime("[%H:%M:%S]")
|
||||
time_ = datetime.now().astimezone().time().strftime("[%H:%M:%S]")
|
||||
payload = msg.payload.decode(errors="backslashreplace")
|
||||
message = time_ + payload
|
||||
safe_print(message)
|
||||
|
||||
@@ -338,7 +338,10 @@ class EsphomeStorageJSON:
|
||||
@property
|
||||
def last_update_check(self) -> datetime | None:
|
||||
try:
|
||||
return datetime.strptime(self.last_update_check_str, "%Y-%m-%dT%H:%M:%S")
|
||||
# Stored format is naive ISO without %z; preserved for backward compat.
|
||||
return datetime.strptime( # noqa: DTZ007
|
||||
self.last_update_check_str, "%Y-%m-%dT%H:%M:%S"
|
||||
)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
return None
|
||||
|
||||
|
||||
Reference in new issue
Block a user