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

Added Met Office weather and sensor components #6742

Merged
merged 11 commits into from Mar 31, 2017

Conversation

jacobtomlinson
Copy link
Contributor

@jacobtomlinson jacobtomlinson commented Mar 22, 2017

Description:
Get weather data from the Met Office's DataPoint API.

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

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: metoffice
    api_key: "myapikey"
    monitored_conditions:
      - temperature
      - weather

weather:
  - platform: metoffice
    api_key: "myapikey"

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

@homeassistant
Copy link
Contributor

Hi @jacobtomlinson,

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!

"""

import logging
import datetime

Choose a reason for hiding this comment

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

'datetime' imported but unused

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved

@jacobtomlinson
Copy link
Contributor Author

This has been raised to replace #5463. However it's not 100% finished and is still undergoing some testing.

@jacobtomlinson
Copy link
Contributor Author

Good to go at my end

REQUIREMENTS = ['datapoint==0.4.3']

CONF_ATTRIBUTION = "Data provided by the Met Office"
CONF_MO_API_KEY = 'api_key'
Copy link
Member

Choose a reason for hiding this comment

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

We have CONF_API_KEY in const.py. i don't see a good reason to introduce CONF_MO_API_KEY.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

'weather': ['Weather', None],
'temperature': ['Temperature', TEMP_CELSIUS],
'feels_like_temperature': ['Feels Like Temperature', TEMP_CELSIUS],
'wind_speed': ['Wind Speed', 'mps'],
Copy link
Member

Choose a reason for hiding this comment

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

Other platforms are using m/s.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@property
def name(self):
"""Return the name of the sensor."""
return 'Met Office {}'.format(SENSOR_TYPES[self._condition][0])
Copy link
Member

Choose a reason for hiding this comment

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

Often DEFAULT_NAME is used if the user is not giving a name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is in keeping with the bom component. Also your comment here is at odds with your comment on the weather component about using CONF_NAME.

Do you suggest using CONF_NAME if it has been set and DEFAULT_NAME otherwise?

Copy link
Member

@fabaff fabaff Mar 26, 2017

Choose a reason for hiding this comment

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

This is useful for users who are enabling multiple platform.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I'll get that done.

Support for UK Met Office weather service.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.metoffice/
Copy link
Member

Choose a reason for hiding this comment

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

Link needs to match the component otherwise the error message of the configuration check points to the wrong location. sensor.* -> weather.*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

datapoint = dp.connection(api_key=config.get(CONF_MO_API_KEY))

if None in (hass.config.latitude, hass.config.longitude):
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
Copy link
Member

Choose a reason for hiding this comment

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

To make this platform more generic, I would like to suggest that the user can specify the location in the configuration and use the data from a different location.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Will add this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. User's can specify latitude and longitude in the component configs, and if they do not it will default to their home latitude and longitude.

Documentation also updated in home-assistant/home-assistant.io#2318.

@property
def condition(self):
"""Return the current condition."""
return self.data.data.weather.text
Copy link
Member

Choose a reason for hiding this comment

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

The condition should be mapped to an entry from CONDITION_CLASSES. This will make it possible in the future to show a visual representation of the condition in the frontend.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, will add this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@property
def name(self):
"""Return the name of the sensor."""
return 'Met Office ({})'.format(self.site.name)
Copy link
Member

Choose a reason for hiding this comment

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

Using CONF_NAME would give the user more flexibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See comment on other name property review.

self._datapoint = datapoint
self._site = site
self.data = None
self.lastupdate = LAST_UPDATE
Copy link
Member

Choose a reason for hiding this comment

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

Is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, removed

if not site:
_LOGGER.error("Unable to get nearest Met Office forecast site")
return False
else:
Copy link
Member

Choose a reason for hiding this comment

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

We can drop the else and adjust the indent since the if statement returns False.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

from homeassistant.components.weather import WeatherEntity, PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, TEMP_CELSIUS
from homeassistant.helpers import config_validation as cv
# Reuse data and API logic from the sensor implementation
Copy link
Member

Choose a reason for hiding this comment

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

From my point of view it should be the other way around. The weather platform should act as the primary implementation and the sensor platform as a dependent.

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'm not sure I agree. I admit the reason I did it this way around is because it's the way the bom component did it. However it feels like the weather component is a collection of sensors, therefore it makes sense for the weather component to import the sensors.

Copy link
Member

Choose a reason for hiding this comment

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

We created the weather component to replace the sensor platforms for meteorological data.

So, I tend to say that the weather component should be the main implementation because there will be a frontend element some day and the sensor platforms will be obsolete. But as we are moving slowing in this regard I will leave it up to you to decide what platform should be the main implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for explaining, makes a lot of sense. I'll update it so the weather component is the primary.


from homeassistant.components.weather import WeatherEntity, PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, TEMP_CELSIUS, CONF_API_KEY,
CONF_LATITUDE, CONF_LONGITUDE

Choose a reason for hiding this comment

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

IndentationError: unexpected indent
unexpected indentation

datapoint = dp.connection(api_key=config.get(CONF_API_KEY))

latitude = config.get(CONF_LATITUDE, hass.config.latitude)
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)

Choose a reason for hiding this comment

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

undefined name 'CONF_LONGITUDE'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

import datapoint as dp
datapoint = dp.connection(api_key=config.get(CONF_API_KEY))

latitude = config.get(CONF_LATITUDE, hass.config.latitude)

Choose a reason for hiding this comment

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

undefined name 'CONF_LATITUDE'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@jacobtomlinson
Copy link
Contributor Author

@fabaff I've finished working through your review. There were three items I disagreed with or had questions about. Would you mind updating these?

@fabaff fabaff merged commit 2d6b095 into home-assistant:dev Mar 31, 2017
@fabaff fabaff mentioned this pull request Apr 6, 2017
@cannfoddr
Copy link

This is not working for me and a few others over on the forums. Where can I log an Issue with the developer?

@jacobtomlinson
Copy link
Contributor Author

jacobtomlinson commented May 3, 2017 via email

@cannfoddr
Copy link

cannfoddr commented May 3, 2017 via email

@cannfoddr
Copy link

cannfoddr commented May 5, 2017 via email

@home-assistant home-assistant locked and limited conversation to collaborators Aug 12, 2017
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.

None yet

6 participants