Skip to content

Qwikswitch async & updates#12641

Merged
kellerza merged 2 commits intohome-assistant:devfrom
kellerza:qwikswitch-update
Mar 25, 2018
Merged

Qwikswitch async & updates#12641
kellerza merged 2 commits intohome-assistant:devfrom
kellerza:qwikswitch-update

Conversation

@kellerza
Copy link
Copy Markdown
Member

@kellerza kellerza commented Feb 24, 2018

Description:

Added async to qwikswitch library & HA component, but should hav no impact on user settings.

Old library used 2 dedicated threads for long poll & worker.

  • Qwikswitch async
  • pyqwikswitch 0.5
  • Qwikswitch brightness fixes
  • No more global vars --> hass.data

Example entry for configuration.yaml (if applicable):

qwikswitch:
  url: http://127.0.0.1:2020

Checklist:

  • The code change is tested and works locally.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • N/A New files were added to .coveragerc.

@kellerza
Copy link
Copy Markdown
Member Author

Merging today should give me 2 weeks in dev to test it out...

_LOGGER.error("Configure Qwikswitch component failed")
logging.getLogger(__name__).error(
"Configure Qwikswitch Light component failed")
return False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is checking this return value, just return.


add_devices(qwikswitch.QSUSB['light'])
add_devices(hass.data['qwikswitch']['light'])
return True
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

qsusb = QSUsb(url, _LOGGER, dimmer_adjust)
def callback_value_changed(qsdevices, key, new): \
# pylint: disable=unused-argument
"""Update entiry values based on device change."""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: entiry -> entity.

def callback_value_changed(qsdevices, key, new): \
# pylint: disable=unused-argument
"""Update entiry values based on device change."""
entity = hass.data[DOMAIN].get(key)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The component should not know about the entity instances. That's a concern of the platforms. You should probably refactor this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can eg use our dispatch helper to signal the entities to update state.

return self._value
return self._qsusb[self._id, 1] > 0

def turn_on(self, **kwargs):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have both turn_on and async_turn_on?

item[QSDATA][QS_NAME].lower().endswith(' switch')):
item[QSDATA][QS_NAME] = item[QSDATA][QS_NAME][:-7] # Remove switch
new_dev = QSSwitch(_id, qsusb)
hass.data[DOMAIN]['switch'].append(new_dev)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entities should not be stored in hass.data. It's ok to store an index referencing the entities though. Eg a map between entity_id and some other id pattern.

_LOGGER.error("Configure Qwikswitch component")
logging.getLogger(__name__).error(
"Configure Qwikswitch Switch component failed")
return False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is checking this return value.


add_devices(qwikswitch.QSUSB['switch'])
add_devices(hass.data['qwikswitch']['switch'])
return True
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.discovery import load_platform
from homeassistant.components.light import (
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bad pattern to import the Light entity class into the component. The platform specific entity classes should stay in the platforms.

If all platforms have the same logic regarding entity instantiation and you want to be DRY, you can eg instead create a common function used in setup_platform that takes the platform entity class as an argument to instantiate the entities. This function can be defined here in the component and be imported into the platforms.

"""Add lights from the main Qwikswitch component."""
if discovery_info is None:
_LOGGER.error("Configure Qwikswitch component failed")
logging.getLogger(__name__).error(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to log this? It will only happen if a user by mistake keeps a platform section for qwikswitch in the config under the light component.

@kellerza
Copy link
Copy Markdown
Member Author

Thanks for the review @MartinHjelmare - will use for the next PR

Merged since it was open for a month and I would like to do some other changes... See lots of the things you mention is really carried over from the first version (my first PR to the project 😄) Before discovery, hass.data, dispatchers, async etc,- things have come a long way!

@kellerza kellerza mentioned this pull request Mar 28, 2018
7 tasks
@balloob balloob mentioned this pull request Apr 13, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants