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

Create ecobee weather platform #10869

Merged
merged 3 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/components/ecobee.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from homeassistant.util import Throttle
from homeassistant.util.json import save_json

REQUIREMENTS = ['python-ecobee-api==0.0.11']
REQUIREMENTS = ['python-ecobee-api==0.0.12']

_CONFIGURING = {}
_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -82,6 +82,7 @@ def setup_ecobee(hass, network, config):
hass, 'climate', DOMAIN, {'hold_temp': hold_temp}, config)
discovery.load_platform(hass, 'sensor', DOMAIN, {}, config)
discovery.load_platform(hass, 'binary_sensor', DOMAIN, {}, config)
discovery.load_platform(hass, 'weather', DOMAIN, {}, config)


class EcobeeData(object):
Expand Down
146 changes: 146 additions & 0 deletions homeassistant/components/weather/ecobee.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""
Support for displaying weather info from Ecobee API.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/weather.ecobee/
"""
import logging
from homeassistant.components import ecobee
from homeassistant.components.weather import (
WeatherEntity, ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME)
from homeassistant.const import (STATE_UNKNOWN, TEMP_FAHRENHEIT)


DEPENDENCIES = ['ecobee']

ATTR_FORECAST_CONDITION = 'condition'
ATTR_FORECAST_TEMP_LOW = 'templow'
ATTR_FORECAST_TEMP_HIGH = 'temphigh'
ATTR_FORECAST_PRESSURE = 'pressure'
ATTR_FORECAST_VISIBILITY = 'visibility'
ATTR_FORECAST_WIND_SPEED = 'windspeed'
ATTR_FORECAST_HUMIDITY = 'humidity'

MISSING_DATA = -5002


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Ecobee weather component."""
if discovery_info is None:
return
dev = list()
data = ecobee.NETWORK
for index in range(len(data.ecobee.thermostats)):
thermostat = data.ecobee.get_thermostat(index)
if 'weather' in thermostat:
dev.append(EcobeeWeather(thermostat['name'], index))

add_devices(dev, True)


class EcobeeWeather(WeatherEntity):
"""Representation of Ecobee weather data."""

def __init__(self, name, index):
"""Initialize the sensor."""
self._name = name
self._index = index
self.weather = None

def get_forecast(self, index, param):
"""Retrieve forecast parameter."""
try:
forecast = self.weather['forecasts'][index]
return forecast[param]
except (ValueError, IndexError):
return STATE_UNKNOWN

@property
def name(self):
"""Return the name of the sensor."""
return self._name

@property
def condition(self):
"""Return the current condition."""
return self.get_forecast(0, 'condition')

@property
def temperature(self):
"""Return the temperature."""
return float(self.get_forecast(0, 'temperature')) / 10
Copy link
Member

Choose a reason for hiding this comment

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

This will raise an error if get_forecast returns STATE_UNKNOWN.


@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_FAHRENHEIT

@property
def pressure(self):
"""Return the pressure."""
return int(self.get_forecast(0, 'pressure'))
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.


@property
def humidity(self):
"""Return the humidity."""
return int(self.get_forecast(0, 'relativeHumidity'))
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.


@property
def visibility(self):
"""Return the visibility."""
return int(self.get_forecast(0, 'visibility'))
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.


@property
def wind_speed(self):
"""Return the wind speed."""
return int(self.get_forecast(0, 'windSpeed'))
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.


@property
def wind_bearing(self):
"""Return the wind direction."""
return int(self.get_forecast(0, 'windBearing'))
Copy link
Member

Choose a reason for hiding this comment

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

Same as above.


@property
def attribution(self):
"""Return the attribution."""
station = self.weather['weatherStation']
time = self.weather['timestamp']
return "Ecobee weather provided by " + station + " at " + time
Copy link
Member

Choose a reason for hiding this comment

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

Use string formatting.

"blabla bla {} bla {}".format(station, time)


@property
def forecast(self):
"""Return the forecast array."""
try:
forecasts = []
for day in self.weather['forecasts']:
forecast = {
ATTR_FORECAST_TIME: day['dateTime'],
ATTR_FORECAST_CONDITION: day['condition'],
ATTR_FORECAST_TEMP: float(day['tempHigh']) / 10,
}
if day['tempHigh'] == MISSING_DATA:
break
if day['tempLow'] != MISSING_DATA:
forecast[ATTR_FORECAST_TEMP_LOW] = \
float(day['tempLow']) / 10
if day['pressure'] != MISSING_DATA:
forecast[ATTR_FORECAST_PRESSURE] = int(day['pressure'])
if day['windSpeed'] != MISSING_DATA:
forecast[ATTR_FORECAST_WIND_SPEED] = int(day['windSpeed'])
if day['visibility'] != MISSING_DATA:
forecast[ATTR_FORECAST_WIND_SPEED] = int(day['visibility'])
if day['relativeHumidity'] != MISSING_DATA:
forecast[ATTR_FORECAST_HUMIDITY] = \
int(day['relativeHumidity'])
forecasts.append(forecast)
return forecasts
except (ValueError, IndexError):
return STATE_UNKNOWN

def update(self):
"""Get the latest state of the sensor."""
data = ecobee.NETWORK
data.update()
thermostat = data.ecobee.get_thermostat(self._index)
self.weather = thermostat.get('weather', None)
logging.error("Weather Update")
Copy link
Member

Choose a reason for hiding this comment

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

Don't do this. You should add a default logger for this module at the top, then use that. The log level here should be debug, and maybe add self.weather to the log.

2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ python-clementine-remote==1.0.1
python-digitalocean==1.12

# homeassistant.components.ecobee
python-ecobee-api==0.0.11
python-ecobee-api==0.0.12

# homeassistant.components.climate.eq3btsmart
# python-eq3bt==0.1.6
Expand Down