Introduced Ring binary sensors and refactored Ring component#6520
Conversation
- Added unittest for Ring binary_sensor. - Bumped ring_doorbell 3rd party module.
| } | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_USERNAME): cv.string, |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
That is a good idea @balloob. I will work on that.
- Added unittest for Ring binary_sensor. - Bumped ring_doorbell 3rd party module.
| from unittest import mock | ||
|
|
||
| import homeassistant.components.binary_sensor as sensor | ||
| from homeassistant.components.binary_sensor import ring |
There was a problem hiding this comment.
'homeassistant.components.binary_sensor.ring' imported but unused
|
|
||
| ring = Ring(username, password) | ||
| if ring.is_connected: | ||
| RING = RingData(ring) |
There was a problem hiding this comment.
Use hass.data to store RING. Do not use globals.
There was a problem hiding this comment.
I see. Thanks for the heads.
|
|
||
| def update(self): | ||
| """Get the latest data and updates the state.""" | ||
| self._data.check_alerts(cache=self._cache) |
There was a problem hiding this comment.
Why do you need to pass in the cache? That is something that should be handled by the shared data class.
There was a problem hiding this comment.
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.
0659774 to
e61015a
Compare
|
@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 == |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
|
@balloob code updated. Thank you! |
|
Looks good! 🐬 |
Description:
ring_doorbell3rd party Python moduleExample entry for
configuration.yaml(if applicable):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:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable ([example][ex-requir]).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass