Fix and improvment of Swiss Hydrological Data component#17166
Fix and improvment of Swiss Hydrological Data component#17166fabaff merged 5 commits intohome-assistant:devfrom Bouni:fix-swiss-hydrological-data
Conversation
|
The issue with the proposed solution is that every instance is going to download around 800 kB of data per hour (approx. 30-40 kB per file) when all sensors are activate but only one value per file is of interest. Thus we are wasting a lot of resources. When I wrote the integration the FOEN agreed on a automatic approach to get the XML file with the data from ftp.hydrodata.ch. Now the access requires credentials which made our integration non-functional. I see three way to fix it:
|
|
@fabaff |
|
@fabaff
Anyway, the signaled that if we could have our own backend (as you've suggested) we could get an account for that. What exactly did you mean by "publish the content by ourself as RESTFul API" where would that service be hosted? Its obviously not a big deal writing the interpreter and REST API, but who is going to host it? Or are there already other components relying on such an HA provided API which could be implemented for this component as well? |
|
Just a quick update: |
|
If you give me access to the API then I can create a simple client which we then can use for this sensor. |
|
@fabaff I still wait for the credentials, at the moment the API just respondes data from a single static file they gave me for reference. I'll ping you as soon as the API is live! |
|
@fabaff I think I'm done with my API but I still wait for the login data from FEON.
What exactly do you mean by "simple client" ? |
|
Just got an E-Mail, my account at FEON will be ready by 5th of November. |
A module that is performing the requests and process the received data. |
|
I've already done that with this https://pypi.org/project/swisshydrodata/ I would like to test the component for a few days to see if there are any problems and then update my pull request. Is there anything you want me to do / to test before I update the PR? |
Nice |
| attributes[ATTR_LOCATION] = self.data.measurings['location'] | ||
| attributes[ATTR_UPDATE] = self.data.measurings['update_time'] | ||
| attributes[ATTR_ATTRIBUTION] = CONF_ATTRIBUTION | ||
| return attributes |
There was a problem hiding this comment.
This is a breaking change which needs an entry in the release notes.
| attributes[ATTR_ATTRIBUTION] = CONF_ATTRIBUTION | ||
| return attributes | ||
| return { | ||
| ATTR_ATTRIBUTION: CONF_ATTRIBUTION |
There was a problem hiding this comment.
How about adding the station details? Location, etc.
| data["max_discharge"] = shd.get_max_discharge() | ||
| data["mean_temperature"] = shd.get_mean_temperature() | ||
| data["mean_level"] = shd.get_mean_level() | ||
| data["mean_discharge"] = shd.get_mean_discharge() |
There was a problem hiding this comment.
I'm not so sure that this is the right approach resource-wise. There will be a dozen requests for every update to get the data instead of one that is sharing the data.
You want to change the sensor style (single sensors instead of attributes), thus you should only get the data, e. g., for mean_temperature that's needed and not everything that is available. BTW, there is a chance (home-assistant/architecture#100) that it needs to be updated/changed again soon.
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_STATION): vol.Coerce(int), | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| vol.Required(CONF_MEASUREMENTS): vol.Any([ |
There was a problem hiding this comment.
That's a breaking change. Please check the documentation for the validation to check that it's a list.
| _LOGGER.error("The URL is not accessible") | ||
| data = HydrologicalData(station) | ||
|
|
||
| response = requests.get( |
There was a problem hiding this comment.
Should be tested with swisshydrodata as shd should be created here anyway and passed to HydrologicalData.
| ) | ||
| if response.status_code != 200: | ||
| _LOGGER.error("The given station does not exist: %s", station) | ||
| return False |
| if self.hydro_data.data is None: | ||
| self._state = STATE_UNKNOWN | ||
| else: | ||
| self._state = self.hydro_data.data["parameters"][self._measurement][self._value] |
| "ATTR_WATER_BODY": self.hydro_data.data["water-body-name"], | ||
| "ATTR_WATER_BODY_TYPE": self.hydro_data.data["water-body-type"], | ||
| "ATTR_STATION": self.hydro_data.data["name"], | ||
| "ATTR_UPDATE": self.hydro_data.data["parameters"][self._measurement]["datetime"], |
| """Return the unit of measurement of this entity, if any.""" | ||
| if self._state is not STATE_UNKNOWN: | ||
| return self._unit_of_measurement | ||
| return self.hydro_data.data["parameters"][self._measurement]["unit"] |
There was a problem hiding this comment.
@fabaff Whats the pythonic way for breaking lines like this so that I meet the hounds requirements?
There was a problem hiding this comment.
There seems no other way than between the brackets here.
There was a problem hiding this comment.
I tried several ways and I always get lint errors, so I used this "trick":
https://github.com/home-assistant/home-assistant/blob/0dd43bb424ef846f18cc33d8dd28873fcf38f452/homeassistant/components/sensor/swiss_hydrological_data.py#L138-L142
| vol.Required(CONF_MONITORED_CONDITIONS): vol.Schema({ | ||
| "temperature": vol.All(cv.ensure_list, [vol.In(CONDITIONS)]), | ||
| "level": vol.All(cv.ensure_list, [vol.In(CONDITIONS)]), | ||
| "discharge": vol.All(cv.ensure_list, [vol.In(CONDITIONS)]) |
There was a problem hiding this comment.
@fabaff Is this what you meant by ensuring that the conditions are a list?
| self._measurement, | ||
| self._station, | ||
| self._condition, | ||
| self._value) |
There was a problem hiding this comment.
@fabaff What do you think about the way I name the sensors? Is that a good way?
There was a problem hiding this comment.
I would go with the water body for the name by default.
There was a problem hiding this comment.
The "problem" is that the waterbody name is something I get from the API and if its down for some reason I cannot know the waterbody name when setting up the entities. Thats why I went for the station ID. The user still can set a friendly_name, right!?
| "ATTR_UPDATE": | ||
| self.hydro_data.data["parameters"][self._condition]["datetime"], | ||
| "ATTR_ATTRIBUTION": CONF_ATTRIBUTION | ||
| } |
There was a problem hiding this comment.
You removed the definition of all constants (line 33-42) and made the key strings in the dict. You need to add the missing one (like ATTR_WATER_BODY).
There was a problem hiding this comment.
Ok, got that, I'll fix it today if I find some spare time.
|
💩 I think I've messed someing up |
- Simplify the sensor configuration (expose value as attributes rather than sensor) - Make the setup fail if station is not available - Add unique ID - Prepare for config flow
|
@swissuser31415 this is a pull request from 2018, please don't bump old/closed/merged/handled PRs. If you need support, please see our website on where to go: https://www.home-assistant.io/help Thanks! 👍 |



Description:
Fix for the not working Swiss Hydrological Data component.
Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#6535
Example entry for
configuration.yaml(if applicable):Checklist:
tox. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
REQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices: