Skip to content

Commit

Permalink
Updating support for swtich/button data type
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Feb 1, 2022
1 parent eb7aaad commit d885e9d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
11 changes: 11 additions & 0 deletions fastybird_fb_bus_connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ def write_property(self, property_item: Union[DevicePropertyEntity, ChannelPrope
isinstance(value_to_write, (str, int, float, bool, datetime, ButtonPayload, SwitchPayload))
or value_to_write is None
):
if (
isinstance(value_to_write, SwitchPayload)
and register_record.data_type == DataType.SWITCH
and value_to_write == SwitchPayload.TOGGLE
):
if register_record.actual_value == SwitchPayload.ON:
value_to_write = SwitchPayload.OFF

else:
value_to_write = SwitchPayload.ON

self.__registers_registry.set_expected_value(register=register_record, value=value_to_write)

return
Expand Down
2 changes: 1 addition & 1 deletion fastybird_fb_bus_connector/events/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def __create_or_update_channel_properties(self, channel: ChannelEntity, register
"id": register.id,
"identifier": RegisterAttribute.STATE.value, # f"register_{(register.address + 1):02}",
"data_type": register.data_type,
"format": None,
"format": register.format,
"unit": None,
"invalid": None,
"queryable": register.queryable,
Expand Down
32 changes: 32 additions & 0 deletions fastybird_fb_bus_connector/registry/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,38 @@ def data_type(self) -> DataType:

# -----------------------------------------------------------------------------

@property
def format(
self,
) -> Union[
Tuple[Optional[int], Optional[int]],
Tuple[Optional[float], Optional[float]],
List[Union[str, Tuple[str, Optional[str], Optional[str]]]],
None,
]:
"""Attribute register value format"""
if self.data_type == DataType.SWITCH:
return [
SwitchPayload.ON.value,
SwitchPayload.OFF.value,
SwitchPayload.TOGGLE.value,
]

if self.data_type == DataType.BUTTON:
return [
ButtonPayload.PRESSED.value,
ButtonPayload.RELEASED.value,
ButtonPayload.CLICKED.value,
ButtonPayload.DOUBLE_CLICKED.value,
ButtonPayload.TRIPLE_CLICKED.value,
ButtonPayload.LONG_CLICKED.value,
ButtonPayload.EXTRA_LONG_CLICKED.value,
]

return None

# -----------------------------------------------------------------------------

@property
def data_type_size(self) -> int:
"""Record data type bytes size"""
Expand Down

0 comments on commit d885e9d

Please sign in to comment.