Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Refactored Arlo component and enhanced Arlo API queries and times #14823

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

Merged
merged 17 commits into from
Jun 12, 2018

Conversation

tchellomello
Copy link
Contributor

@tchellomello tchellomello commented Jun 5, 2018

Description:

Arlo does not offer an official API so all this work is based on reverse engineering.
Refactored Arlo component and enhanced Arlo API queries and times.
This PR also added a service call method to force the entities to be updated.

Related issue (if applicable): fixes #13176

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

Example entry for configuration.yaml (if applicable):

arlo:
  username: [email protected]
  password: secret
  scan_interval: 120

ffmpeg:

camera: 
  - platform: arlo

alarm_control_panel:
  - platform: arlo

sensor:
  - platform: arlo

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

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

If the code communicates with devices, web services, or third-party tools:

  • 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 or updated 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:

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

@tchellomello tchellomello changed the title Refactored Arlo component and enhanced Arlo API queries and times **WIP** Refactored Arlo component and enhanced Arlo API queries and times WIP Jun 5, 2018

def __init__(self, hub):
"""Initialize the entity."""
self.hub = hub
Copy link
Member

Choose a reason for hiding this comment

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

Why store the hub on another instance if it's just that attribute and no methods needed?

async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_ARLO, self._update_callback)

def _update_callback(self):
Copy link
Member

Choose a reason for hiding this comment

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

Decorate this with @callback imported from core.py.

@@ -68,24 +69,29 @@ def icon(self):
"""Return icon."""
return ICON

@asyncio.coroutine
Copy link
Member

Choose a reason for hiding this comment

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

We're now using Python 3.5 async syntax, ie async def.


def _update_callback(self):
"""Call update method."""
self.schedule_update_ha_state(True)
Copy link
Member

Choose a reason for hiding this comment

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

Use self.async_schedule_update_ha_state.

return
i += 1
self._state = None
_LOGGER.info("Updating Arlo Alarm Control Panel %s", self.name)
Copy link
Member

Choose a reason for hiding this comment

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

Max debug.

# assign refresh period to base station thread
try:
if arlo.base_stations:
arlo_base_station = arlo.base_stations[0]
Copy link
Member

@MartinHjelmare MartinHjelmare Jun 5, 2018

Choose a reason for hiding this comment

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

This is better:

arlo_base_station = next((
    station for station in arlo.base_stations), None)
if arlo_base_station is None:
    return False

if arlo.base_stations:
arlo_base_station = arlo.base_stations[0]
arlo_base_station.refresh_rate = scan_interval.total_seconds()
except (AttributeError, IndexError):
Copy link
Member

Choose a reason for hiding this comment

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

Why would there be an attribute error?

The index error is solved by using my suggestion above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is not needed anymore. Good refactoring!! Thanks

@@ -145,7 +154,7 @@ def motion_detection_enabled(self):
def set_base_station_mode(self, mode):
"""Set the mode in the base station."""
# Get the list of base stations identified by library
base_stations = self.hass.data[DATA_ARLO].base_stations
base_stations = self.hass.data.get(DATA_ARLO).hub.base_stations
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 change to dict.get. We know that the key will be in the dict.

@@ -135,7 +144,7 @@ def brand(self):
@property
def should_poll(self):
"""Camera should poll periodically."""
return True
return False
Copy link
Member

Choose a reason for hiding this comment

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

This is the default for cameras.

@tchellomello
Copy link
Contributor Author

Thanks for the review @MartinHjelmare Good tips indeed!! Please let me know what you think now. I hope to update the documentation tonight so then we can get this merged.

def _update_callback(self):
"""Call update method."""
self.schedule_update_ha_state(True)
self.async_schedule_update_ha_state()
Copy link
Member

Choose a reason for hiding this comment

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

Pass True as argument to call the update method before updating state.

self.async_schedule_update_ha_state(True)

@@ -49,7 +49,7 @@

def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up an Arlo IP Camera."""
arlo = hass.data.get(DATA_ARLO).hub
arlo = hass.data[DATA_ARLO]
if not arlo:
Copy link
Member

Choose a reason for hiding this comment

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

This can't happen.

@@ -39,7 +39,7 @@

def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up an Arlo IP sensor."""
arlo = hass.data.get(DATA_ARLO).hub
arlo = hass.data.get(DATA_ARLO)
if not arlo:
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.

"""Call ArloHub to refresh information."""
_LOGGER.info("Updating Arlo Hub component")
hass.data[DATA_ARLO].update(update_cameras=True,
update_base_station=True)

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

@callback
def _update_callback(self):
"""Call update method."""
self.async_schedule_update_ha_state()
Copy link
Member

Choose a reason for hiding this comment

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

Add True as argument.

return self._camera.last_image
return self._camera.last_image_from_cache

@asyncio.coroutine
Copy link
Member

Choose a reason for hiding this comment

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

Update all coroutines to use new syntax.

async_dispatcher_connect(
self.hass, SIGNAL_UPDATE_ARLO, self._update_callback)

def _update_callback(self):
Copy link
Member

Choose a reason for hiding this comment

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

Update this like my previous comment. I haven't commented on all the places that needs the same changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, my bad.


def _update_callback(self):
"""Call update method."""
self.schedule_update_ha_state(True)
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.

@tchellomello tchellomello changed the title Refactored Arlo component and enhanced Arlo API queries and times WIP Refactored Arlo component and enhanced Arlo API queries and times Jun 8, 2018
@ghost ghost added the in progress label Jun 8, 2018
@tchellomello
Copy link
Contributor Author

@MartinHjelmare thanks again for the code review. I've updated the documentation and code. 🏆

@arsaboo
Copy link
Contributor

arsaboo commented Jun 8, 2018

Seeing a lot of errors (started appearing after about 14 minutes of restarting) with the latest code https://hastebin.com/evogidegih.sql

@tchellomello
Copy link
Contributor Author

tchellomello commented Jun 8, 2018

@arsaboo I got the same here. Thanks for the heads up. I'm working on this now

@tchellomello
Copy link
Contributor Author

@arsaboo it seems we got it now. Please let me know if works for you 👍

@arsaboo
Copy link
Contributor

arsaboo commented Jun 11, 2018

Looks like everything (except the alarm_control_panel) is fixed. The camera and sensors are flawless and each entity is not making its own separate API call. Also, there are no errors/warnings in the logs....so that is already a major improvement and will make lot of people very happy.

For some reason, alarm_control_panel goes to Unknown and stays there. Not sure what is causing this.

@tchellomello
Copy link
Contributor Author

@arsaboo thanks for your update. I'm glad this worked and fixed the huge amount of calls made to the Arlo API.
In regards the alarm_control_panel I'm working on a different PR that will include a big refactor in some areas of the pyarlo code. The alarm_control_panel starts correctly but after some time, it goes to unknown after some time and I think I know why. We should get a new PR for that soon.

@MartinHjelmare does that sound a good plan? Could we get this merged so then our users can get to use Arlo again while I'm working on the alarm_control_panel isolated issue?

Thanks to everyone who helped on this! 👍

@arsaboo
Copy link
Contributor

arsaboo commented Jun 11, 2018

I will let @MartinHjelmare decide, but given that Arlo is currently broken, it will be great if we can get this merged soon.

Thanks @tchellomello for all your efforts.

@MartinHjelmare
Copy link
Member

Sounds good!

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.

Some small fixes left. You can also remove the second argument True in the camera platform call to add_devices.

})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up an Arlo IP Camera."""
arlo = hass.data.get(DATA_ARLO)
arlo = hass.data[DATA_ARLO]
if not arlo:
return False
Copy link
Member

Choose a reason for hiding this comment

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

Only return.

@callback
def _update_callback(self):
"""Call update method."""
self.async_schedule_update_ha_state(True)
Copy link
Member

Choose a reason for hiding this comment

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

There's no update method for this entity anymore so we don't need the argument True here. Sorry if I said so earlier.


async_add_devices(sensors, True)
add_devices(sensors, True)
return True
Copy link
Member

Choose a reason for hiding this comment

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

Remove this.

})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up an Arlo IP Camera."""
arlo = hass.data.get(DATA_ARLO)
arlo = hass.data[DATA_ARLO]
if not arlo:
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this can happen?

@tchellomello
Copy link
Contributor Author

Thanks @MartinHjelmare PR updated. 👍

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.

Great!

@MartinHjelmare MartinHjelmare merged commit cdc5388 into home-assistant:dev Jun 12, 2018
@ghost ghost removed the in progress label Jun 12, 2018
@tchellomello tchellomello deleted the arlo_fix branch June 12, 2018 13:37
This was referenced Jun 22, 2018
@greybags
Copy link

Hi Guys, fantastic work on this, I love that it has my Arlo cameras integrated and I can use automations to control them using presence detection but has the alarm_control_panel issue been addressed? I still see unknown after a while and it then stays there?

Cheers

@home-assistant home-assistant locked as resolved and limited conversation to collaborators Aug 11, 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.

Miscellaneous Arlo warnings and errors
6 participants