-
Notifications
You must be signed in to change notification settings - Fork 119
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
Added getter_callback to Characteristic #90
Changes from 8 commits
9d9a240
4148873
6c46754
e9b16f7
cece383
9750db2
7851399
781aad9
49a4e07
9bb6cf4
c1c4ade
4e7ae11
9b2875d
d2c628b
a9d4c4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,7 @@ class Characteristic: | |
""" | ||
|
||
__slots__ = ('display_name', 'type_id', 'properties', 'broker', | ||
'setter_callback', 'value') | ||
'value', 'getter_callback', 'setter_callback') | ||
|
||
def __init__(self, display_name, type_id, properties): | ||
"""Initialise with the given properties. | ||
|
@@ -98,8 +98,9 @@ def __init__(self, display_name, type_id, properties): | |
self.type_id = type_id | ||
self.properties = properties | ||
self.broker = None | ||
self.setter_callback = None | ||
self.value = self._get_default_value() | ||
self.getter_callback = None | ||
self.setter_callback = None | ||
|
||
def __repr__(self): | ||
"""Return the representation of the characteristic.""" | ||
|
@@ -114,6 +115,15 @@ def _get_default_value(self): | |
value = HAP_FORMAT_DEFAULTS[self.properties[PROP_FORMAT]] | ||
return self.to_valid_value(value) | ||
|
||
def get_value(self): | ||
""" | ||
This is to allow for calling `getter_callback` | ||
:return: Current Characteristic Value | ||
""" | ||
if self.getter_callback: | ||
self.value = self.to_valid_value(value=self.getter_callback()) | ||
return self.value | ||
|
||
def to_valid_value(self, value): | ||
"""Perform validation and conversion to valid value""" | ||
if self.properties.get(PROP_VALID_VALUES): | ||
|
@@ -168,6 +178,12 @@ def set_value(self, value, should_notify=True): | |
If not set_value will be aborted and an error message will be | ||
displayed. | ||
|
||
`Characteristic.setter_callback` | ||
You may also define a `setter_callback` on the `Characteristic`. | ||
This will be called with the value being set as the arg. | ||
|
||
.. seealso:: Characteristic.value | ||
|
||
:param value: The value to assign as this Characteristic's value. | ||
:type value: Depends on properties["Format"] | ||
|
||
|
@@ -184,12 +200,11 @@ def set_value(self, value, should_notify=True): | |
def client_update_value(self, value): | ||
"""Called from broker for value change in Home app. | ||
|
||
Change self.value to value and call callback. | ||
Call set_value and call callback. | ||
""" | ||
logger.debug('client_update_value: %s to %s', | ||
self.display_name, value) | ||
self.value = value | ||
self.notify() | ||
# Call setter_callback | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here. It would be quite confusion if a self._value = value
self.notify() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please revert the changes to |
||
if self.setter_callback: | ||
self.setter_callback(value) | ||
|
||
|
@@ -221,10 +236,10 @@ def to_HAP(self): | |
hap_rep.update({k: self.properties[k] for k in | ||
self.properties.keys() & PROP_NUMERIC}) | ||
elif self.properties[PROP_FORMAT] == HAP_FORMAT_STRING: | ||
if len(self.value) > 64: | ||
hap_rep[HAP_REPR_MAX_LEN] = min(len(self.value), 256) | ||
if len(self.get_value()) > 64: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, thanks for checking. Pushed changes. |
||
hap_rep[HAP_REPR_MAX_LEN] = min(len(self.get_value()), 256) | ||
if HAP_PERMISSION_READ in self.properties[PROP_PERMISSIONS]: | ||
hap_rep[HAP_REPR_VALUE] = self.value | ||
hap_rep[HAP_REPR_VALUE] = self.get_value() | ||
|
||
return hap_rep | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation stating that this calls the getter if any to update the .value