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

Change attributes in AdaptiveSwitch via service call #443

Merged

Conversation

th3w1zard1
Copy link
Collaborator

@th3w1zard1 th3w1zard1 commented Mar 12, 2023

A possible alternative to PR #235

I added a service call for users to modify a switch on the fly, exactly how they would in the config flow, while still keeping overhead low.

__settings__ is called by __init__ and change_switch_settings() to set all variables defined in the service call data and the config flow.
__init__ now only initializes variables we don't want the user touching, such as _lock and switch._name
the function validate() had to be refactored slightly to allow calling with service call data instead of ConfigEntry.

change_switch_settings() has been fully written up in services.yaml, with example data pulled straight from the defaults of const.py

switch should now set settings in __settings__ method and only define the nonchangeable initial attributes in its __init__ method
@th3w1zard1 th3w1zard1 force-pushed the change-config-via-service-call branch from 8ca9490 to 6b1a52d Compare March 14, 2023 05:53
@th3w1zard1 th3w1zard1 force-pushed the change-config-via-service-call branch from 4d56611 to af4b343 Compare March 14, 2023 06:58
next on bugtest agenda: try to break the component with malformed arguments.
All example values have been directly pulled from 'const.py' allowing users to press 'fill example data' to fill defauts.

For some reason 'sunrise_offset' and 'sunset_offset' couldn't be strings
@th3w1zard1 th3w1zard1 force-pushed the change-config-via-service-call branch from d0705b0 to c2cf1b6 Compare March 14, 2023 09:24
@th3w1zard1
Copy link
Collaborator Author

I bugtested this using a myriad of extraordinary values in the service call, the only oddity I could find was sunrise_offset and sunset_offset are both required to be a number.

example:
interval: "90"
PASS
interval: 90
PASS
sunset_offset: "5"
FAIL: 'sunset_offset: expected type 'int' got None
sunset_offset: 5
PASS

@th3w1zard1
Copy link
Collaborator Author

Please don't hesitate to ask for any changes in my implementation! Love the project, thanks for developing it!

@th3w1zard1
Copy link
Collaborator Author

This PR is as finished as it'll ever be.
Issues concerning bad data being sent to this service call will execute the same as setting an improper option in the config flow.
Ex: setting sunset_time before sunrise_time will still output the same error regarding the calculation of noon/midnight.

@basnijholt
Copy link
Owner

This looks awesome! Great job 😄

I think I will be able to give this a try tonight or otherwise tomorrow.

@th3w1zard1
Copy link
Collaborator Author

@basnijholt I just finished your suggested reviews here

@th3w1zard1
Copy link
Collaborator Author

@basnijholt Could you respond to my other reviews here before you merge this one?
Mainly just:

Is there a max value for sunset_offset/sunrise_offset?

@basnijholt
Copy link
Owner

I merged in the master branch and fixed the merge conflict.

@basnijholt
Copy link
Owner

Is there a max value for sunset_offset/sunrise_offset?

I think the offset should be smaller than a value at which the sunset+offset ≥ next sunrise.

@th3w1zard1 th3w1zard1 self-assigned this Mar 27, 2023
@th3w1zard1 th3w1zard1 requested a review from basnijholt March 27, 2023 08:21
@th3w1zard1 th3w1zard1 merged commit 324f26d into basnijholt:master Mar 27, 2023
@basnijholt
Copy link
Owner

basnijholt commented Mar 27, 2023

Please don't merge without PR review approval. And also use squash.

basnijholt pushed a commit that referenced this pull request Mar 27, 2023
Squashed commit of the following:

commit ef15d6a
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Mar 27 03:18:43 2023 -0500

    Readd `platform` from older release

commit 8d867e5
Merge: a858b94 0127496
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Mar 27 03:01:23 2023 -0500

    Merge branch 'master' into change-config-via-service-call

commit 0127496
Author: Bas Nijholt <basnijholt@gmail.com>
Date:   Sun Mar 26 22:47:50 2023 -0700

    Add example of how to pass command line args (#483)

commit a858b94
Author: Bas Nijholt <bas@nijho.lt>
Date:   Sat Mar 25 21:11:55 2023 -0700

    Rename switch.__settings__ to switch._set_changeable_settings

commit bb6eac1
Author: Bas Nijholt <bas@nijho.lt>
Date:   Sat Mar 25 21:10:17 2023 -0700

    small change in handle_change_switch_settings

commit 2005806
Merge: a7acdb1 66e65e2
Author: Bas Nijholt <bas@nijho.lt>
Date:   Sat Mar 25 20:46:19 2023 -0700

    Merge remote-tracking branch 'origin/master' into change-config-via-service-call

commit a7acdb1
Merge: c9d42ad 8c630c9
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Sat Mar 25 15:43:27 2023 -0500

    Merge remote-tracking branch 'upstream/master' into change-config-via-service-call

commit c9d42ad
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Sat Mar 25 14:49:34 2023 -0500

    @basnijholt's reviews

commit 538341f
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Sat Mar 25 13:43:00 2023 -0500

    More formatting

    All mess and no formatting makes jack a dull boy.

commit 9c72787
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Sat Mar 25 13:11:15 2023 -0500

    Update README.md

commit ba1b6cb
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Sat Mar 25 12:57:40 2023 -0500

    Removed unmodifyable options from service call.

    modifying `interval`, `name`, or `lights` isn't possible.

commit bdb4733
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 21 23:01:43 2023 -0500

    Run pre-commit again

commit 3809b6d
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Wed Mar 22 03:41:37 2023 +0000

    [pre-commit.ci] auto fixes from pre-commit.com hooks

    for more information, see https://pre-commit.ci

commit 71c9bd5
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 21 22:41:07 2023 -0500

    Run pre-commit

commit 3ba8190
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 05:58:39 2023 -0500

    Delete change_switch_settings_full_service_data.txt

commit 0111379
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 05:55:26 2023 -0500

    Delete temp.txt

commit c2cf1b6
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 04:24:13 2023 -0500

    Removed custom workflow from pytest.yaml

commit 7a2edbf
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 04:00:50 2023 -0500

    Update services.yaml

    All example values have been directly pulled from 'const.py' allowing users to press 'fill example data' to fill defauts.

    For some reason 'sunrise_offset' and 'sunset_offset' couldn't be strings

commit 30135c3
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 03:13:00 2023 -0500

    Updated services.yaml

commit 8f101da
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 03:01:03 2023 -0500

    added fixes from bugtests

    next on bugtest agenda: try to break the component with malformed arguments.

commit 4b38729
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 02:10:12 2023 -0500

    Update pytest.yaml

commit cb92ce6
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 02:09:24 2023 -0500

    refactored slightly

commit af4b343
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Tue Mar 14 01:58:18 2023 -0500

    fixed syntax errors

commit 5e8b065
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Mon Mar 13 23:50:41 2023 -0500

    rewrote method of changing settings

    switch should now set settings in __settings__ method and only define the nonchangeable initial attributes in its __init__ method

commit 506c810
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Sun Mar 12 16:06:32 2023 -0500

    initial bugtest done

commit 84b95d6
Author: Benjamin Auquite <halomastar@gmail.com>
Date:   Sun Mar 12 12:40:45 2023 -0500

    initial commit no tests done
basnijholt added a commit that referenced this pull request Mar 27, 2023
@@ -130,6 +130,14 @@ adaptive_lighting:
| `lights` | yes | entity_id(s) of lights, if not specified, all lights in the switch are selected. |
| `manual_control` | yes | Whether to add ('true') or remove ('false') the light from the 'manual_control' list, default: true |

`adaptive_lighting.change_switch_settings` (new in 1.5.0) Change any of the above configuration options of Adaptive Lighting (such as `sunrise_time` or `prefer_rgb_color`) with a service call directly from your script/automation.
Copy link
Owner

Choose a reason for hiding this comment

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

1.7.0

basnijholt added a commit that referenced this pull request Mar 27, 2023
basnijholt added a commit that referenced this pull request Mar 27, 2023
basnijholt added a commit that referenced this pull request Mar 27, 2023
@basnijholt
Copy link
Owner

I have changed the merge to a squash merge and made some "review" changes in #485 😄

basnijholt added a commit that referenced this pull request Mar 27, 2023
basnijholt added a commit that referenced this pull request Mar 27, 2023
th3w1zard1 added a commit that referenced this pull request Mar 28, 2023
* Retroactive review changes for #443

* Move up __init__

* Add test for test_change_switch_settings_service

* Add docs

* Add test for defaults

* Remove unused function

* Add skip var

* move pylint marker

* Refactor validate

* Update README.md

---------

Co-authored-by: Benjamin Auquite <halomastar@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment