Skip to content

Add abode support for CUE automations#31514

Closed
shred86 wants to merge 20 commits into
home-assistant:devfrom
shred86:abode_cue
Closed

Add abode support for CUE automations#31514
shred86 wants to merge 20 commits into
home-assistant:devfrom
shred86:abode_cue

Conversation

@shred86
Copy link
Copy Markdown
Contributor

@shred86 shred86 commented Feb 6, 2020

Breaking change

Abode is retiring its legacy automations and quick actions on 28 Feb (see Abode support article here). This change removes support for legacy automations and quick actions while adding support for CUE automations which will show up in Home Assistant as switches (same as previous implementation). Abode's replacement for quick actions is manually triggered CUE automations which can be called through Home Assistant service abode.trigger_automation (previously labeled abode.trigger_quick_action).

Proposed change

Removed support for Abode's legacy automations and quick actions being retired on 28 Feb. Adds support for Abode's new CUE automations which will show up as switches in Home Assistant (same as previously implementation). CUE automations can be called using the abode.trigger_automation service. Additionally, added tests for the Abode integration, removed some unused variables.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: N/A
  • This PR is related to issue: N/A
  • Link to documentation pull request: #11984

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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

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

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@MartinHjelmare MartinHjelmare changed the title Add support for CUE automations Add abode support for CUE automations Feb 6, 2020
@shred86 shred86 mentioned this pull request Feb 8, 2020
4 tasks
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 8, 2020

Codecov Report

Merging #31514 into dev will decrease coverage by 0.02%.
The diff coverage is 96.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #31514      +/-   ##
==========================================
- Coverage   94.75%   94.72%   -0.03%     
==========================================
  Files         772      778       +6     
  Lines       55885    56109     +224     
==========================================
+ Hits        52953    53152     +199     
- Misses       2932     2957      +25
Impacted Files Coverage Δ
homeassistant/components/abode/sensor.py 97.5% <ø> (ø)
homeassistant/components/abode/light.py 100% <ø> (ø)
homeassistant/components/abode/config_flow.py 100% <100%> (ø) ⬆️
homeassistant/components/abode/const.py 100% <100%> (ø) ⬆️
homeassistant/components/abode/switch.py 95.12% <100%> (ø)
homeassistant/components/abode/binary_sensor.py 100% <100%> (ø)
...eassistant/components/abode/alarm_control_panel.py 85.29% <100%> (ø)
homeassistant/components/abode/camera.py 66% <50%> (ø)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 58de7fe...3fb2555. Read the comment docs.

@shred86
Copy link
Copy Markdown
Contributor Author

shred86 commented Feb 8, 2020

First attempt at adding tests for the Abode component. I'm still figuring out how to write tests but for now, I'm attempting to test that the Abode component is initialized correctly and that devices are setup appropriately. I don't have all of the different Abode devices in my home setup but I'll continue to expand the abode_devices.json fixtures as I obtain more device JSON data. I also haven't implemented any checking of abode services or state changes yet.

@shred86
Copy link
Copy Markdown
Contributor Author

shred86 commented Feb 10, 2020

More test updates. Here are the known "issues" with tests:

1. Pylint is complaining about accessing a protected member _devices of the Abode class. I'm simply trying to simulate what occurs when Abode's web socket server pushes an event update and abodepy receives it, which calls a callback to the Abode integration. Unfortunately, manually calling the callback doesn't seem to be working. See comments in code.

2. Once I figure out the above (manually calling a callback), there are several updates I'll have to the tests to check the device state. Right now some of the tests are incomplete until I can figure out the above.

  1. Gave up on the above approach. I'm now mocking the abodepy method being called and checking if it's called once. In the future, I'll need to figure out how to simulate an event update being pushed from abodepy which means I'll still need to figure out how to call a callback or at least force a HA entity refresh.

  2. Abodepy creates a abodepy_cache.pickle inside the testing_config folder. CI keeps failing because of this. I noticed some other APIs have config files inside testing_config so I went ahead and committed the abodepy_cache.pickle that is generated when I run the tests locally. Hopefully this isn't an issue as it mimics what's occurring "real world". The only other option I can think of to avoid the pickle file in tests is if I add a config option to disable the pickle cache, which I'd prefer not to do since it's adding functionality that wouldn't be used other than with tests.

  3. I'm having issues with raising assertions. See comments in code.

Anyways, hopefully this is a decent start to tests for the abode integration. I'll continue to make additions/modifications as required for this PR to be merged, but I also continue to plan on working on tests as I learn more to get 100% coverage for abode.

Comment thread tests/components/abode/test_light.py Outdated
async def test_connection_error(hass, requests_mock):
"""Test exception when unable to connect to Abode."""
requests_mock.post(CONST.LOGIN_URL, exc=ConnectTimeout)
# Tried using 'with pytest.raises(ConnectTimeout):' but
Copy link
Copy Markdown
Contributor Author

@shred86 shred86 Feb 16, 2020

Choose a reason for hiding this comment

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

For whatever reason, ConnectTimeout is not being raised. The strange part is I know the request_mock.post(CONST.LOGIN_URL, exc=ConnectTImeout) line is partially working because when I run the test, I can see it's executing the appropriate part of the abode module and logging the correct message. However, where it prints out the exception in that log message, it's blank. I've tried a number of different things such as mocking abodepy.Abode.login and setting a side effect exception but it still doesn't work.

self._automation.set_active(True)
"""Enable the automation."""
if self._automation.enable(True):
self.schedule_update_ha_state()
Copy link
Copy Markdown
Contributor Author

@shred86 shred86 Feb 16, 2020

Choose a reason for hiding this comment

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

This is required based on there being no web socket events for automations being disabled or enabled (unlike normal devices in Abode). How the abode integration is currently setup, when a device is switched on/off, it calls a method in abodepy that will execute the command and also immediately refresh the device's JSON data. However, the abode integration waits until it receives a web socket event of the device state changing to execute a call back which calls self.schedule_update_ha_state(). Again, this doesn't happen with automations hence the reason I'm calling self.schedule_update_ha_state() here.

As a side note, it seems like doing this on all devices in the abode integration would make sense such that Home Assistant has the correct state immediately (the JSON response is much faster than waiting for the web socket event). In some situations where the device web socket event is slow to be received, you will see a switch in HA go from on, to off, to back on when you simply turned it on. The downside is we're basically updating the device state in HA twice, once immediately after the device state was changed and again when we receive the web socket event. I'm not sure if that's considered bad practice, but it seems better from a UX perspective.

Comment thread tests/components/abode/test_light.py Outdated
async def test_set_color(hass, requests_mock):
"""Test the color can be set."""
await setup_platform(hass, LIGHT_DOMAIN)
# light.turn_on service is calling `turn_on` in switch.py and light.py
Copy link
Copy Markdown
Contributor Author

@shred86 shred86 Feb 16, 2020

Choose a reason for hiding this comment

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

Edit: Made some slight modifications to test_light.py but the comment below is still valid (not outdated).

The fix for this is simply adding return statements in AbodeLight.turn_on in light.py. The issue is the else: statement is being matched every time which is not desired. If turn_on is called with any color or brightness information, it should execute the appropriate if statement and return. Otherwise, we're making two separate calls to Abode's servers which is not the intent.

I have the fixes and changes to test_light.py on my local repo but I haven't committed them based on the dev docs stating:

Do not mix clean-ups and new features in a single pull request.

I can either way until this PR is merged or since it's a very small change, add them to this one if desired.

@shred86 shred86 closed this Feb 28, 2020
@lock lock Bot locked and limited conversation to collaborators Feb 29, 2020
@shred86 shred86 deleted the abode_cue branch March 7, 2020 17:43
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.

2 participants