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

Introduced Ring binary sensors and refactored Ring component #6520

Merged
merged 20 commits into from Mar 31, 2017

Conversation

tchellomello
Copy link
Contributor

@tchellomello tchellomello commented Mar 10, 2017

Description:

  • Introduced Ring binary sensors.
  • Added unittest for binary sensors and sensors
  • Bump ring_doorbell 3rd party Python module
  • Updated documentation

Example entry for configuration.yaml (if applicable):

#configuration.yaml
ring:
    username: !secret ring_username
    password: !secret ring_password

binary_sensor:
  - platform: ring
    monitored_conditions:
      - ding
      - motion

sensor:
  - platform: ring
    monitored_conditions:
      - battery
      - last_activity
      - last_ding
      - last_motion
      - volume

screenshot from 2017-03-27 02-10-28

HA Forum thread: https://community.home-assistant.io/t/ring-doorbell/7943

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

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][ex-requir]).
  • New dependencies are only imported inside functions that use them ([example][ex-import]).
  • 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.

- Added unittest for Ring binary_sensor.

- Bumped ring_doorbell 3rd party module.
@tchellomello tchellomello mentioned this pull request Mar 10, 2017
}

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): 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.

Instead of having each platform require credentials, please add a component to manage the credentials and share the Ring instance by adding it to hass.data (a dictionary intended for sharing exactly such objects)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a good idea @balloob. I will work on that.

from unittest import mock

import homeassistant.components.binary_sensor as sensor
from homeassistant.components.binary_sensor import ring

Choose a reason for hiding this comment

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

'homeassistant.components.binary_sensor.ring' imported but unused


ring = Ring(username, password)
if ring.is_connected:
RING = RingData(ring)
Copy link
Member

Choose a reason for hiding this comment

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

Use hass.data to store RING. Do not use globals.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Thanks for the heads.


def update(self):
"""Get the latest data and updates the state."""
self._data.check_alerts(cache=self._cache)
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 you need to pass in the cache? That is something that should be handled by the shared data class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The cache file is required for multiple sensors. It basically will save a pickle data file with the notification state shared by different sensors. The cache if required because it is shared on the object level and not on the account level which is shared by hass.data.

@@ -90,14 +65,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

class RingSensor(Entity):
"""A sensor implementation for Ring device."""

Choose a reason for hiding this comment

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

blank line contains whitespace

@tchellomello tchellomello changed the title Introduced Ring binary sensors Introduced Ring binary sensors and refactored Ring component Mar 27, 2017
@tchellomello
Copy link
Contributor Author

@balloob code update. Please let know what you think now.

I'm working to update the documentation to support the new sensors and how to configure it using the hub concept.

Thank you!

self._data.check_alerts(cache=self._cache)

if self._data.alert:
self._state = bool(self._sensor_type ==
Copy link
Member

Choose a reason for hiding this comment

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

The == will always return a boolean, no need to make it one.

from ring_doorbell import Ring

ring = Ring(username, password)
if ring.is_connected:
Copy link
Member

Choose a reason for hiding this comment

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

You want to return False if not connected, otherwise the component will be seen as set up correctly but the platforms will not be able to find the entry in hass.data


def mocked_requests_get(*args, **kwargs):
"""Mock requests.get invocations."""
class MockResponse:
Copy link
Member

Choose a reason for hiding this comment

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

We have exactly this functionality already available as requests_mock. See this example: https://github.com/home-assistant/home-assistant/blob/dev/tests/util/test_location.py#L62-L66

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this tip @balloob

@tchellomello
Copy link
Contributor Author

@balloob code updated. Thank you!

@balloob
Copy link
Member

balloob commented Mar 31, 2017

Looks good! 🐬

@balloob balloob merged commit 05398a9 into home-assistant:dev Mar 31, 2017
@tchellomello tchellomello deleted the binary_sensor_ring branch April 1, 2017 03:13
@tchellomello tchellomello restored the binary_sensor_ring branch April 1, 2017 03:17
@tchellomello tchellomello deleted the binary_sensor_ring branch April 1, 2017 03:18
@fabaff fabaff mentioned this pull request Apr 6, 2017
@home-assistant home-assistant locked and limited conversation to collaborators Jul 17, 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.

None yet

5 participants