Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename fire events #1277

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Cleanup cover
- Replace last_updated by last_refreshed
- Rename fire events

# Version 2023.10.13 (2023-10-28)

Expand Down
4 changes: 2 additions & 2 deletions hahomematic/central/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def add_sysvar_entity(self, sysvar_entity: GenericSystemVariable) -> None:
def remove_sysvar_entity(self, name: str) -> None:
"""Remove a sysvar entity."""
if (sysvar_entity := self.get_sysvar_entity(name=name)) is not None:
sysvar_entity.remove_entity()
sysvar_entity.fire_remove_entity_callback()
del self._sysvar_entities[name]

@property
Expand All @@ -283,7 +283,7 @@ def add_program_button(self, program_button: HmProgramButton) -> None:
def remove_program_button(self, pid: str) -> None:
"""Remove a program button."""
if (program_button := self.get_program_button(pid=pid)) is not None:
program_button.remove_entity()
program_button.fire_remove_entity_callback()
del self._program_buttons[pid]

@property
Expand Down
4 changes: 2 additions & 2 deletions hahomematic/platforms/custom/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def load_entity_value(self, call_source: CallSource) -> None:
"""Init the entity values."""
for entity in self._readable_entities:
await entity.load_entity_value(call_source=call_source)
self.update_entity()
self.fire_update_entity_callback()

def is_state_change(self, **kwargs: Any) -> bool:
"""
Expand Down Expand Up @@ -221,7 +221,7 @@ def _add_entity(
if is_visible:
entity.set_usage(EntityUsage.CE_VISIBLE)

entity.register_internal_update_callback(update_callback=self.update_entity)
entity.register_internal_update_callback(update_callback=self.fire_update_entity_callback)
self._data_entities[field] = entity

def unregister_update_callback(self, update_callback: Callable, custom_id: str) -> None:
Expand Down
21 changes: 12 additions & 9 deletions hahomematic/platforms/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from hahomematic.platforms.generic.entity import GenericEntity
from hahomematic.platforms.support import PayloadMixin, get_device_name
from hahomematic.platforms.update import HmUpdate
from hahomematic.support import CacheEntry, Channel, check_or_create_directory
from hahomematic.support import CacheEntry, Channel, check_or_create_directory, reduce_args

_LOGGER: Final = logging.getLogger(__name__)

Expand Down Expand Up @@ -330,7 +330,7 @@ def add_entity(self, entity: CallbackEntity) -> None:
self.central.add_event_subscription(entity=entity)
if isinstance(entity, GenericEntity):
self._generic_entities[(entity.channel_address, entity.parameter)] = entity
self.register_update_callback(entity.update_entity)
self.register_update_callback(update_callback=entity.fire_update_entity_callback)
if isinstance(entity, hmce.CustomEntity):
self._custom_entities[entity.channel_no] = entity
if isinstance(entity, GenericEvent):
Expand All @@ -342,12 +342,12 @@ def remove_entity(self, entity: CallbackEntity) -> None:
self.central.remove_event_subscription(entity=entity)
if isinstance(entity, GenericEntity):
del self._generic_entities[(entity.channel_address, entity.parameter)]
self.unregister_update_callback(entity.update_entity)
self.unregister_update_callback(update_callback=entity.fire_update_entity_callback)
if isinstance(entity, hmce.CustomEntity):
del self._custom_entities[entity.channel_no]
if isinstance(entity, GenericEvent):
del self._generic_events[(entity.channel_address, entity.parameter)]
entity.remove_entity()
entity.fire_remove_entity_callback()

def clear_collections(self) -> None:
"""Remove entities from collections and central."""
Expand Down Expand Up @@ -468,7 +468,7 @@ def set_forced_availability(self, forced_availability: ForcedDeviceAvailability)
if self._forced_availability != forced_availability:
self._forced_availability = forced_availability
for entity in self.generic_entities:
entity.update_entity()
entity.fire_update_entity_callback()

async def export_device_definition(self) -> None:
"""Export the device definition for current device."""
Expand Down Expand Up @@ -538,13 +538,16 @@ async def reload_paramset_descriptions(self) -> None:
await self.central.paramset_descriptions.save()
for entity in self.generic_entities:
entity.update_parameter_data()
self.update_device()
self.fire_update_device_callback()

def update_device(self, *args: Any) -> None:
"""Do what is needed when the state of the entity has been updated."""
def fire_update_device_callback(self, *args: Any) -> None:
"""Do what is needed when the state of the device has been updated."""
self._set_last_updated()
for _callback in self._update_callbacks:
_callback(*args)
try:
_callback(*args)
except Exception as ex:
_LOGGER.warning("FIRE_UPDATE_DEVICE failed: %s", reduce_args(args=ex.args))

def __str__(self) -> str:
"""Provide some useful information."""
Expand Down
54 changes: 44 additions & 10 deletions hahomematic/platforms/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
convert_value,
generate_channel_unique_id,
)
from hahomematic.support import reduce_args

_LOGGER: Final = logging.getLogger(__name__)

Expand Down Expand Up @@ -104,6 +105,7 @@ def __init__(self, central: hmcu.CentralUnit, unique_id: str) -> None:
self._central: Final = central
self._unique_id: Final = unique_id
self._update_callbacks: dict[Callable, str] = {}
self._refresh_callbacks: dict[Callable, str] = {}
self._remove_callbacks: list[Callable] = []
self._custom_id: str | None = None

Expand Down Expand Up @@ -194,6 +196,24 @@ def unregister_update_callback(self, update_callback: Callable, custom_id: str)
if self.custom_id == custom_id:
self._custom_id = None

def register_refresh_callback(self, refresh_callback: Callable, custom_id: str) -> None:
"""Register update callback."""
if callable(refresh_callback):
self._refresh_callbacks[refresh_callback] = custom_id
if custom_id != DEFAULT_CUSTOM_ID:
if self._custom_id is not None:
raise HaHomematicException(
f"REGISTER_REFRESH_CALLBACK failed: hm_entity: {self.full_name} is already registered by {self._custom_id}"
)
self._custom_id = custom_id

def unregister_refresh_callback(self, refresh_callback: Callable, custom_id: str) -> None:
"""Unregister update callback."""
if refresh_callback in self._refresh_callbacks:
del self._refresh_callbacks[refresh_callback]
if self.custom_id == custom_id:
self._custom_id = None

def register_remove_callback(self, remove_callback: Callable) -> None:
"""Register the remove callback."""
if callable(remove_callback) and remove_callback not in self._remove_callbacks:
Expand All @@ -204,15 +224,29 @@ def unregister_remove_callback(self, remove_callback: Callable) -> None:
if remove_callback in self._remove_callbacks:
self._remove_callbacks.remove(remove_callback)

def update_entity(self, *args: Any, **kwargs: Any) -> None:
def fire_update_entity_callback(self, *args: Any, **kwargs: Any) -> None:
"""Do what is needed when the value of the entity has been updated."""
for _callback in self._update_callbacks:
_callback(*args, **kwargs)

def remove_entity(self, *args: Any) -> None:
try:
_callback(*args, **kwargs)
except Exception as ex:
_LOGGER.warning("FIRE_UPDATE_ENTITY_EVENT failed: %s", reduce_args(args=ex.args))

def fire_refresh_entity_callback(self, *args: Any, **kwargs: Any) -> None:
"""Do what is needed when the value of the entity has been refreshed."""
for _callback in self._refresh_callbacks:
try:
_callback(*args, **kwargs)
except Exception as ex:
_LOGGER.warning("FIRE_REFRESH_ENTITY_EVENT failed: %s", reduce_args(args=ex.args))

def fire_remove_entity_callback(self, *args: Any) -> None:
"""Do what is needed when the entity has been removed."""
for _callback in self._remove_callbacks:
_callback(*args)
try:
_callback(*args)
except Exception as ex:
_LOGGER.warning("FIRE_REMOVE_ENTITY_EVENT failed: %s", reduce_args(args=ex.args))


class BaseEntity(CallbackEntity, PayloadMixin):
Expand Down Expand Up @@ -315,9 +349,9 @@ def set_usage(self, usage: EntityUsage) -> None:
"""Set the entity usage."""
self._usage = usage

def update_entity(self, *args: Any, **kwargs: Any) -> None:
def fire_update_entity_callback(self, *args: Any, **kwargs: Any) -> None:
"""Do what is needed when the value of the entity has been updated."""
super().update_entity(*args, **kwargs)
super().fire_update_entity_callback(*args, **kwargs)
self._central.fire_entity_data_event_callback(
interface_id=self._device.interface_id, entity=self
)
Expand Down Expand Up @@ -601,14 +635,14 @@ async def load_entity_value(self, call_source: CallSource) -> None:
def update_value(self, value: Any) -> None:
"""Update value of the entity."""
if value == NO_CACHE_ENTRY:
if self.last_updated != INIT_DATETIME:
if self.last_refreshed != INIT_DATETIME:
self._state_uncertain = True
self.update_entity()
self.fire_update_entity_callback()
return
self._value = self._convert_value(value)
self._state_uncertain = False
self._set_last_updated()
self.update_entity()
self.fire_update_entity_callback()

def update_parameter_data(self) -> None:
"""Update parameter data."""
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/platforms/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def event_type(self) -> EventType:
def event(self, value: Any) -> None:
"""Handle event for which this handler has subscribed."""
if self.event_type in ENTITY_EVENTS:
self.update_entity(parameter=self.parameter.lower())
self.fire_update_entity_callback(parameter=self.parameter.lower())
self.fire_event(value)

def fire_event(self, value: Any) -> None:
Expand Down
3 changes: 2 additions & 1 deletion hahomematic/platforms/generic/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def event(self, value: Any) -> None:
new_value = self._convert_value(value)
if self._value == new_value:
self._set_last_refreshed()
self.fire_refresh_entity_callback()
return
self.update_value(value=new_value)

Expand All @@ -71,7 +72,7 @@ def event(self, value: Any) -> None:
Parameter.UN_REACH,
Parameter.STICKY_UN_REACH,
):
self._device.update_device(self._unique_id)
self._device.fire_update_device_callback(self._unique_id)
self._central.fire_ha_event_callback(
event_type=EventType.DEVICE_AVAILABILITY,
event_data=self.get_event_data(new_value),
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/platforms/hub/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def update_data(self, data: ProgramData) -> None:
self.last_execute_time = data.last_execute_time
do_update = True
if do_update:
self.update_entity()
self.fire_update_entity_callback()

async def press(self) -> None:
"""Handle the button press."""
Expand Down
2 changes: 1 addition & 1 deletion hahomematic/platforms/hub/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def update_value(self, value: Any) -> None:

if self._value != value:
self._value = value
self.update_entity()
self.fire_update_entity_callback()

async def send_variable(self, value: Any) -> None:
"""Set variable value on CCU/Homegear."""
Expand Down