-
-
Notifications
You must be signed in to change notification settings - Fork 38.1k
Style change follow up #13707
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
Style change follow up #13707
Changes from 3 commits
03eb379
5243038
8703cb2
2b05f1e
cb0aad4
dfa7191
99b67df
2f7ce59
2793753
d99f9df
0af42de
e0eabb1
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 |
|---|---|---|
|
|
@@ -77,72 +77,61 @@ def get_accessory(hass, state, aid, config): | |
| _LOGGER.warning('The entitiy "%s" is not supported, since it ' | ||
| 'generates an invalid aid, please change it.', | ||
| state.entity_id) | ||
| return None | ||
| return | ||
|
|
||
| if state.domain == 'sensor': | ||
| unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) | ||
| if unit == TEMP_CELSIUS or unit == TEMP_FAHRENHEIT: | ||
| _LOGGER.debug('Add "%s" as "%s"', | ||
| state.entity_id, 'TemperatureSensor') | ||
| return TYPES['TemperatureSensor'](hass, state.entity_id, | ||
| state.name, aid=aid) | ||
| elif unit == '%': | ||
| _LOGGER.debug('Add "%s" as %s"', | ||
| state.entity_id, 'HumiditySensor') | ||
| return TYPES['HumiditySensor'](hass, state.entity_id, state.name, | ||
| aid=aid) | ||
|
|
||
| elif state.domain == 'binary_sensor' or state.domain == 'device_tracker': | ||
| _LOGGER.debug('Add "%s" as "%s"', state.entity_id, 'BinarySensor') | ||
| return TYPES['BinarySensor'](hass, state.entity_id, | ||
| state.name, aid=aid) | ||
| a_type = None | ||
| a_kwargs = {'aid': aid} | ||
|
|
||
| elif state.domain == 'cover': | ||
| # Only add covers that support set_cover_position | ||
| features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) | ||
| if features & SUPPORT_SET_POSITION: | ||
| _LOGGER.debug('Add "%s" as "%s"', | ||
| state.entity_id, 'WindowCovering') | ||
| return TYPES['WindowCovering'](hass, state.entity_id, state.name, | ||
| aid=aid) | ||
| if state.domain == 'alarm_control_panel': | ||
|
Member
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. Insert a_type = None |
||
| a_type = 'SecuritySystem' | ||
| a_kwargs['alarm_code'] = config.get(ATTR_CODE) | ||
|
|
||
| elif state.domain == 'alarm_control_panel': | ||
| _LOGGER.debug('Add "%s" as "%s"', state.entity_id, 'SecuritySystem') | ||
| return TYPES['SecuritySystem'](hass, state.entity_id, state.name, | ||
| alarm_code=config.get(ATTR_CODE), | ||
| aid=aid) | ||
| elif state.domain == 'binary_sensor' or state.domain == 'device_tracker': | ||
| a_type = 'BinarySensor' | ||
|
|
||
| elif state.domain == 'climate': | ||
| features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) | ||
| support_temp_range = SUPPORT_TARGET_TEMPERATURE_LOW | \ | ||
| SUPPORT_TARGET_TEMPERATURE_HIGH | ||
| # Check if climate device supports auto mode | ||
| support_auto = bool(features & support_temp_range) | ||
| a_type = 'Thermostat' | ||
| a_kwargs['support_auto'] = bool(features & support_temp_range) | ||
|
|
||
| _LOGGER.debug('Add "%s" as "%s"', state.entity_id, 'Thermostat') | ||
| return TYPES['Thermostat'](hass, state.entity_id, | ||
| state.name, support_auto, aid=aid) | ||
| elif state.domain == 'cover': | ||
| # Only add covers that support set_cover_position | ||
| features = state.attributes.get(ATTR_SUPPORTED_FEATURES, 0) | ||
| if features & SUPPORT_SET_POSITION: | ||
| a_type = 'WindowCovering' | ||
|
|
||
| elif state.domain == 'light': | ||
| _LOGGER.debug('Add "%s" as "%s"', state.entity_id, 'Light') | ||
| return TYPES['Light'](hass, state.entity_id, state.name, aid=aid) | ||
| a_type = 'Light' | ||
|
|
||
| elif state.domain == 'lock': | ||
| return TYPES['Lock'](hass, state.entity_id, state.name, aid=aid) | ||
| a_type = 'Lock' | ||
|
|
||
| elif state.domain == 'sensor': | ||
| unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) | ||
| if unit == TEMP_CELSIUS or unit == TEMP_FAHRENHEIT: | ||
| a_type = 'TemperatureSensor' | ||
| elif unit == '%': | ||
| a_type = 'HumiditySensor' | ||
|
|
||
| elif state.domain == 'switch' or state.domain == 'remote' \ | ||
| or state.domain == 'input_boolean' or state.domain == 'script': | ||
| _LOGGER.debug('Add "%s" as "%s"', state.entity_id, 'Switch') | ||
| return TYPES['Switch'](hass, state.entity_id, state.name, aid=aid) | ||
| a_type = 'Switch' | ||
|
|
||
| if a_type is None: | ||
| return | ||
|
|
||
| return None | ||
| _LOGGER.debug('Add "%s" as "%s"', state.entity_id, a_type) | ||
| return TYPES[a_type](hass, state.name, state.entity_id, **a_kwargs) | ||
|
|
||
|
|
||
| def generate_aid(entity_id): | ||
| """Generate accessory aid with zlib adler32.""" | ||
| aid = adler32(entity_id.encode('utf-8')) | ||
| if aid == 0 or aid == 1: | ||
| return None | ||
| return | ||
| return aid | ||
|
|
||
|
|
||
|
|
@@ -151,7 +140,7 @@ class HomeKit(): | |
|
|
||
| def __init__(self, hass, port, entity_filter, entity_config): | ||
| """Initialize a HomeKit object.""" | ||
| self._hass = hass | ||
| self.hass = hass | ||
| self._port = port | ||
| self._filter = entity_filter | ||
| self._config = entity_config | ||
|
|
@@ -164,11 +153,11 @@ def setup(self): | |
| """Setup bridge and accessory driver.""" | ||
| from .accessories import HomeBridge, HomeDriver | ||
|
|
||
| self._hass.bus.async_listen_once( | ||
| self.hass.bus.async_listen_once( | ||
| EVENT_HOMEASSISTANT_STOP, self.stop) | ||
|
|
||
| path = self._hass.config.path(HOMEKIT_FILE) | ||
| self.bridge = HomeBridge(self._hass) | ||
| path = self.hass.config.path(HOMEKIT_FILE) | ||
| self.bridge = HomeBridge(self.hass) | ||
| self.driver = HomeDriver(self.bridge, self._port, get_local_ip(), path) | ||
|
|
||
| def add_bridge_accessory(self, state): | ||
|
|
@@ -177,7 +166,7 @@ def add_bridge_accessory(self, state): | |
| return | ||
| aid = generate_aid(state.entity_id) | ||
| conf = self._config.pop(state.entity_id, {}) | ||
| acc = get_accessory(self._hass, state, aid, conf) | ||
| acc = get_accessory(self.hass, state, aid, conf) | ||
| if acc is not None: | ||
| self.bridge.add_accessory(acc) | ||
|
|
||
|
|
@@ -192,12 +181,12 @@ def start(self, *args): | |
| type_covers, type_lights, type_locks, type_security_systems, | ||
| type_sensors, type_switches, type_thermostats) | ||
|
|
||
| for state in self._hass.states.all(): | ||
| for state in self.hass.states.all(): | ||
| self.add_bridge_accessory(state) | ||
| self.bridge.set_broker(self.driver) | ||
|
|
||
| if not self.bridge.paired: | ||
| show_setup_message(self.bridge, self._hass) | ||
| show_setup_message(self.hass, self.bridge) | ||
|
|
||
| _LOGGER.debug('Driver start') | ||
| self.driver.start() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,8 @@ | |
| from homeassistant.util import dt as dt_util | ||
|
|
||
| from .const import ( | ||
| DEBOUNCE_TIMEOUT, ACCESSORY_MODEL, ACCESSORY_NAME, BRIDGE_MODEL, | ||
| BRIDGE_NAME, MANUFACTURER, SERV_ACCESSORY_INFO, CHAR_MANUFACTURER, | ||
| DEBOUNCE_TIMEOUT, BRIDGE_MODEL, BRIDGE_NAME, MANUFACTURER, | ||
| SERV_ACCESSORY_INFO, CHAR_MANUFACTURER, | ||
| CHAR_MODEL, CHAR_NAME, CHAR_SERIAL_NUMBER) | ||
| from .util import ( | ||
| show_setup_message, dismiss_setup_message) | ||
|
|
@@ -85,34 +85,47 @@ def set_accessory_info(acc, name, model, manufacturer=MANUFACTURER, | |
| class HomeAccessory(Accessory): | ||
| """Adapter class for Accessory.""" | ||
|
|
||
| # pylint: disable=no-member | ||
|
|
||
| def __init__(self, name=ACCESSORY_NAME, model=ACCESSORY_MODEL, | ||
| category='OTHER', **kwargs): | ||
| def __init__(self, hass, name, entity_id, category='OTHER', **kwargs): | ||
| """Initialize a Accessory object.""" | ||
| super().__init__(name, **kwargs) | ||
|
Member
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. Is this **kwargs or the combination of config and aid that should be passed here?
Member
Author
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. I will change |
||
| set_accessory_info(self, name, model) | ||
| set_accessory_info(self, name, model=entity_id) | ||
| self.category = getattr(Category, category, Category.OTHER) | ||
| self.entity_id = entity_id | ||
| self.hass = hass | ||
|
|
||
| def _set_services(self): | ||
| add_preload_service(self, SERV_ACCESSORY_INFO) | ||
|
|
||
| def run(self): | ||
| """Method called by accessory after driver is started.""" | ||
| state = self.hass.states.get(self.entity_id) | ||
| self.update_state(new_state=state) | ||
| self.update_state_callback(new_state=state) | ||
| async_track_state_change( | ||
| self.hass, self.entity_id, self.update_state) | ||
| self.hass, self.entity_id, self.update_state_callback) | ||
|
|
||
| def update_state_callback(self, entity_id=None, old_state=None, | ||
| new_state=None): | ||
| """Callback from state change listener.""" | ||
| _LOGGER.debug('New_state: %s', new_state) | ||
| if new_state is None: | ||
| return | ||
| self.update_state(new_state) | ||
|
|
||
| def update_state(self, new_state): | ||
|
Member
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. 👍 This is a good idea and seem to simplify things as well |
||
| """Method called on state change to update HomeKit value. | ||
|
|
||
| Overridden by accessory types. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class HomeBridge(Bridge): | ||
| """Adapter class for Bridge.""" | ||
|
|
||
| def __init__(self, hass, name=BRIDGE_NAME, | ||
| model=BRIDGE_MODEL, **kwargs): | ||
| def __init__(self, hass, name=BRIDGE_NAME, **kwargs): | ||
| """Initialize a Bridge object.""" | ||
| super().__init__(name, **kwargs) | ||
| set_accessory_info(self, name, model) | ||
| set_accessory_info(self, name, model=BRIDGE_MODEL) | ||
| self.hass = hass | ||
|
|
||
| def _set_services(self): | ||
|
|
@@ -130,7 +143,7 @@ def add_paired_client(self, client_uuid, client_public): | |
| def remove_paired_client(self, client_uuid): | ||
| """Override super function to show setup message if unpaired.""" | ||
| super().remove_paired_client(client_uuid) | ||
| show_setup_message(self, self.hass) | ||
| show_setup_message(self.hass, self) | ||
|
|
||
|
|
||
| class HomeDriver(AccessoryDriver): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ | |
| CATEGORY_WINDOW_COVERING, SERV_WINDOW_COVERING, | ||
| CHAR_CURRENT_POSITION, CHAR_TARGET_POSITION, CHAR_POSITION_STATE) | ||
|
|
||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
|
|
@@ -20,13 +19,10 @@ class WindowCovering(HomeAccessory): | |
| The cover entity must support: set_cover_position. | ||
| """ | ||
|
|
||
| def __init__(self, hass, entity_id, display_name, **kwargs): | ||
| def __init__(self, hass, name, entity_id, **kwargs): | ||
| """Initialize a WindowCovering accessory object.""" | ||
| super().__init__(display_name, entity_id, | ||
| CATEGORY_WINDOW_COVERING, **kwargs) | ||
|
|
||
| self.hass = hass | ||
| self.entity_id = entity_id | ||
| super().__init__(hass, name, entity_id, | ||
| category=CATEGORY_WINDOW_COVERING, **kwargs) | ||
|
Member
Author
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. Personally I would really like to move these calls to the
Member
Author
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. On solution could be to use a decorator function for for the def super_init(category):
def category_wrapper(func_init):
def wrapper(self, hass, name, entity_id, **kwargs):
super(self.__class__, self).__init__(
hass, name, entity_id, category=CATEGORY_LIGHT, **kwargs)
func_init(self)
return wrapper
return category_wrapper
@TYPES.register('Light')
class Light(HomeAccessory):
@super_init(CATEGORY_LIGHT)
def __init__(self):Should we do that or stay at the current: def __init__(self, hass, name, entity_id, **kwargs):
super().__init__(hass, name, entity_id, category=CATEGORY_LIGHT, **kwargs)
Member
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. I don't like the decorator and not sure how that will interfere with You could do this (note: def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs, category=CATEGORY_LIGHT)but being explicit might give you some code editor hints, so I like this version more: def __init__(self, hass, name, entity_id, **kwargs):
super().__init__(hass, name, entity_id, **kwargs,
category=CATEGORY_LIGHT)
Member
Author
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 idea behind the decorator was to limit code redundancy between different types. You're right the it might be more difficult for editors to read, however I don't think that is that big of a deal since those attributes are already assigned in Regarding the interference with
Member
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. If you dont like the super rather add a bootstrap/postinit method in the child classes that you call from the parent’s init. Type could be a class var on the child
Member
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. In HomeAccessory you then have: def __init__(self, hass, name, entity_id, **kwargs):
"""Initialize a Accessory object."""
super().__init__(name, **kwargs)
set_accessory_info(self, name, model=entity_id)
self.entity_id = entity_id
self.hass = hass
cat = self.init_category()
self.category = getattr(Category, cat, Category.OTHER)with only
Member
Author
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. Extending on your idea what do you think about the following:
Member
Author
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.
|
||
|
|
||
| self.current_position = None | ||
| self.homekit_target = None | ||
|
|
@@ -56,11 +52,8 @@ def move_cover(self, value): | |
| self.hass.components.cover.set_cover_position( | ||
| value, self.entity_id) | ||
|
|
||
| def update_state(self, entity_id=None, old_state=None, new_state=None): | ||
| def update_state(self, new_state): | ||
| """Update cover position after state changed.""" | ||
| if new_state is None: | ||
| return | ||
|
|
||
| current_position = new_state.attributes.get(ATTR_CURRENT_POSITION) | ||
| if isinstance(current_position, int): | ||
| self.current_position = current_position | ||
|
|
||
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.
Although they are the same, I would suggest we keep
return Nonewhere we care / intend to use the return of a functionThere 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.
I have again added it to
get_accessoryandgenerate_aid. In cases where it is an abort condition (likeHomeKit.start(),HomeKit.stop(),HomeAccessory.update_state_callback()) I would prefer to stay with justreturn.