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

Add additional sensors to RainMachine #80914

Merged
merged 6 commits into from
Oct 25, 2022
Merged

Add additional sensors to RainMachine #80914

merged 6 commits into from
Oct 25, 2022

Conversation

shbatm
Copy link
Contributor

@shbatm shbatm commented Oct 24, 2022

Proposed change

Expose additional sensors from the RainMachine controller:

  1. Flow Sensor Leak Clicks & Volume: the reported number of clicks and calculated volume of a leak (flow meter activity when sprinklers are not running)
  2. Last Leak Time: the last reported leak time (as timestamp)
  3. Rain Sensor Rain Start: the last reported rain detection start time (as timestamp)

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)
  • Deprecation (breaking change to happen in the future)
  • 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: fixes #n/a
  • This PR is related to issue: n/a
  • Link to documentation pull request: n/a

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.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @bachya, mind taking a look at this pull request as it has been labeled with an integration (rainmachine) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of rainmachine can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant unassign rainmachine Removes the current integration label and assignees on the issue, add the integration domain after the command.

@shbatm shbatm marked this pull request as ready for review October 24, 2022 21:49
@shbatm shbatm requested a review from bachya as a code owner October 24, 2022 21:49
@shbatm
Copy link
Contributor Author

shbatm commented Oct 24, 2022

@bachya All information is already returned every minute from the /provision endpoint, so I don't need to use the /watering/flowmeter endpoint. In case you ever need to in the future, I noticed that the /provision endpoint uses flowSensor but the other uses flowMeter, both report the same values. Just FYI.

@bachya
Copy link
Contributor

bachya commented Oct 24, 2022

In case you ever need to in the future, I noticed that the /provision endpoint uses flowSensor but the other uses flowMeter, both report the same values. Just FYI.

More fun with the RainMachine API. 😂

@shbatm
Copy link
Contributor Author

shbatm commented Oct 24, 2022

More fun with the RainMachine API.

As a side note: my unit was severely borked a while back and I had to get creative in some ssh repairs--long story short, I have some de-compiled python source code for the main controller if you ever need it for reference.

@@ -317,6 +377,14 @@ def update_from_latest_data(self) -> None:
self._attr_native_value = self.coordinator.data.get("system", {}).get(
"flowSensorWateringClicks"
)
elif self.entity_description.key == TYPE_LAST_LEAK_DETECTED:
self._attr_native_value = utc_from_timestamp(
self.coordinator.data.get("system", {}).get("lastLeakDetected")
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't utc_from_timestamp blow up if this ends up being None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. Resolved in the restuctured method. See comment below.

homeassistant/components/rainmachine/sensor.py Outdated Show resolved Hide resolved
)

if clicks and clicks_per_m3:
self._attr_native_value = round((clicks * 1000) / clicks_per_m3, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Feels like we have a bunch of replicated logic throughout here – what would you think about some easy helper methods (attached to this class) that centralize it?

I would look at anything you're replicating between sensor types (e.g., the round((clicks * 1000) / clicks_per_m3, 1) calculation [and the data points fed into it]).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Restructured the method to be less redundant. Take a look.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks reasonable to me!

@bachya
Copy link
Contributor

bachya commented Oct 25, 2022

You've confirmed this functionally works?

@shbatm
Copy link
Contributor Author

shbatm commented Oct 25, 2022

Confirmed working:
image

image

image

config_entry-rainmachine-044f461135ae717322ae9b933b75b1a4.json.txt

Side note: any objections to using the zoneDuration info from the controller to set the default zone run times?

@shbatm
Copy link
Contributor Author

shbatm commented Oct 25, 2022

Caught an error when I reset the last leak event in the controller. API says it uses null for no last event, but actually returns 0 which angers the timestamp-type sensor. Updated with fix.

@bachya
Copy link
Contributor

bachya commented Oct 25, 2022

Side note: any objections to using the zoneDuration info from the controller to set the default zone run times?

That would be useful! I'd make that a config entry option so users can decide if they want that or a manually set duration (would avoid a breaking change, too).

Copy link
Contributor

@bachya bachya left a comment

Choose a reason for hiding this comment

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

Looks great!

@bachya bachya merged commit e9a3982 into home-assistant:dev Oct 25, 2022
@shbatm shbatm deleted the rainmachine_add_sensors branch October 25, 2022 15:20
@shbatm
Copy link
Contributor Author

shbatm commented Oct 25, 2022

That would be useful! I'd make that a config entry option so users can decide if they want that or a manually set duration (would avoid a breaking change, too).

Only problem is I have NO IDEA where those durations are being set. They don't match the last manual runs or the suggested program values. I'll do a PR in the future if I figure out how to set/store them, but for now, I'll leave it alone.

@shbatm
Copy link
Contributor Author

shbatm commented Oct 25, 2022

Ahh.. found it. It's the last manual time you used in the RainMachine App.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 26, 2022
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