Skip to content

Refactor Ring data handling#30777

Merged
balloob merged 2 commits into
devfrom
ring-part-3
Jan 15, 2020
Merged

Refactor Ring data handling#30777
balloob merged 2 commits into
devfrom
ring-part-3

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented Jan 15, 2020

Description:

One final PR for Ring to clean up the data handling, inspired by comments from @MartinHjelmare.

Moves all data handling to global objects. They are either global endpoint based or per-device based.

Extract a generic RingEntityMixin to keep implementations simple.

Thanks to @dshokouhi for being able to test with his Ring account.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

Copy link
Copy Markdown
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.

It's still not clear to me why it's important to allow platforms to decide if we should fetch data for the devices. I don't see where the number of requests could be dynamic based on a decision in a platform.

Comment thread homeassistant/components/ring/__init__.py Outdated
Comment thread homeassistant/components/ring/__init__.py Outdated
Comment thread homeassistant/components/ring/__init__.py Outdated
Comment thread homeassistant/components/ring/__init__.py Outdated
Comment thread homeassistant/components/ring/__init__.py Outdated
self._unsub_interval = None

def refresh_all(self, _):
def refresh_all(self, _=None):
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.

Why is this not an async interface? We seem to be calling this from async context and two out of three calls inside are async.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Jumping into the executor 10 times inside a loop is a lot of overhead of jumping around between async and sync. It's in that case better to run in sync and just schedule the callbacks to be called.

self._active_alert = next(
(
alert
for alert in self._ring.active_alerts()
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.

Is this just accessing a local data cache?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes

Comment thread homeassistant/components/ring/camera.py
self.devices[device.device_id]["task"] = asyncio.current_task()
self.devices[device.device_id][
"data"
] = await self.hass.async_add_executor_job(self.update_method, device)
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 tracking a device also need to update the data? Couldn't we just let the consumer get the data from the cache?

Why do we need to let the platforms track devices? We already know what devices there are. We could let the component control the device interface completely and just have platforms get data as needed from the cache.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If an entity is disabled, we wouldn't want to fetch that data.

@balloob balloob merged commit 1e82813 into dev Jan 15, 2020
@delete-merged-branch delete-merged-branch Bot deleted the ring-part-3 branch January 15, 2020 16:10
raman325 added a commit to raman325/home-assistant that referenced this pull request Jan 15, 2020
* upstream/dev: (82 commits)
  Add support for vacuums to Alexa. (home-assistant#30764)
  Refactor Ring data handling (home-assistant#30777)
  Restore unit_of_measurement from entity registry (home-assistant#30780)
  Update pyubee to 0.8 (home-assistant#30785)
  Update emulated_roku to 0.1.9 (home-assistant#30791)
  Add Config Flow support, Device Registry support, available property to vizio component (home-assistant#30653)
  Allow input_* and timer component setup without config (home-assistant#30772)
  Search: Add search to default config and don't resolve area (home-assistant#30762)
  [ci skip] Translation update
  Use storage based collections for Timer platform (home-assistant#30765)
  Upgrade youtube_dl to version 2020.01.15 (home-assistant#30767)
  Whitelist Frenck for release
  Hass.io allow to reset password with CLI (home-assistant#30755)
  Revert home-assistant#29701 (home-assistant#30766)
  Add Safe Mode (home-assistant#30723)
  Update Ring to 0.6.0 (home-assistant#30748)
  Add support for the voltage sensor on the greeneye GEM (home-assistant#30484)
  Fix supported_features in MQTT fan (home-assistant#28680)
  Fix small typo in alarmdotcom component (home-assistant#30758)
  bump aiokef to 0.2.5 which uses locks (home-assistant#30753)
  ...
frenck pushed a commit that referenced this pull request Jan 15, 2020
* Refactor Ring data handling

* Add async_ to methods
@lock lock Bot locked and limited conversation to collaborators Jan 16, 2020
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.

4 participants