Skip to content

Commit 3a67c89

Browse files
committed
Add availability test to context and set availability in coordinator
1 parent 4b0c515 commit 3a67c89

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

custom_components/sessy/coordinator.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626
from sessypy.util import SessyLoginException, SessyNotSupportedException
2727

28-
from typing import Callable, Optional
28+
from typing import Any, Callable, Optional
2929

3030
from .const import (
3131
COORDINATOR_RETRIES,
@@ -268,7 +268,7 @@ def __init__(
268268
availability_key: str = None,
269269
availability_test_value: str = None
270270
):
271-
self.context = SessyEntityContext(data_key, transform_function)
271+
self.context = SessyEntityContext(data_key, transform_function, availability_key, availability_test_value)
272272
super().__init__(coordinator, self.context)
273273
self.hass = hass
274274
self.config_entry = config_entry
@@ -310,7 +310,10 @@ def _handle_coordinator_update(self) -> None:
310310
self.async_write_ha_state()
311311

312312
def copy_from_cache(self):
313-
self.cache_value, self._attr_available = self.coordinator.data.get(self.context, None)
313+
value, available = self.coordinator.data.get(self.context, tuple((None, False)))
314+
self.cache_value = value
315+
self._attr_available = available
316+
314317
if self.cache_value is None:
315318
raise TypeError(
316319
f"Key {self.data_key} has no value in coordinator {self.coordinator.name}"
@@ -319,6 +322,11 @@ def copy_from_cache(self):
319322
def update_from_cache(self):
320323
"""Entity function to write the latest cache value to the proper attributes. Implemented on platform level."""
321324
raise NotImplementedError()
325+
326+
@property
327+
def available(self) -> bool:
328+
"""Return True if entity is available."""
329+
return super().available and self._attr_available
322330

323331

324332
class SessyEntityContext:
@@ -328,7 +336,7 @@ def __init__(self, data_key: str, transform_function: Optional[Callable] = None,
328336
self.availability_key = availability_key
329337
self.availability_test_value = availability_test_value
330338

331-
def apply(self, data):
339+
def apply(self, data) -> tuple[Any, bool]:
332340
value = get_nested_key(data, self.data_key)
333341
if self.transform_function:
334342
value = self.transform_function(value)

0 commit comments

Comments
 (0)