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 Streamlabs Water Monitor #21205

Merged
merged 11 commits into from
Jun 6, 2019
Merged

Conversation

cpopp
Copy link
Contributor

@cpopp cpopp commented Feb 19, 2019

Description:

Related issue (if applicable): fixes #

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

Example entry for configuration.yaml (if applicable):

streamlabswater:
  api_key: API_KEY

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.

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).
  • New dependencies are only imported inside functions that use them (example).
  • 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.

@homeassistant
Copy link
Contributor

Hi @cpopp,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@cpopp
Copy link
Contributor Author

cpopp commented Feb 25, 2019

Let me know if there is anything else I should address. As far as I'm aware I've addressed all feedback up to this point.

@frenck
Copy link
Member

frenck commented Feb 25, 2019

⚠️ I was unable to find a documentation PR (matching this code change) in our documentation repository.

📚Please, update the documentation and open a PR for it (be sure to create a documentation PR against the next branch) and link 🔗 this PR and the new documentation PR by mentioning the link to the PR's in both openings posts/descriptions.

🏷 I am adding the docs-missing label until this has been resolved.

👍 Thanks (times a million!)

@cpopp
Copy link
Contributor Author

cpopp commented Feb 25, 2019

⚠️ I was unable to find a documentation PR (matching this code change) in our documentation repository.

📚Please, update the documentation and open a PR for it (be sure to create a documentation PR against the next branch) and link 🔗 this PR and the new documentation PR by mentioning the link to the PR's in both openings posts/descriptions.

🏷 I am adding the docs-missing label until this has been resolved.

👍 Thanks (times a million!)

Will do. I had assumed it was fine to just start with the docstrings based on the developer docs, but it sounds like it's not, so when I get some time I'll figure out the docs and open a PR there.

homeassistant/components/streamlabswater/__init__.py Outdated Show resolved Hide resolved
ATTR_AWAY_MODE = 'away_mode'
SERVICE_SET_AWAY_MODE = 'set_away_mode'
AWAY_MODE_AWAY = 'away'
AWAY_MODE_HOME = 'home'
Copy link
Member

Choose a reason for hiding this comment

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

Those two constants should be named in a clear way according their content.

Copy link
Contributor Author

@cpopp cpopp Mar 17, 2019

Choose a reason for hiding this comment

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

Do you have an example of what they should be changed to? I modeled the approach after the nest integration for consistency and this is how it declared those constants.

Copy link
Contributor

Choose a reason for hiding this comment

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

why not just HOME = 'home' and AWAY = 'away' as values for something called DEVICE_MODE?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could there be more modes than away or home? Why not to make it is_away ?

Copy link
Contributor Author

@cpopp cpopp Mar 21, 2019

Choose a reason for hiding this comment

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

The notion of an away mode seemed like something that could eventually be a general concept so I followed the approach used by climate which calls the service set_away_mode. For consistency and ease of search I used the same constant names used in the nest integration. I don't necessarily disagree with the idea that something different might be more optimal, but until someone designs a common architecture to plug in to I thought being consistent with existing integrations and climate was better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree to use similar const names as in climate. But in climate "away" is binary. There's no AWAY_MODE_AWAY, AWAY_MODE_HOME etc. I mean "away" notion is binary in its essence. Someone/Something is either away or not. And climate implements it accordingly, with turn_away_mode_on() and turn_away_mode_off() and a bool property is_away_mode_on

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Adminiuga yeah I agree mine looks a bit more like the nest integration than climate in that regard.

_LOGGER = logging.getLogger(__name__)

ATTR_AWAY_MODE = 'away_mode'
SERVICE_SET_AWAY_MODE = 'set_away_mode'
Copy link
Member

Choose a reason for hiding this comment

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

This constant is already present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're referring to the climate constants, right? I opted to not go with those because my device has nothing to do with climate.


NAME_DAILY_USAGE = "Daily Water"
NAME_MONTHLY_USAGE = "Monthly Water"
NAME_YEARLY_USAGE = "Yearly Water"
Copy link
Member

Choose a reason for hiding this comment

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

Snake case please

Suggested change
NAME_YEARLY_USAGE = "Yearly Water"
NAME_YEARLY_USAGE = "yearly_water"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are just used for the names which as far as I could tell in other sensors are typically capitalized with spaces (or accepted freeform as config parameters).

Copy link
Member

Choose a reason for hiding this comment

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

Correct. For names spaces and capitalization is allowed. Attribute keys should be snake_case underscore

self._streamlabs_usage_data.update()


class StreamLabsMonthlyUsage(Entity):
Copy link
Member

Choose a reason for hiding this comment

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

StreamLabsMonthlyUsage() and StreamLabsYearlyUsage() should be integrated in StreamLabsDailyUsage. There is a lot of duplicate code. StreamLabsUsage could get the details for a dict/list which describes the different sensors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the monthly and yearly classes to extend the daily usage and that got rid of most of what was common.

homeassistant/components/streamlabswater/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/streamlabswater/binary_sensor.py Outdated Show resolved Hide resolved
@cpopp cpopp force-pushed the streamlabswater branch from 303a010 to f8afd35 Compare March 17, 2019 18:15
@cpopp
Copy link
Contributor Author

cpopp commented Mar 17, 2019

I rebased the changes on the latest dev since some conflicting changes were added since I opened the PR.

@@ -0,0 +1,73 @@
"""Support for Streamlabs Water Monitor Away Mode."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not implement this as a switch and get rid of the streamlabswater.set_away_modeservice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two reasons: 1.) When I first started to implement this I considered that but then noticed that would cause it to be controlled along with all other switches in a call to turn_off switches when not specifying a particular entity_id (via stuff a user might have set up programatically or via the one click button in the UI to toggle all switches). This seemed pretty risky to me in that disabling away_mode unintentionally could potentially mean a user's home is not being monitored for water user while gone and a leak would go undetected. So, I thought it was worth making it a separate service so a user could trust it would be operated on independently and integrated in a way.

2.) In general when I surveyed the various switches in home assistant nearly all of them represented turning on or off some physical entity in the real world rather than a conceptual behavior of the entity...so while technically it applicable (it's just a togglable binary value after all) it seemed like it did not fit with the intent of what a switch is.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are absolutely right! But if you build it as an alarm_panel it would make sense, arm away and arm home matches the service call & you can show the triggered state instead of the binary sensor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that makes sense. Long term I think it would be cool for the away mode to integrate with some presence system supported in the core of home assistant so that it gets managed automatically for the user. For now someone could use the switch template if they wanted it to act like a switch.

Copy link
Contributor

Choose a reason for hiding this comment

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

The "integration with presence system" is done via automations like this one:

automation:
  trigger:
  - platform: zone
    entity_id: device_tracker.paulus
    zone: zone.home
    event: leave
  action:
  - service: alarm_control_panel.alarm_arm_away
    data:
      entity_id: alarm_control_panel.streamlabs_water

cpopp added 9 commits April 21, 2019 16:28
The Streamlabs Water component is unable to recover if it is given
an invalid API key or location id so this change is to ensure
we validate they are correct during setup and return a failure
if they are not.
The sensors for the component were not causing an immediate load of
data from the API when being set up so there was some lag after
startup before values would show up.  This change does an explicit
update when the sensors are setup to ensure data is viewable
immediately after startup.
Dependencies were incorrectly specified using DEPENDS rather
than DEPENDENCIES
Remove detailed class docstrings since they're in the documentation,
reduce code duplication in sensor classes, and remove periods from
the end of log messages.
@cpopp cpopp force-pushed the streamlabswater branch from 99044cf to cc277b0 Compare April 21, 2019 21:28
@codecov
Copy link

codecov bot commented Apr 21, 2019

Codecov Report

Merging #21205 into dev will decrease coverage by 0.45%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #21205      +/-   ##
==========================================
- Coverage   94.29%   93.83%   -0.46%     
==========================================
  Files         458      448      -10     
  Lines       37203    36529     -674     
==========================================
- Hits        35079    34276     -803     
- Misses       2124     2253     +129
Impacted Files Coverage Δ
homeassistant/components/mqtt/fan.py 74.33% <0%> (-23.64%) ⬇️
homeassistant/components/mqtt/light/schema_json.py 73.06% <0%> (-20.65%) ⬇️
homeassistant/bootstrap.py 58.08% <0%> (-16.33%) ⬇️
homeassistant/components/mqtt/lock.py 91.83% <0%> (-8.17%) ⬇️
homeassistant/components/axis/device.py 93.02% <0%> (-6.98%) ⬇️
homeassistant/components/hassio/ingress.py 71.3% <0%> (-6.75%) ⬇️
homeassistant/components/deconz/config_flow.py 97.77% <0%> (-2.23%) ⬇️
homeassistant/components/mqtt/vacuum.py 92.46% <0%> (-2.05%) ⬇️
homeassistant/components/hue/bridge.py 73.07% <0%> (-1%) ⬇️
homeassistant/components/homekit/util.py 99.05% <0%> (-0.95%) ⬇️
... and 176 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 3b0660a...cc277b0. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented Apr 21, 2019

Codecov Report

Merging #21205 into dev will decrease coverage by 0.45%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev   #21205      +/-   ##
==========================================
- Coverage   94.29%   93.83%   -0.46%     
==========================================
  Files         458      448      -10     
  Lines       37203    36529     -674     
==========================================
- Hits        35079    34276     -803     
- Misses       2124     2253     +129
Impacted Files Coverage Δ
homeassistant/components/mqtt/fan.py 74.33% <0%> (-23.64%) ⬇️
homeassistant/components/mqtt/light/schema_json.py 73.06% <0%> (-20.65%) ⬇️
homeassistant/bootstrap.py 58.08% <0%> (-16.33%) ⬇️
homeassistant/components/mqtt/lock.py 91.83% <0%> (-8.17%) ⬇️
homeassistant/components/axis/device.py 93.02% <0%> (-6.98%) ⬇️
homeassistant/components/hassio/ingress.py 71.3% <0%> (-6.75%) ⬇️
homeassistant/components/deconz/config_flow.py 97.77% <0%> (-2.23%) ⬇️
homeassistant/components/mqtt/vacuum.py 92.46% <0%> (-2.05%) ⬇️
homeassistant/components/hue/bridge.py 73.07% <0%> (-1%) ⬇️
homeassistant/components/homekit/util.py 99.05% <0%> (-0.95%) ⬇️
... and 176 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 3b0660a...cc277b0. Read the comment docs.

from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['streamlabswater==1.0.1']
Copy link
Member

Choose a reason for hiding this comment

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

This constant is no longer used. Please remove.

@balloob balloob merged commit 1bca313 into home-assistant:dev Jun 6, 2019
@balloob balloob mentioned this pull request Jun 26, 2019
alandtse pushed a commit to alandtse/home-assistant that referenced this pull request Oct 12, 2019
* Add Streamlabs Water Monitor

* Fail Streamlabswater component setup when given invalid parameters

The Streamlabs Water component is unable to recover if it is given
an invalid API key or location id so this change is to ensure
we validate they are correct during setup and return a failure
if they are not.

* Prime Streamlabswater component sensors so data is available immediately

The sensors for the component were not causing an immediate load of
data from the API when being set up so there was some lag after
startup before values would show up.  This change does an explicit
update when the sensors are setup to ensure data is viewable
immediately after startup.

* Switch Streamlabswater logging to use %s for string formatting

* Update Streamlabswater component with correct dependencies

Dependencies were incorrectly specified using DEPENDS rather
than DEPENDENCIES

* Streamlabswater pull request feedback

Remove detailed class docstrings since they're in the documentation,
reduce code duplication in sensor classes, and remove periods from
the end of log messages.

* Reduce line length in Streamlabswater sensor

* Add docstring on Streamlabswater service callback method

* Get rid of unnecessary initializers in Streamlabswater sensor

* Add manifest file for Streamlabs Water Monitor

* Remove unused REQUIREMENTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants