Skip to content

Commit 50a70ca

Browse files
committed
Fix bug with handling of end_driving_delay config
1 parent cfd40d0 commit 50a70ca

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

custom_components/composite/config.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
from contextlib import suppress
5+
from datetime import timedelta
56
import logging
67
from pathlib import Path
78
from 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+
6577
def _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(

custom_components/composite/device_tracker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, State
4141
from homeassistant.helpers import entity_registry as er
42+
import homeassistant.helpers.config_validation as cv
4243
from homeassistant.helpers.dispatcher import async_dispatcher_send
4344
from homeassistant.helpers.entity_platform import AddEntitiesCallback
4445
from homeassistant.helpers.event import async_call_later, async_track_state_change_event
@@ -289,7 +290,10 @@ async def _process_config_options(self) -> None:
289290
options = cast(ConfigEntry, self.platform.config_entry).options
290291
self._req_movement = options[CONF_REQ_MOVEMENT]
291292
self._driving_speed = options.get(CONF_DRIVING_SPEED)
292-
self._end_driving_delay = options.get(CONF_END_DRIVING_DELAY)
293+
if (edd := options.get(CONF_END_DRIVING_DELAY)) is None:
294+
self._end_driving_delay = None
295+
else:
296+
self._end_driving_delay = cast(timedelta, cv.time_period(edd))
293297
entity_cfgs = {
294298
entity_cfg[CONF_ENTITY]: entity_cfg
295299
for entity_cfg in options[CONF_ENTITY_ID]

0 commit comments

Comments
 (0)