-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Netatmo public #15684
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
Netatmo public #15684
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
261cb70
Add a sensor for netatmo public data
colinfrei fc789d2
A bit of cleanup before submitting pull request
colinfrei a7bffa0
Add netatmo_public file to .coveragerc, as per pull request template …
colinfrei 78a8d63
Fixes for tox complaining
colinfrei 4f5a8e2
make calculations simpler, based on review feedback
colinfrei 9c74c9b
explicitly pass required_data parameter to netatmo API
colinfrei 49d5aac
remove unnecessary spaces
colinfrei fd90ce8
remove debug code
colinfrei 8afb891
code style fix
colinfrei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| """ | ||
| Support for Sensors using public Netatmo data. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.netatmo_public/. | ||
| """ | ||
| from datetime import timedelta | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import (CONF_NAME, CONF_TYPE) | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.util import Throttle | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEPENDENCIES = ['netatmo'] | ||
|
|
||
| CONF_AREAS = 'areas' | ||
| CONF_LAT_NE = 'lat_ne' | ||
| CONF_LON_NE = 'lon_ne' | ||
| CONF_LAT_SW = 'lat_sw' | ||
| CONF_LON_SW = 'lon_sw' | ||
|
|
||
| DEFAULT_NAME = 'Netatmo Public Data' | ||
| DEFAULT_TYPE = 'max' | ||
| SENSOR_TYPES = {'max', 'avg'} | ||
|
|
||
| # NetAtmo Data is uploaded to server every 10 minutes | ||
| MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=600) | ||
|
|
||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_AREAS): vol.All(cv.ensure_list, [ | ||
| { | ||
| vol.Required(CONF_LAT_NE): cv.latitude, | ||
| vol.Required(CONF_LAT_SW): cv.latitude, | ||
| vol.Required(CONF_LON_NE): cv.longitude, | ||
| vol.Required(CONF_LON_SW): cv.longitude, | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| vol.Optional(CONF_TYPE, default=DEFAULT_TYPE): | ||
| vol.In(SENSOR_TYPES) | ||
| } | ||
| ]), | ||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Set up the access to Netatmo binary sensor.""" | ||
| netatmo = hass.components.netatmo | ||
|
|
||
| sensors = [] | ||
| areas = config.get(CONF_AREAS) | ||
| for area_conf in areas: | ||
| data = NetatmoPublicData(netatmo.NETATMO_AUTH, | ||
| lat_ne=area_conf.get(CONF_LAT_NE), | ||
| lon_ne=area_conf.get(CONF_LON_NE), | ||
| lat_sw=area_conf.get(CONF_LAT_SW), | ||
| lon_sw=area_conf.get(CONF_LON_SW), | ||
| calculation=area_conf.get(CONF_TYPE)) | ||
| sensors.append(NetatmoPublicSensor(area_conf.get(CONF_NAME), data)) | ||
| add_devices(sensors) | ||
|
|
||
|
|
||
| class NetatmoPublicSensor(Entity): | ||
| """Represent a single sensor in a Netatmo.""" | ||
|
|
||
| def __init__(self, name, data): | ||
| """Initialize the sensor.""" | ||
| self.netatmo_data = data | ||
| self._name = name | ||
| self._state = None | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Icon to use in the frontend.""" | ||
| return 'mdi:weather-rainy' | ||
|
|
||
| @property | ||
| def device_class(self): | ||
| """Return the device class of the sensor.""" | ||
| return None | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return true if binary sensor is on.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit of measurement of this entity.""" | ||
| return 'mm' | ||
|
|
||
| def update(self): | ||
| """Get the latest data from NetAtmo API and updates the states.""" | ||
| self.netatmo_data.update() | ||
| self._state = self.netatmo_data.data | ||
|
|
||
|
|
||
| class NetatmoPublicData: | ||
| """Get the latest data from NetAtmo.""" | ||
|
|
||
| def __init__(self, auth, lat_ne, lon_ne, lat_sw, lon_sw, calculation): | ||
| """Initialize the data object.""" | ||
| self.auth = auth | ||
| self.data = None | ||
| self.lat_ne = lat_ne | ||
| self.lon_ne = lon_ne | ||
| self.lat_sw = lat_sw | ||
| self.lon_sw = lon_sw | ||
| self.calculation = calculation | ||
|
|
||
| @Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
| def update(self): | ||
| """Request an update from the Netatmo API.""" | ||
| import pyatmo | ||
| _LOGGER.error('Updating Netatmo data.') | ||
| raindata = pyatmo.PublicData(self.auth, | ||
| LAT_NE=self.lat_ne, | ||
| LON_NE=self.lon_ne, | ||
| LAT_SW=self.lat_sw, | ||
| LON_SW=self.lon_sw, | ||
| required_data_type="rain") | ||
|
|
||
| if raindata.CountStationInArea() == 0: | ||
| _LOGGER.warning('No Rain Station available in this area.') | ||
| return | ||
|
|
||
| raindata_live = raindata.getLive() | ||
|
|
||
| if self.calculation == 'avg': | ||
| self.data = sum(raindata_live.values()) / len(raindata_live) | ||
| else: | ||
| self.data = max(raindata_live.values()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong log level. We can remove this all together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
definitely - not sure how that sneaked in.
Fixed here: #15966