Skip to content

Commit

Permalink
Merge pull request #226 from ocrease/Beta9Fixes
Browse files Browse the repository at this point in the history
Beta9 fixes
  • Loading branch information
MindrustUK authored Dec 18, 2024
2 parents 3fc5d9a + 1229932 commit 27eda58
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
2 changes: 2 additions & 0 deletions custom_components/heatmiserneo/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
HeatmiserNeoEntityDescription,
HeatmiserNeoHubEntity,
HeatmiserNeoHubEntityDescription,
profile_sensor_enabled_by_default,
)
from .helpers import profile_level

Expand Down Expand Up @@ -187,6 +188,7 @@ class HeatmiserNeoHubBinarySensorEntityDescription(
device.device_type in HEATMISER_TYPE_IDS_THERMOSTAT_NOT_HC
and device.time_clock_mode
),
enabled_by_default_fn=profile_sensor_enabled_by_default,
),
)

Expand Down
41 changes: 40 additions & 1 deletion custom_components/heatmiserneo/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import logging
from typing import Any

from neohubapi.neohub import ATTR_SYSTEM, NeoHub, NeoStat
from neohubapi.neohub import ATTR_SYSTEM, NeoHub, NeoStat, ScheduleFormat
from propcache import cached_property

from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import ServiceCall
Expand All @@ -36,6 +37,7 @@ class HeatmiserNeoEntityDescription(EntityDescription):

setup_filter_fn: Callable[[NeoStat, Any], bool] = lambda dev, sys_data: True
availability_fn: Callable[[NeoStat], bool] = lambda device: not device.offline
enabled_by_default_fn: Callable[[HeatmiserNeoEntity], bool] | None = None
icon_fn: Callable[[NeoStat], str | None] | None = None
# extra_attrs: list[str] | None = None
custom_functions: (
Expand All @@ -51,6 +53,7 @@ class HeatmiserNeoHubEntityDescription(EntityDescription):
setup_filter_fn: Callable[[HeatmiserNeoCoordinator], bool] = (
lambda coordinator: True
)
enabled_by_default_fn: Callable[[HeatmiserNeoHubEntity], bool] | None = None
icon_fn: Callable[[NeoStat], str | None] | None = None
# extra_attrs: list[str] | None = None
custom_functions: (
Expand Down Expand Up @@ -150,6 +153,16 @@ def icon(self) -> str | None:
return self.entity_description.icon_fn(self.data)
return None

@cached_property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added.
This only applies when fist added to the entity registry.
"""
if self.entity_description.enabled_by_default_fn:
return self.entity_description.enabled_by_default_fn(self)
return super().entity_registry_enabled_default

async def call_custom_action(self, service_call: ServiceCall) -> None:
"""Call a custom action specified in the entity description."""
await self.entity_description.custom_functions.get(service_call.service)(
Expand Down Expand Up @@ -246,6 +259,16 @@ def icon(self) -> str | None:
return self.entity_description.icon_fn(self.data)
return None

@cached_property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added.
This only applies when fist added to the entity registry.
"""
if self.entity_description.enabled_by_default_fn:
return self.entity_description.enabled_by_default_fn(self)
return super().entity_registry_enabled_default


async def call_custom_action(
entity: HeatmiserNeoEntity, service_call: ServiceCall
Expand All @@ -266,3 +289,19 @@ async def call_custom_action(

def _device_supports_away(dev: NeoStat) -> bool:
return dev.device_type in HEATMISER_TYPE_IDS_AWAY


def profile_sensor_enabled_by_default(entity: HeatmiserNeoEntity) -> bool:
"""Determine if a profile entity should be enabled by default."""
if (
hasattr(entity.coordinator.system_data, "FORMAT")
and entity.coordinator.system_data.FORMAT != ScheduleFormat.ZERO
):
return True
if (
entity.data.time_clock_mode
and hasattr(entity.coordinator.system_data, "ALT_TIMER_FORMAT")
and entity.coordinator.system_data.ALT_TIMER_FORMAT != ScheduleFormat.ZERO
):
return True
return False
9 changes: 6 additions & 3 deletions custom_components/heatmiserneo/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ def profile_level(

flatten_fn = _flatten_timer_levels
levels_filter = _timer_level_filter
elif profile_id == 0:
profile = coordinator.profiles_0.get(data.device_id)
else:
profile = coordinator.profiles.get(int(profile_id))
if profile_format == ScheduleFormat.ZERO:
return None
if profile_id == 0:
profile = coordinator.profiles_0.get(data.device_id)
else:
profile = coordinator.profiles.get(int(profile_id))

if hasattr(profile, "error") or not profile:
return None
Expand Down
3 changes: 3 additions & 0 deletions custom_components/heatmiserneo/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
HeatmiserNeoHubEntity,
HeatmiserNeoHubEntityDescription,
call_custom_action,
profile_sensor_enabled_by_default,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -411,6 +412,7 @@ async def async_base_set_profile(
set_value_fn=async_set_profile,
name="Active Profile",
# translation_key="preheat_time",
enabled_by_default_fn=profile_sensor_enabled_by_default,
),
HeatmiserNeoSelectEntityDescription(
key="heatmiser_neo_active_timer_profile",
Expand All @@ -427,6 +429,7 @@ async def async_base_set_profile(
set_value_fn=async_set_timer_profile,
name="Active Profile",
# translation_key="preheat_time",
enabled_by_default_fn=profile_sensor_enabled_by_default,
),
)

Expand Down
8 changes: 6 additions & 2 deletions custom_components/heatmiserneo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
HeatmiserNeoEntityDescription,
HeatmiserNeoHubEntity,
HeatmiserNeoHubEntityDescription,
profile_sensor_enabled_by_default,
)
from .helpers import profile_level

Expand Down Expand Up @@ -176,7 +177,7 @@ class HeatmiserNeoHubSensorEntityDescription(
key="heatmiser_neo_stat_hold_temp",
device_class=SensorDeviceClass.TEMPERATURE,
name="Hold Temperature",
value_fn=lambda device: device.data.hold_temp,
value_fn=lambda device: device.data.hold_temp if device.data.hold_on else None,
setup_filter_fn=lambda device, sys_data: (
(
device.device_type in HEATMISER_TYPE_IDS_THERMOSTAT_NOT_HC
Expand All @@ -195,7 +196,7 @@ class HeatmiserNeoHubSensorEntityDescription(
key="heatmiser_neo_stat_hold_temp_cool",
device_class=SensorDeviceClass.TEMPERATURE,
name="Hold Cooling Temperature",
value_fn=lambda device: device.data.hold_cool,
value_fn=lambda device: device.data.hold_cool if device.data.hold_on else None,
setup_filter_fn=lambda device, sys_data: (
device.device_type in HEATMISER_TYPE_IDS_HC
and not device.time_clock_mode
Expand Down Expand Up @@ -231,6 +232,7 @@ class HeatmiserNeoHubSensorEntityDescription(
unit_of_measurement_fn=lambda _, sys_data: (
HEATMISER_TEMPERATURE_UNIT_HA_UNIT.get(sys_data.CORF, None)
),
enabled_by_default_fn=profile_sensor_enabled_by_default,
),
HeatmiserNeoSensorEntityDescription(
key="heatmiser_neo_profile_next_temp",
Expand All @@ -244,6 +246,7 @@ class HeatmiserNeoHubSensorEntityDescription(
unit_of_measurement_fn=lambda _, sys_data: (
HEATMISER_TEMPERATURE_UNIT_HA_UNIT.get(sys_data.CORF, None)
),
enabled_by_default_fn=profile_sensor_enabled_by_default,
),
HeatmiserNeoSensorEntityDescription(
key="heatmiser_neo_profile_next_time",
Expand All @@ -253,6 +256,7 @@ class HeatmiserNeoHubSensorEntityDescription(
setup_filter_fn=lambda device, _: (
device.device_type in HEATMISER_TYPE_IDS_THERMOSTAT_NOT_HC
),
enabled_by_default_fn=profile_sensor_enabled_by_default,
),
)

Expand Down

0 comments on commit 27eda58

Please sign in to comment.