22from __future__ import annotations
33
44from contextlib import suppress
5+ from datetime import timedelta
56import logging
67from pathlib import Path
78from typing import Any , cast
@@ -62,6 +63,17 @@ def _entities(entities: list[str | dict]) -> list[dict]:
6263 return result
6364
6465
66+ def _time_period_to_dict (delay : timedelta ) -> dict [str , float ]:
67+ """Return timedelta as a dict."""
68+ result : dict [str , float ] = {}
69+ if delay .days :
70+ result ["days" ] = delay .days
71+ result ["hours" ] = delay .seconds // 3600
72+ result ["minutes" ] = (delay .seconds // 60 ) % 60
73+ result ["seconds" ] = delay .seconds % 60
74+ return result
75+
76+
6577def _entity_picture (entity_picture : str ) -> str :
6678 """Validate entity picture.
6779
@@ -177,14 +189,15 @@ def _defaults(config: dict) -> dict:
177189 vol .Length (1 ),
178190 _entities ,
179191)
192+ _DELAY = vol .All (cv .positive_time_period , _time_period_to_dict )
180193_TRACKER = {
181194 vol .Required (CONF_NAME ): cv .string ,
182195 vol .Optional (CONF_ID ): cv .slugify ,
183196 vol .Required (CONF_ENTITY_ID ): _ENTITIES ,
184197 vol .Optional (CONF_TIME_AS ): cv .string ,
185198 vol .Optional (CONF_REQ_MOVEMENT ): cv .boolean ,
186199 vol .Optional (CONF_DRIVING_SPEED ): vol .Coerce (float ),
187- vol .Optional (CONF_END_DRIVING_DELAY ): cv . positive_time_period ,
200+ vol .Optional (CONF_END_DRIVING_DELAY ): _DELAY ,
188201 vol .Optional (CONF_ENTITY_PICTURE ): vol .All (cv .string , _entity_picture ),
189202}
190203_CONFIG_SCHEMA = vol .Schema (
@@ -201,8 +214,7 @@ def _defaults(config: dict) -> dict:
201214 CONF_REQ_MOVEMENT , default = DEF_REQ_MOVEMENT
202215 ): cv .boolean ,
203216 vol .Optional (CONF_DRIVING_SPEED ): vol .Coerce (float ),
204- vol .Optional (CONF_END_DRIVING_DELAY ):
205- cv .positive_time_period ,
217+ vol .Optional (CONF_END_DRIVING_DELAY ): _DELAY ,
206218 }
207219 ),
208220 vol .Required (CONF_TRACKERS , default = list ): vol .All (
0 commit comments