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

Switchmate #15535

Merged
merged 8 commits into from
Aug 29, 2018
Merged

Switchmate #15535

merged 8 commits into from
Aug 29, 2018

Conversation

Danielhiversen
Copy link
Member

@Danielhiversen Danielhiversen commented Jul 18, 2018

Description:

Swichmate is a bluetooth switch https://www.mysimplysmarthome.com/products/switchmate-switches/

There are only 3 lines with device specific code, so I decided to not split it to an external library.

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#6031

Example entry for configuration.yaml (if applicable):

switch:
  - platform: switchmate
    mac: 'cb:25:0b......'
    name: "Name of the device"

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

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

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

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New or updated dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_FRIENDLY_NAME, default=DEFAULT_NAME): cv.string,
Copy link
Member

Choose a reason for hiding this comment

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

Use CONF_NAME instead of CONF_FRIENDLY_NAME.

import bluepy
self._state = False
self._friendly_name = friendly_name
self._handle = 0x2e
Copy link
Member

Choose a reason for hiding this comment

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

Please extract the magic string into a constant.

self._device = bluepy.btle.Peripheral(self._mac,
bluepy.btle.ADDR_TYPE_RANDOM)
except bluepy.btle.BTLEException:
_LOGGER.error("Failed to setup switchmate", exc_info=True)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we want to add exc_info.

@property
def unique_id(self) -> str:
"""Return a unique, HASS-friendly identifier for this entity."""
return '{0}_{1}'.format(self._mac.replace(':', ''), self.entity_id)
Copy link
Member

Choose a reason for hiding this comment

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

Remove entity_id from unique_id.

"""Return the name of the switch."""
return self._friendly_name

@Throttle(MIN_TIME_BETWEEN_UPDATES)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to throttle this? If we want to adjust the scan interval, define the SCAN_INTERVAL constant at the module level.

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self) -> None:
"""Synchronize state with switch."""
self._state = self._device.readCharacteristic(self._handle) == b'\x00'
Copy link
Member

Choose a reason for hiding this comment

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

Please extract magic string b'\x00' into constant.

def turn_on(self, **kwargs) -> None:
"""Turn the switch on."""
self._device.writeCharacteristic(self._handle, b'\x00', True)
self._state = True
Copy link
Member

Choose a reason for hiding this comment

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

Preferably we shouldn't modify state here but let update do that, which will be called immediately after the call to this method. How is the responsiveness of the update call?

@property
def unique_id(self) -> str:
"""Return a unique, HASS-friendly identifier for this entity."""
return '{0}'.format(self._mac.replace(':', ''))
Copy link
Member

Choose a reason for hiding this comment

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

String formatting can be removed. Just do the replace.

Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Looks good. Note my comment about exception info though. We usually don't include all that in error logs. We settle for the exception argument.

@Danielhiversen
Copy link
Member Author

@MartinHjelmare : Can I merge this, or do you want someone else to take a look?

@MartinHjelmare
Copy link
Member

Yes, go ahead and merge. I've approved it.

@Danielhiversen Danielhiversen merged commit 18ba50b into dev Aug 29, 2018
@ghost ghost removed the in progress label Aug 29, 2018
@Danielhiversen Danielhiversen deleted the switchmate branch August 29, 2018 10:56
@balloob balloob mentioned this pull request Sep 17, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 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.

None yet

4 participants