Skip to content

update broadlink.py to add support for MP1 switch#9222

Merged
balloob merged 11 commits into
home-assistant:devfrom
giangvo:mp1-switch
Sep 14, 2017
Merged

update broadlink.py to add support for MP1 switch#9222
balloob merged 11 commits into
home-assistant:devfrom
giangvo:mp1-switch

Conversation

@giangvo
Copy link
Copy Markdown
Contributor

@giangvo giangvo commented Aug 30, 2017

Description:

  • Update switch/broadlink.py to add support for MP1 switch
  • I tested it with my MP1

Related issue (if applicable): fixes #

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

- platform: broadlink
  host: 192.168.0.x
  mac: xx:xx:xx:xx:xx:xx
  type: mp1
  friendly_name: 'MP 1'
  slots:
    # friendly name of slots - optional
    # if not set, slot name will be switch's friendly_name + 'slot {slot_index}'. e.g 'MP 1 slot 1'
    slot_1: 'TV slot'
    slot_2: 'Xbox slot'
    slot_3: 'Fan slot'
    slot_4: 'Speaker slot'

Checklist:

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

@homeassistant
Copy link
Copy Markdown
Contributor

Hello @giangvo,

When attempting to inspect the commits of your pull request for CLA signature status among all authors we encountered commit(s) which were not linked to a GitHub account, thus not allowing us to determine their status(es).

The commits that are missing a linked GitHub account are the following:

Unfortunately, we are unable to accept this pull request until this situation is corrected.

Here are your options:

  1. If you had an email address set for the commit that simply wasn't linked to your GitHub account you can link that email now and it will retroactively apply to your commits. The simplest way to do this is to click the link to one of the above commits and look for a blue question mark in a blue circle in the top left. Hovering over that bubble will show you what email address you used. Clicking on that button will take you to your email address settings on GitHub. Just add the email address on that page and you're all set. GitHub has more information about this option in their help center.

  2. If you didn't use an email address at all, it was an invalid email, or it's one you can't link to your GitHub, you will need to change the authorship information of the commit and your global Git settings so this doesn't happen again going forward. GitHub provides some great instructions on how to change your authorship information in their help center.

    • If you only made a single commit you should be able to run
      git commit --amend --author="Author Name <email@address.com>"
      
      (substituting Author Name and email@address.com for your actual information) to set the authorship information.
    • If you made more than one commit and the commit with the missing authorship information is not the most recent one you have two options:
      1. You can re-create all commits missing authorship information. This is going to be the easiest solution for developers that aren't extremely confident in their Git and command line skills.
      2. You can use this script that GitHub provides to rewrite history. Please note: this should be used only if you are very confident in your abilities and understand its impacts.
    • Whichever method you choose, I will come by to re-check the pull request once you push the fixes to this branch.

We apologize for this inconvenience, especially since it usually bites new contributors to Home Assistant. We hope you understand the need for us to protect ourselves and the great community we all have built legally. The best thing to come out of this is that you only need to fix this once and it benefits the entire Home Assistant and GitHub community.

Thanks, I look forward to checking this PR again soon! ❤️

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

no newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

trailing whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

blank line contains whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

trailing whitespace

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

missing whitespace after ','

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @giangvo,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @giangvo,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

class BroadlinkMP1Switch(BroadlinkRMSwitch):
"""Representation of an Broadlink switch."""

def __init__(self, friendly_name, device, slot):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

IndentationError: unexpected indent
indentation is not a multiple of four
unexpected indentation

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @giangvo,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

self._state = state


class BroadlinkMP1Switch(BroadlinkRMSwitch):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Add new BroadlinkMP1Switch class to control 4 slots

class BroadlinkMP1Switch(BroadlinkRMSwitch):
"""Representation of an Broadlink switch."""

def __init__(self, friendly_name, device, slot):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

slot param is used to define slot to control

if switch_type == "mp1-3":
switches = [BroadlinkMP1Switch(friendly_name, broadlink_device, 3)]
if switch_type == "mp1-4":
switches = [BroadlinkMP1Switch(friendly_name, broadlink_device, 4)]
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.

Does not all mp1 switches have 4 slots?
Then I think we always should add all 4 slots.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yup. I updated the code to add all 4 slots and changed device type to mp1 only. New config should be like this

- platform: broadlink
  host: 192.168.0.x
  mac: xx:xx:xx:xx:xx:xx
  type: mp1
  friendly_name: 'MP 1'
  slots:
    # friendly name of slots - optional
    # if not set, slot name will be switch's friendly_name + 'slot {slot_index}'. e.g 'MP 1 slot 1'
    slot_1: 'TV slot'
    slot_2: 'Xbox slot'
    slot_3: 'Fan slot'
    slot_4: 'Speaker slot'


def _update(self, retry=2):
try:
states = self._device.check_power()
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.

I think we here will ask for the same data 4 times (once for each slot).
You should make a data object that you can pass to the init function instead of the device object. The data object should handle the check_power call.
Here is an example: https://github.com/home-assistant/home-assistant/blob/38071501b421627dadbceb4bbdced195ac4ec620/homeassistant/components/switch/digitalloggers.py#L137

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated

@Throttle(TIME_BETWEEN_UPDATES)
def update(self):
"""Fetch new state data for this device."""
self._update();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

statement ends with a semicolon



class BroadlinkMP1Switch(object):
"""Representation of a slot of Broadlink switch - Using for fetch states of all slots"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (92 > 79 characters)

self._command_on = 1
self._command_off = 0
self._slot = slot
self._parent_device = parent_device;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

statement ends with a semicolon

broadlink_device = broadlink.mp1((ip_addr, 80), mac_addr)
parent_device = BroadlinkMP1Switch(broadlink_device)
switches = [BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 1), broadlink_device, 1, parent_device), BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 2), broadlink_device, 2, parent_device),
BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 3), broadlink_device, 3, parent_device), BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 4), broadlink_device, 4, parent_device)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent
line too long (195 > 79 characters)

elif switch_type in MP1_TYPES:
broadlink_device = broadlink.mp1((ip_addr, 80), mac_addr)
parent_device = BroadlinkMP1Switch(broadlink_device)
switches = [BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 1), broadlink_device, 1, parent_device), BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 2), broadlink_device, 2, parent_device),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (203 > 79 characters)
trailing whitespace

from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (
CONF_FRIENDLY_NAME, CONF_SWITCHES, CONF_COMMAND_OFF, CONF_COMMAND_ON,
CONF_FRIENDLY_NAME, CONF_SWITCHES, CONF_SLOTS, CONF_COMMAND_OFF, CONF_COMMAND_ON,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (85 > 79 characters)

switches = [BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 1), broadlink_device, 1, parent_device),
BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 2), broadlink_device, 2, parent_device),
BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 3), broadlink_device, 3, parent_device),
BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 4), broadlink_device, 4, parent_device)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent
line too long (103 > 79 characters)

parent_device = BroadlinkMP1Switch(broadlink_device)
switches = [BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 1), broadlink_device, 1, parent_device),
BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 2), broadlink_device, 2, parent_device),
BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 3), broadlink_device, 3, parent_device),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent
line too long (103 > 79 characters)

broadlink_device = broadlink.mp1((ip_addr, 80), mac_addr)
parent_device = BroadlinkMP1Switch(broadlink_device)
switches = [BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 1), broadlink_device, 1, parent_device),
BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 2), broadlink_device, 2, parent_device),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent
line too long (103 > 79 characters)

elif switch_type in MP1_TYPES:
broadlink_device = broadlink.mp1((ip_addr, 80), mac_addr)
parent_device = BroadlinkMP1Switch(broadlink_device)
switches = [BroadlinkMP1Slot(_get_mp1_slot_name(friendly_name, 1), broadlink_device, 1, parent_device),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (111 > 79 characters)

vol.Optional(CONF_FRIENDLY_NAME): cv.string,
})

MP1_SWITCH_SLOT_SCHEMA = vol.Schema({
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

slot's schema to define slot's friendly names

@giangvo
Copy link
Copy Markdown
Contributor Author

giangvo commented Aug 31, 2017

@Danielhiversen , thanks for your review. Should I merge this PR now? Could u also advise me how to update document after merging this?
Thanks,

Copy link
Copy Markdown
Member

@pvizeli pvizeli left a comment

Choose a reason for hiding this comment

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

I know that was before but rename all friendly_name stuff to name only. Otherwise we compatition the real friendly name from core and it will be not very userfriendly.

vol.Optional('slot_2', default=None): cv.string,
vol.Optional('slot_3', default=None): cv.string,
vol.Optional('slot_4', default=None): cv.string
})
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 not set default None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed. btw, could u explain why we should not set default None here?

@giangvo
Copy link
Copy Markdown
Contributor Author

giangvo commented Aug 31, 2017

@pvizeli , thanks for your review. Could you please explain it a bit

rename all friendly_name stuff to name only

Changing friendly_name to name so we also need to update configuration? Will it break current configuration of users?

Update: Do you mean same change as in this one https://github.com/home-assistant/home-assistant/pull/4343/files ? If yes, I think it should be in different PR to refactor all broadlink switch types.

@giangvo
Copy link
Copy Markdown
Contributor Author

giangvo commented Sep 5, 2017

@pvizeli could u please approve this PR?
Thanks

@home-assistant home-assistant deleted a comment from houndci-bot Sep 5, 2017
Comment thread homeassistant/const.py Outdated
CONF_STATE = 'state'
CONF_STRUCTURE = 'structure'
CONF_SWITCHES = 'switches'
CONF_SLOTS = 'slots'
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.

Let's keep this in the local file instead of adding it to the general constants.


def get_outlet_status(self, slot):
"""Get status of outlet from cached status list."""
return self._states['s' + str(slot)]
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.

For string concatenation, please use the format function:

return self._states['s{}'.format(slot)]

if retry < 1:
_LOGGER.error(error)
return
if not self._auth():
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.

Did you try to get this kind of logic added to the broadlink lib that is being used? It would be more suitable to be added there.

Copy link
Copy Markdown
Contributor Author

@giangvo giangvo Sep 13, 2017

Choose a reason for hiding this comment

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

I would do it in other PR and let people who know more about broadlink lib do it. I am not familiar with it :(

_LOGGER.error("Failed to send packet to device")

def _get_mp1_slot_name(switch_friendly_name, slot):
if not slots['slot_' + str(slot)]:
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.

Use string format()

"""Set up Broadlink switches."""
import broadlink
devices = config.get(CONF_SWITCHES, {})
slots = config.get('slots', {})
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.

No need to specify default, the platform schema provides a default.

@giangvo
Copy link
Copy Markdown
Contributor Author

giangvo commented Sep 13, 2017

@balloob , please review again. I updated code to use .format
Thanks for your review.

@balloob
Copy link
Copy Markdown
Member

balloob commented Sep 14, 2017

Awesome! Thanks! Congratulations on your first merged PR! 💃 🐬 👍

@balloob balloob merged commit 3430c1c into home-assistant:dev Sep 14, 2017
@giangvo
Copy link
Copy Markdown
Contributor Author

giangvo commented Sep 14, 2017

Thanks @balloob . Could u please advise how should I update the document?
Should I fork this https://github.com/home-assistant/home-assistant.github.io and make a PR?

@balloob
Copy link
Copy Markdown
Member

balloob commented Sep 14, 2017

Yes. Make sure you branch off next and create a PR against the next branch.

@giangvo
Copy link
Copy Markdown
Contributor Author

giangvo commented Sep 14, 2017

Thanks

@giangvo
Copy link
Copy Markdown
Contributor Author

giangvo commented Sep 14, 2017

I created PR for updating document here: home-assistant/home-assistant.io#3367 . Please review @balloob
Thanks

@balloob balloob mentioned this pull request Sep 22, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Mar 3, 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.

7 participants