1313from homeassistant .helpers .storage import Store
1414from homeassistant .util import dt as dt_util
1515
16- from .const import (
17- SIGNAL_TASK_CREATED ,
18- SIGNAL_TASK_DELETED ,
19- SIGNAL_TASK_UPDATED ,
20- )
16+ from .domain import DOMAIN
2117
2218if TYPE_CHECKING :
23- from homeassistant .config_entries import ConfigEntry
2419 from homeassistant .core import HomeAssistant
2520
26- STORAGE_KEY = "maint.tasks"
21+ SIGNAL_TASK_CREATED = "maint_task_created"
22+ SIGNAL_TASK_UPDATED = "maint_task_updated"
23+ SIGNAL_TASK_DELETED = "maint_task_deleted"
24+
25+ STORAGE_KEY = f"{ DOMAIN } .store"
2726STORAGE_VERSION = 1
27+
2828FREQUENCY_UNIT_DAYS : Literal ["days" ] = "days"
2929FREQUENCY_UNIT_WEEKS : Literal ["weeks" ] = "weeks"
3030FREQUENCY_UNIT_MONTHS : Literal ["months" ] = "months"
3535)
3636FrequencyUnit = Literal ["days" , "weeks" , "months" ]
3737
38-
39- class _UnsetType :
40- """Sentinel type for unset optional values."""
41-
42-
43- UNSET = _UnsetType ()
4438_LOGGER = logging .getLogger (__name__ )
4539
4640
@@ -140,29 +134,25 @@ async def async_load(self) -> None:
140134 return
141135 _LOGGER .debug ("Loading Maint tasks from storage" )
142136 data = await self ._store .async_load ()
143- if not data :
144- self ._tasks = {}
145- else :
146- entries = data .get ("entries" , {})
147- tasks_by_entry : dict [str , dict [str , MaintTask ]] = {}
148- for entry_id , tasks in entries .items ():
149- entry_tasks : dict [str , MaintTask ] = {}
150- for task_data in tasks :
151- entry_tasks [task_data ["task_id" ]] = MaintTask .from_dict (task_data )
152- tasks_by_entry [entry_id ] = entry_tasks
153- self ._tasks = tasks_by_entry
154- total_tasks = sum (len (tasks ) for tasks in self ._tasks .values ())
137+ entries = data .get ("entries" , {}) if data else {}
138+ self ._tasks = {
139+ entry_id : {
140+ task_data ["task_id" ]: MaintTask .from_dict (task_data )
141+ for task_data in tasks
142+ }
143+ for entry_id , tasks in entries .items ()
144+ }
145+ entries_count , tasks_count = self ._counts ()
155146 _LOGGER .debug (
156147 "Loaded Maint task store: %s entries, %s tasks" ,
157- len ( self . _tasks ) ,
158- total_tasks ,
148+ entries_count ,
149+ tasks_count ,
159150 )
160151 self ._loaded = True
161152
162153 async def _async_save (self ) -> None :
163154 """Persist tasks to disk."""
164- entries_count = len (self ._tasks )
165- tasks_count = sum (len (tasks ) for tasks in self ._tasks .values ())
155+ entries_count , tasks_count = self ._counts ()
166156 await self ._store .async_save (
167157 {
168158 "entries" : {
@@ -177,6 +167,10 @@ async def _async_save(self) -> None:
177167 tasks_count ,
178168 )
179169
170+ def _counts (self ) -> tuple [int , int ]:
171+ """Return counts of entries and tasks for logging."""
172+ return len (self ._tasks ), sum (len (tasks ) for tasks in self ._tasks .values ())
173+
180174 async def _async_get_entry_tasks (self , entry_id : str ) -> dict [str , MaintTask ]:
181175 """Return the task mapping for an entry."""
182176 await self .async_load ()
@@ -314,8 +308,8 @@ def validate( # noqa: PLR0913
314308 message = "description cannot be empty"
315309 raise ValueError (message )
316310
317- if last_completed is not None and last_completed == "" :
318- message = "last_completed cannot be empty "
311+ if last_completed is not None and not isinstance ( last_completed , date ) :
312+ message = "last_completed must be a date "
319313 raise ValueError (message )
320314
321315 if frequency is not None and frequency <= 0 :
0 commit comments