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 RainMachine switch platform #8827

Merged
merged 11 commits into from
Aug 8, 2017
Merged

Add RainMachine switch platform #8827

merged 11 commits into from
Aug 8, 2017

Conversation

bachya
Copy link
Contributor

@bachya bachya commented Aug 4, 2017

Description:

Adds support for RainMachine sprinkler controllers (via the Switch component).

Related issue (if applicable):

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

Example entry for configuration.yaml (if applicable):

switch:
  platform: rainmachine
  ip_address: 192.168.1.100
  password: my_password_123

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.

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.

Overall looks good. Some minor comments.

.coveragerc Outdated
@@ -143,6 +143,9 @@ omit =
homeassistant/components/rachio.py
homeassistant/components/*/rachio.py

homeassistant/components/rainmachine.py
Copy link
Member

Choose a reason for hiding this comment

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

Don't add this line since you don't add a component only a platform.

.coveragerc Outdated
@@ -143,6 +143,9 @@ omit =
homeassistant/components/rachio.py
homeassistant/components/*/rachio.py

homeassistant/components/rainmachine.py
homeassistant/components/*/rainmachine.py
Copy link
Member

Choose a reason for hiding this comment

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

Replace this with homeassistant/components/switch/rainmachine.py and move the line to the switch group in alphabetical order.



@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
Copy link
Member

Choose a reason for hiding this comment

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

Move this function above the class definitions.

_LOGGER.error('Neither IP address nor email address given')
return False
except rm.exceptions.HTTPError as exec_info:
_LOGGER.error('HTTP error during authentication: %s', str(exec_info))
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 have to convert exec_info into a string. The base Exception class in Python defines __str__.


async_add_devices(entities)
except rm.exceptions.HTTPError as exec_info:
_LOGGER.error('Request failed: %s', str(exec_info))
Copy link
Member

Choose a reason for hiding this comment

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

See above.


def __init__(self, client, zone_json, **kwargs):
"""Initialize a RainMachine zone."""
super(RainMachineZone, self).__init__(client, zone_json, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

super().__init__(client, zone_json, **kwargs)

except exceptions.HTTPError as exc_info:
_LOGGER.error('Unable to turn off zone "%s"',
self.rainmachine_id)
_LOGGER.debug(str(exc_info))
Copy link
Member

Choose a reason for hiding this comment

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

Remove str conversion.

except exceptions.HTTPError as exc_info:
_LOGGER.error('Unable to turn on zone "%s"',
self.rainmachine_id)
_LOGGER.debug(str(exc_info))
Copy link
Member

Choose a reason for hiding this comment

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

See above.

MIN_SCAN_TIME_FORCED = timedelta(milliseconds=100)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_IP_ADDRESS):
Copy link
Member

@MartinHjelmare MartinHjelmare Aug 7, 2017

Choose a reason for hiding this comment

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

Require at least one of the auth keys and make them exclusive, something like this:

PLATFORM_SCHEMA = vol.Schema(vol.All(
    cv.has_at_least_one_key(CONF_IP_ADDRESS, CONF_EMAIL),
    {
        vol.Required(CONF_PLATFORM): cv.string,
        vol.Optional(CONF_SCAN_INTERVAL): cv.time_period,
        vol.Exclusive(CONF_IP_ADDRESS, 'auth'): cv.string,
        vol.Exclusive(CONF_EMAIL, 'auth'): vol.Email,
        vol.Required(CONF_PASSWORD): cv.string,
        vol.Optional(CONF_ZONE_RUN_TIME, default=DEFAULT_ZONE_RUN_SECONDS): cv.positive_int,
        vol.Optional(CONF_HIDE_DISABLED_ENTITIES, default=True): cv.boolean
    }
), extra=vol.ALLOW_EXTRA)

Note that you can't extend the platform schema when you want to use a validator around the dict.

Copy link
Contributor Author

@bachya bachya Aug 7, 2017

Choose a reason for hiding this comment

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

This is great, but I'm confused on how to utilize it? Without any extra changes, I get exceptions about my ip_address and email_address variables (set in async_setup_platform()) being functions.

Never mind, got it. 😄

_LOGGER.debug('Configuring remote API...')
auth = rm.Authenticator.create_remote(email_address, password)
else:
_LOGGER.error('Neither IP address nor email address given')
Copy link
Member

Choose a reason for hiding this comment

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

If you add the more extensive platform validation schema you can remove this.

MIN_SCAN_TIME_REMOTE = timedelta(seconds=5)
MIN_SCAN_TIME_FORCED = timedelta(milliseconds=100)

PLATFORM_SCHEMA = vol.Schema(

Choose a reason for hiding this comment

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

redefinition of unused 'PLATFORM_SCHEMA' from line 10

@bachya
Copy link
Contributor Author

bachya commented Aug 7, 2017

Thanks, @MartinHjelmare – I've made your requested changes.

@MartinHjelmare
Copy link
Member

MartinHjelmare commented Aug 7, 2017

I think the branch got jacked. 😜
If you know your git wushu, you can rebase and remove the stray commits that shouldn't be there. Otherwise there's always:
https://xkcd.com/1597/

Edit: Take for habit to work in a separate feature branch of your own, not directly on dev.

@bachya
Copy link
Contributor Author

bachya commented Aug 7, 2017

@MartinHjelmare: That's what I had done prior to this PR; not sure what happened. I'll fix shortly.

Edit: Alright, we should be in business now (once CI passes). 👍

super(RainMachineZone, self).__init__(client, zone_json, **kwargs)
self._run_time = kwargs.get(CONF_ZONE_RUN_TIME)
super().__init__(client, zone_json, **kwargs)
self._run_time = kwargs.get(zone_run_time)
Copy link
Member

Choose a reason for hiding this comment

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

The argument is already available now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is already pushed:

screen shot 2017-08-07 at 1 26 53 pm

Are you not seeing that? Wondering if I messed up the branch further...

Copy link
Member

Choose a reason for hiding this comment

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

That looks like what I have. But I think this line should be changed to this:

self._run_time = zone_run_time

Cause zone_run_time is now available as a positional argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, great point. Updating shortly.

@bachya
Copy link
Contributor Author

bachya commented Aug 7, 2017

@MartinHjelmare: The lint CI step is failing with this error:

lint runtests: commands[1] | pylint homeassistant
************* Module homeassistant.components.switch.rainmachine
E: 41,21: No value for argument 'v' in function call (no-value-for-parameter)
ERROR: InvocationError: '/home/travis/build/home-assistant/home-assistant/.tox/lint/bin/pylint homeassistant'

That isn't anywhere in my code; guessing it might be in the platform schema definition. Any advice for me?

@bachya
Copy link
Contributor Author

bachya commented Aug 8, 2017

Found this: pylint-dev/pylint#818key point:

Yeah, this happens because we don't have currently a proper understanding of decorators, which voluptuous seems to use, but it's on our road map. Unfortunately, the only solution that I have for now is to recommend you disabling the error locally or in pylintrc.

Going to tell pylint to ignore that issue, then re-push. Holding tight...

Edit: Victory!

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.

Nice!

@MartinHjelmare MartinHjelmare merged commit 289c88f into home-assistant:dev Aug 8, 2017
@fabaff fabaff mentioned this pull request Aug 12, 2017
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
* Add RainMachine switch platform

* Updated requirements_all.txt

* Cleaning up CI and coverage results

* Small update to deal with older pylint

* Fixed small indentation-based error

* Added some more defensive try/except logic around calls

* I'm not a fan of importing a library multiple times :)

* Making PR-requested changes

* Fixed ref to positional parameter

* Attempting to fix broken linting

* Ignoring no-value-for-parameter pylint error
@home-assistant home-assistant locked and limited conversation to collaborators Dec 11, 2017
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.

5 participants