Skip to content

Commit ae1a6ea

Browse files
authored
Merge pull request #155 from PimDoos/dev
Mark P1 sensors unavailable when P1 data is not OK
2 parents efb7164 + 3a67c89 commit ae1a6ea

File tree

9 files changed

+59
-15
lines changed

9 files changed

+59
-15
lines changed

custom_components/sessy/binary_sensor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,4 @@ def __init__(
8585
self._attr_entity_registry_enabled_default = enabled_default
8686

8787
def update_from_cache(self):
88-
self._attr_available = self.cache_value is not None
8988
self._attr_is_on = self.cache_value

custom_components/sessy/button.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def __init__(
8383
self._attr_entity_category = entity_category
8484

8585
def update_from_cache(self):
86-
self._attr_available = self.cache_value is not None
86+
# No update necessary, availability is set from coordinator
87+
pass
8788

8889
async def async_press(self):
8990
try:

custom_components/sessy/coordinator.py

Lines changed: 25 additions & 6 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,
@@ -265,15 +265,19 @@ def __init__(
265265
data_key: str,
266266
transform_function: Optional[Callable] = None,
267267
translation_key: str = None,
268+
availability_key: str = None,
269+
availability_test_value: str = None
268270
):
269-
self.context = SessyEntityContext(data_key, transform_function)
271+
self.context = SessyEntityContext(data_key, transform_function, availability_key, availability_test_value)
270272
super().__init__(coordinator, self.context)
271273
self.hass = hass
272274
self.config_entry = config_entry
273275

274276
self.transform_function = transform_function
275277
self.data_key = data_key
276278
self.cache_value = None
279+
self.availability_key = availability_key
280+
self.availability_test_value = availability_test_value
277281

278282
device: SessyDevice = config_entry.runtime_data.device
279283

@@ -306,7 +310,10 @@ def _handle_coordinator_update(self) -> None:
306310
self.async_write_ha_state()
307311

308312
def copy_from_cache(self):
309-
self.cache_value = 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+
310317
if self.cache_value is None:
311318
raise TypeError(
312319
f"Key {self.data_key} has no value in coordinator {self.coordinator.name}"
@@ -315,15 +322,27 @@ def copy_from_cache(self):
315322
def update_from_cache(self):
316323
"""Entity function to write the latest cache value to the proper attributes. Implemented on platform level."""
317324
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
318330

319331

320332
class SessyEntityContext:
321-
def __init__(self, data_key: str, transform_function: Optional[Callable] = None):
333+
def __init__(self, data_key: str, transform_function: Optional[Callable] = None, availability_key: Optional[str] = None, availability_test_value: Optional[str] = None):
322334
self.data_key = data_key
323335
self.transform_function = transform_function
336+
self.availability_key = availability_key
337+
self.availability_test_value = availability_test_value
324338

325-
def apply(self, data):
339+
def apply(self, data) -> tuple[Any, bool]:
326340
value = get_nested_key(data, self.data_key)
327341
if self.transform_function:
328342
value = self.transform_function(value)
329-
return value
343+
if self.availability_key:
344+
availability_value = get_nested_key(data, self.availability_key)
345+
available = availability_value == self.availability_test_value
346+
else:
347+
available = value is not None
348+
return value, available

custom_components/sessy/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"issue_tracker": "https://github.com/PimDoos/ha-sessy/issues",
1313
"loggers": ["sessypy"],
1414
"requirements": ["sessypy==0.2.2"],
15-
"version": "0.9.1",
15+
"version": "0.9.2",
1616
"zeroconf":[
1717
{"type":"_http._tcp.local.","name":"sessy-*"}
1818
]

custom_components/sessy/number.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ def __init__(
195195
self.action_function: Callable = action_function
196196

197197
def update_from_cache(self):
198-
self._attr_available = self.cache_value is not None
199198
self._attr_native_value = self.cache_value
200199

201200
async def async_set_native_value(self, value: float):

custom_components/sessy/select.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ def __init__(
7777
self._attr_current_option = None
7878

7979
def update_from_cache(self):
80-
self._attr_available = self.cache_value is not None
8180
self._attr_current_option = self.cache_value
8281

8382
async def async_select_option(self, option: str) -> None:

custom_components/sessy/sensor.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ async def async_setup_entry(
455455
)
456456
sensors.append(
457457
SessySensor(
458-
hass, config_entry, "Tariff", p1_details_coordinator, "tariff_indicator"
458+
hass,
459+
config_entry,
460+
"Tariff",
461+
p1_details_coordinator,
462+
"tariff_indicator",
463+
availability_key="state",
464+
availability_test_value=SessyP1State.OK,
459465
)
460466
)
461467

@@ -469,6 +475,8 @@ async def async_setup_entry(
469475
SensorDeviceClass.POWER,
470476
SensorStateClass.MEASUREMENT,
471477
UnitOfPower.WATT,
478+
availability_key="state",
479+
availability_test_value=SessyP1State.OK,
472480
)
473481
)
474482
sensors.append(
@@ -481,6 +489,8 @@ async def async_setup_entry(
481489
SensorDeviceClass.POWER,
482490
SensorStateClass.MEASUREMENT,
483491
UnitOfPower.WATT,
492+
availability_key="state",
493+
availability_test_value=SessyP1State.OK,
484494
)
485495
)
486496
sensors.append(
@@ -493,6 +503,8 @@ async def async_setup_entry(
493503
SensorDeviceClass.POWER,
494504
SensorStateClass.MEASUREMENT,
495505
UnitOfPower.WATT,
506+
availability_key="state",
507+
availability_test_value=SessyP1State.OK,
496508
)
497509
)
498510
try:
@@ -511,6 +523,8 @@ async def async_setup_entry(
511523
transform_function=divide_by_thousand,
512524
precision=3,
513525
enabled_default=gas_meter_present,
526+
availability_key="state",
527+
availability_test_value=SessyP1State.OK,
514528
)
515529
)
516530
except Exception as e:
@@ -528,6 +542,8 @@ async def async_setup_entry(
528542
SensorStateClass.MEASUREMENT,
529543
UnitOfElectricPotential.MILLIVOLT,
530544
suggested_unit_of_measurement=UnitOfElectricPotential.VOLT,
545+
availability_key="state",
546+
availability_test_value=SessyP1State.OK,
531547
)
532548
)
533549
sensors.append(
@@ -542,6 +558,8 @@ async def async_setup_entry(
542558
UnitOfElectricCurrent.MILLIAMPERE,
543559
precision=0,
544560
suggested_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
561+
availability_key="state",
562+
availability_test_value=SessyP1State.OK,
545563
)
546564
)
547565
sensors.append(
@@ -554,6 +572,8 @@ async def async_setup_entry(
554572
SensorDeviceClass.POWER,
555573
SensorStateClass.MEASUREMENT,
556574
UnitOfPower.WATT,
575+
availability_key="state",
576+
availability_test_value=SessyP1State.OK,
557577
)
558578
)
559579
sensors.append(
@@ -566,6 +586,8 @@ async def async_setup_entry(
566586
SensorDeviceClass.POWER,
567587
SensorStateClass.MEASUREMENT,
568588
UnitOfPower.WATT,
589+
availability_key="state",
590+
availability_test_value=SessyP1State.OK,
569591
)
570592
)
571593

@@ -581,6 +603,8 @@ async def async_setup_entry(
581603
SensorStateClass.TOTAL,
582604
UnitOfEnergy.WATT_HOUR,
583605
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
606+
availability_key="state",
607+
availability_test_value=SessyP1State.OK,
584608
)
585609
)
586610
sensors.append(
@@ -594,6 +618,8 @@ async def async_setup_entry(
594618
SensorStateClass.TOTAL,
595619
UnitOfEnergy.WATT_HOUR,
596620
suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
621+
availability_key="state",
622+
availability_test_value=SessyP1State.OK,
597623
)
598624
)
599625

@@ -711,6 +737,8 @@ def __init__(
711737
precision: int = None,
712738
suggested_unit_of_measurement=None,
713739
enabled_default: bool = True,
740+
availability_key: str = None,
741+
availability_test_value: str = None,
714742
):
715743
super().__init__(
716744
hass=hass,
@@ -720,6 +748,8 @@ def __init__(
720748
data_key=data_key,
721749
transform_function=transform_function,
722750
translation_key=translation_key,
751+
availability_key=availability_key,
752+
availability_test_value=availability_test_value,
723753
)
724754

725755
self._attr_device_class = device_class
@@ -735,7 +765,6 @@ def __init__(
735765
self._attr_entity_registry_enabled_default = enabled_default
736766

737767
def update_from_cache(self):
738-
self._attr_available = self.cache_value is not None
739768
self._attr_native_value = self.cache_value
740769

741770

custom_components/sessy/switch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def __init__(
9999
self.action_function: Callable = action_function
100100

101101
def update_from_cache(self):
102-
self._attr_available = self.cache_value is not None
103102
self._attr_is_on = self.cache_value
104103

105104
async def async_turn_on(self):

custom_components/sessy/time.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ def __init__(
116116
self.action_function: Callable = action_function
117117

118118
def update_from_cache(self):
119-
self._attr_available = self.cache_value is not None
120119
self._attr_native_value = self.cache_value
121120

122121
async def async_set_value(self, value: time):

0 commit comments

Comments
 (0)