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

Add Broadlink switch support #4528

Closed
wants to merge 5 commits into from

Conversation

skyval
Copy link

@skyval skyval commented Nov 22, 2016

Description:
This Broadlink RM switch platform allow to you control Broadlink RM2 Pro and RM mini IR+RF devices

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

Example entry for configuration.yaml (if applicable):

switch:
  platform: broadlink
  host: IP_ADDRESS
  mac: 'MAC_ADDRESS'
  switches:
    reciever:
      command_on: 'switch_packet on'
      command_off: 'switch_packet off'

Configuration variables:

  • host (Required): The hostname/IP address to connect to.
  • mac (Required): Device mac address.
  • switches (Required): The array that contains all switches.
    • identifier (Required): Name of the command switch as slug. Multiple entries are possible.
      • command_on (Required): Base64 encoded packet from RM device to take for on.
      • command_off (Required): Base64 encoded packet from RM device to take for off.
      • optimistic (Optional): Default true: Flag that defines if switch works in optimistic mode.
      • friendly_name (Optional): The name used to display the switch in the frontend.

How to obtain IR/RF packet while in learning mode?
Run: hass --script broadlink --ip YOUR_DEVICE_IP --mac YOUR_DEVICE_MAC

Checklist:

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.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@Danielhiversen
Copy link
Member

Danielhiversen commented Nov 22, 2016

Great work.
Tested and works here.

You should add the files to .coveragerc: https://github.com/home-assistant/home-assistant/blob/dev/.coveragerc

@ezar
Copy link

ezar commented Nov 22, 2016

Please commit!

@skyval
Copy link
Author

skyval commented Nov 22, 2016

I'm already committed?

@Landrash
Copy link
Contributor

@ezar Nagging the author won't help. Pull request are reviewed and approved as soon as possible and out of @skyval's hands until then.

@Danielhiversen
Copy link
Member

@dony71 Yes

@zenitraM
Copy link

Hey, I want to implement support for Broadlink SP2/Mini smart plugs, which work pretty similar to RM2/3 except for the on/off/status read packets:
https://github.com/mjg59/python-broadlink/blob/master/broadlink/__init__.py#L239-L259

How do you think it would make sense to join those with this module? maybe splitting the broadlink common functions somewhere and make the smartplug support to a separate component? or adding a type switch to identify that it is a broadlink RF switch or a smartplug?

@skyval
Copy link
Author

skyval commented Nov 24, 2016

Hi,
I thing will be best choice to join those module.
I thought to do this functionality but I preferred to wait for the python-broadlink library to develop a little more. Then we can upload it on pypi and use it.
But this is open source project and feel free to do what you want :)
I did not because when I started there was no support Sp2/Mini, but if you want I can help with whatever is needed. However, I think the best way is we should probably be heading to fix python library first.

@bagobones
Copy link

I noticed there is work being done for harmony devices and a remote component is being created instead of a switch #4254

@zenitraM
Copy link

I've fixed python-broadlink for Python3 on mjg59/python-broadlink#20, i'll try to give a shot to integrating it.

if auth:
broadlink.send_data(base64.b64decode(self._command_off))


Copy link
Member

Choose a reason for hiding this comment

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

I think that @balloob will require that the Broadlink class is exported to an external library before this can be merged

Copy link
Author

Choose a reason for hiding this comment

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

Where is the right place to export class?

broadlink.send_data(base64.b64decode(self._command_on))

def turn_off(self, **kwargs):
"""Turn the device off."""
Copy link
Member

Choose a reason for hiding this comment

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

To avoid duplicating the code from turn_on, you can extract the code to a _send_data function that both the turn_on and turn_off function can use

class BroadlinkSwitch(SwitchDevice):
"""Representation of an Broadlink switch."""

def __init__(self, hass, object_id, friendly_name, command_on,
Copy link
Member

Choose a reason for hiding this comment

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

object_id is unused

def turn_on(self, **kwargs):
"""Turn the device on."""
import base64
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.

Should only be sat to True if successfully sent the signal, so should be moved to end of function

_LOGGER.info('Broadlink connection successfully established.')

except ValueError as error:
_LOGGER.error(error)
Copy link
Member

Choose a reason for hiding this comment

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

Add return statement here?

except ValueError as error:
_LOGGER.error(error)

auth = broadlink.auth()
Copy link
Member

Choose a reason for hiding this comment

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

duplicate of line 108 ?

_LOGGER = logging.getLogger(__name__)

SWITCH_SCHEMA = vol.Schema({
vol.Optional(CONF_COMMAND_OFF, default='true'): 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.

Why default to 'true'?

self._mac_addr = mac_addr

@staticmethod
def _switch(command):
Copy link
Member

Choose a reason for hiding this comment

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

Unused ?

"""Return if the state is based on assumptions."""
return self._optimistic

def update(self):
Copy link
Member

Choose a reason for hiding this comment

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

Not necessary, so should be removed

vol.Optional(CONF_COMMAND_OFF, default=None): cv.string,
vol.Optional(CONF_COMMAND_ON, default=None): cv.string,
vol.Optional(CONF_FRIENDLY_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_OPTIMISTIC, default=True): cv.boolean,
Copy link
Member

Choose a reason for hiding this comment

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

Why would optimistic be configurable ? We should either be optimistic or not based on how the device reports their state ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I have one of these devices, and to my knowledge it doesn't keep state. Maybe this component was adapted from one that does?


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup Broadlink switches."""
import binascii
Copy link
Member

Choose a reason for hiding this comment

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

This is a Python built-in module, please add import to the top.


def _sendpacket(self, packet):
"""Send packet to device."""
import base64
Copy link
Member

Choose a reason for hiding this comment

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

Please import at the top.

for object_id, device_config in devices.items():

switches.append(
BroadlinkSwitch(
Copy link
Member

Choose a reason for hiding this comment

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

Possible to add an auth check before each device gets added and report that back to the user if they added some wrong info ?

_LOGGER.error(error)


class Broadlink():
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 this into a third party library. Home Assistant should not contain any protocol specific info.

@@ -0,0 +1,238 @@
"""Script to enter Broadlink RM devices in learning mode."""
Copy link
Member

Choose a reason for hiding this comment

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

This should also be part of your library. You can register a service in your switch platform to make this functionality available through Home Assistant.

@@ -40,10 +40,10 @@ apcaccess==0.0.4
apns2==0.1.1

# homeassistant.components.sun
astral==1.3.2
astral==1.2
Copy link
Member

Choose a reason for hiding this comment

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

You should not change these values.

@balloob balloob self-assigned this Dec 2, 2016
@technicalpickles
Copy link
Contributor

#4834 builds on this, and was released in 0.35

@home-assistant home-assistant locked and limited conversation to collaborators Mar 30, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants