core/tests/components/sensor/test_darksky.py

78 lines
2.9 KiB
Python
Raw Normal View History

"""The tests for the Dark Sky platform."""
import re
import unittest
from unittest.mock import MagicMock, patch
import forecastio
from requests.exceptions import HTTPError
import requests_mock
from datetime import timedelta
from homeassistant.components.sensor import darksky
from homeassistant.setup import setup_component
from tests.common import load_fixture, get_test_home_assistant
class TestDarkSkySetup(unittest.TestCase):
"""Test the Dark Sky platform."""
def add_entities(self, new_entities, update_before_add=False):
"""Mock add entities."""
if update_before_add:
for entity in new_entities:
entity.update()
for entity in new_entities:
self.entities.append(entity)
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
self.key = 'foo'
self.config = {
'api_key': 'foo',
Added forecast support to DarkSky (#5264) * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Fix async_volume_up / async_volume_down (#5249) async_volume_up / async_volume_down should be async versions of volume_up / volume_down, not a async version of the default variants of volume_up / volume_down. The previous code always called into the mediaplayers set_volume_level, and never into volume_up / volume_down. Signed-off-by: Anton Lundin <glance@acc.umu.se> * adding a default icon "blind" to a PowerView blinds scene. (#5210) * adding a default icon "blind" to a PowerView blinds scene. * Adding icon property to define blind icon. Removed it from the state attributes dict. * fixing lint error * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Use SHA hash to make token harder to guess (#5258) * Use SHA hash to make token harder to guess Use hashlib SHA256 to encode object id instead of using it directly. * Cache access token Instead of generating a token on the fly cache it in the constructor. * Fix lint * Bugfix async device_tracker see callback (#5259) * Add support for NAD receivers (#5191) * Add support for NAD receivers * remove self.update() in various methods * remove setting attributes in various methods * Change import to hass style * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Revert "Bugfix camera streams (#5306)" This reverts commit 4b43537801a5c088329f6b12c99c95fdb2eb0e9c. Revert "Version bump for kodi dependency (#5307)" This reverts commit 6abad6b76e610b1bfb13f3f9342a2a0a53971fcf. Revert "Add HMWIOSwitch to sensor, binary (#5304)" This reverts commit 2c3f55acc4cc8890e54bf6a94f5a960eee28c486. Revert "Remove GTFS default name & string change" This reverts commit 6000c59bb559b8e37553b3f0def79c2bd84f2af2. Revert "Update pyhomematic 1.19 & small cleanups (#5299)" This reverts commit a30711f1a0e2d4a286799d714fe59ff147883fab. Revert "[sensor] Add Dublin bus RTPI sensor (#5257)" This reverts commit 1219ca3c3bc083c8f919c4db7eb3670686e52861. Revert "Bugfix group reload (#5292)" This reverts commit baa8e53e66167a1fb0f9d090f28325454ad3d4ef. Revert "Support for TrackR device trackers (#5010)" This reverts commit f7a1d63d52dc7687a07cd2c52ef4e8e6894e45d9. Revert "Bump pywemo version." This reverts commit dc937cc8cffbb9ec2b4342d801f8d7332a8dd9cf. Revert "Upgrade to voluptuous to 0.9.3 (#5288)" This reverts commit d12decc4714cb61af58ab08581712b8be5367960. Revert "Upgrade distro to 1.0.2 (#5291)" This reverts commit 64800fd48c02520b1f44be960dc8c539f82d1692. Revert "Don't build Adafruit_BBIO - doesn't work on all platforms. (#5281)" This reverts commit 9a3c0c8cd3a06d118cfcf58d1078912e41f12f31. Revert "Convert flic to synchronous platform. (#5276)" This reverts commit eb9b95c2922181b097258856af9bd2bc4d7a814e. Revert "Upgrade to aiohttp 1.2 (#4964)" This reverts commit e68e29e03ebd43175761d1ae2b4e598d382d2cf4. Revert "Fix TCP sensor to correctly use value_template (#5211)" This reverts commit 1cf9ae5a01d663bb9e3d3e38741b2ae818b36f93. Revert "Cleanup language support on TTS (#5255)" This reverts commit 3f3a3bcc8ac7eec2e5e9eba9981c74db3842f22d. Revert "Add last triggered to script (#5261)" This reverts commit 467cb18625da9323f743ed62a342e446a79fb05b. Revert "Bump flux_led version and make use of PyPi package (#5267)" This reverts commit 34a9fb01ac1fb9568f18677be5faf3d23ab7dc2a. Revert "Add support for NAD receivers (#5191)" This reverts commit 3b59e169f1bc11b3887bc98b2f8425f6c70a0df2. Revert "Bugfix async device_tracker see callback (#5259)" This reverts commit 71fddd26eb9c9ffe6cbd809298f07e17aad152a4. Revert "Use SHA hash to make token harder to guess (#5258)" This reverts commit 922308bc1f7a2a0a769a8c29d663c90a97a0583b. * Revert "Revert "Bugfix camera streams (#5306)"" This reverts commit 2ee8c44021cf9c3a91d20f9ee26752aa8369d2e6. * Update darksky.py
2017-01-27 06:32:45 +00:00
'forecast': [1, 2],
'monitored_conditions': ['summary', 'icon', 'temperature_max'],
'update_interval': timedelta(seconds=120),
}
self.lat = self.hass.config.latitude = 37.8267
self.lon = self.hass.config.longitude = -122.423
self.entities = []
2017-01-07 00:47:25 +00:00
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
def test_setup_with_config(self):
"""Test the platform setup with configuration."""
self.assertTrue(
setup_component(self.hass, 'sensor', {'darksky': self.config}))
@patch('forecastio.api.get_forecast')
def test_setup_bad_api_key(self, mock_get_forecast):
"""Test for handling a bad API key."""
# The Dark Sky API wrapper that we use raises an HTTP error
# when you try to use a bad (or no) API key.
url = 'https://api.darksky.net/forecast/{}/{},{}?units=auto'.format(
self.key, str(self.lat), str(self.lon)
)
msg = '400 Client Error: Bad Request for url: {}'.format(url)
mock_get_forecast.side_effect = HTTPError(msg,)
response = darksky.setup_platform(self.hass, self.config, MagicMock())
self.assertFalse(response)
@requests_mock.Mocker()
@patch('forecastio.api.get_forecast', wraps=forecastio.api.get_forecast)
def test_setup(self, mock_req, mock_get_forecast):
"""Test for successfully setting up the forecast.io platform."""
uri = (r'https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/'
r'(-?\d+\.?\d*),(-?\d+\.?\d*)')
mock_req.get(re.compile(uri),
text=load_fixture('darksky.json'))
darksky.setup_platform(self.hass, self.config, self.add_entities)
self.assertTrue(mock_get_forecast.called)
self.assertEqual(mock_get_forecast.call_count, 1)
Added forecast support to DarkSky (#5264) * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Fix async_volume_up / async_volume_down (#5249) async_volume_up / async_volume_down should be async versions of volume_up / volume_down, not a async version of the default variants of volume_up / volume_down. The previous code always called into the mediaplayers set_volume_level, and never into volume_up / volume_down. Signed-off-by: Anton Lundin <glance@acc.umu.se> * adding a default icon "blind" to a PowerView blinds scene. (#5210) * adding a default icon "blind" to a PowerView blinds scene. * Adding icon property to define blind icon. Removed it from the state attributes dict. * fixing lint error * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Use SHA hash to make token harder to guess (#5258) * Use SHA hash to make token harder to guess Use hashlib SHA256 to encode object id instead of using it directly. * Cache access token Instead of generating a token on the fly cache it in the constructor. * Fix lint * Bugfix async device_tracker see callback (#5259) * Add support for NAD receivers (#5191) * Add support for NAD receivers * remove self.update() in various methods * remove setting attributes in various methods * Change import to hass style * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Added forecast support to DarkSky modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py * Updated Config Validation, extended daily forecast to all supported types * Fix style errors from previous commit, fix test since adding daily for all supported types * Removed temperature from daily as it isn't supported * Revert "Bugfix camera streams (#5306)" This reverts commit 4b43537801a5c088329f6b12c99c95fdb2eb0e9c. Revert "Version bump for kodi dependency (#5307)" This reverts commit 6abad6b76e610b1bfb13f3f9342a2a0a53971fcf. Revert "Add HMWIOSwitch to sensor, binary (#5304)" This reverts commit 2c3f55acc4cc8890e54bf6a94f5a960eee28c486. Revert "Remove GTFS default name & string change" This reverts commit 6000c59bb559b8e37553b3f0def79c2bd84f2af2. Revert "Update pyhomematic 1.19 & small cleanups (#5299)" This reverts commit a30711f1a0e2d4a286799d714fe59ff147883fab. Revert "[sensor] Add Dublin bus RTPI sensor (#5257)" This reverts commit 1219ca3c3bc083c8f919c4db7eb3670686e52861. Revert "Bugfix group reload (#5292)" This reverts commit baa8e53e66167a1fb0f9d090f28325454ad3d4ef. Revert "Support for TrackR device trackers (#5010)" This reverts commit f7a1d63d52dc7687a07cd2c52ef4e8e6894e45d9. Revert "Bump pywemo version." This reverts commit dc937cc8cffbb9ec2b4342d801f8d7332a8dd9cf. Revert "Upgrade to voluptuous to 0.9.3 (#5288)" This reverts commit d12decc4714cb61af58ab08581712b8be5367960. Revert "Upgrade distro to 1.0.2 (#5291)" This reverts commit 64800fd48c02520b1f44be960dc8c539f82d1692. Revert "Don't build Adafruit_BBIO - doesn't work on all platforms. (#5281)" This reverts commit 9a3c0c8cd3a06d118cfcf58d1078912e41f12f31. Revert "Convert flic to synchronous platform. (#5276)" This reverts commit eb9b95c2922181b097258856af9bd2bc4d7a814e. Revert "Upgrade to aiohttp 1.2 (#4964)" This reverts commit e68e29e03ebd43175761d1ae2b4e598d382d2cf4. Revert "Fix TCP sensor to correctly use value_template (#5211)" This reverts commit 1cf9ae5a01d663bb9e3d3e38741b2ae818b36f93. Revert "Cleanup language support on TTS (#5255)" This reverts commit 3f3a3bcc8ac7eec2e5e9eba9981c74db3842f22d. Revert "Add last triggered to script (#5261)" This reverts commit 467cb18625da9323f743ed62a342e446a79fb05b. Revert "Bump flux_led version and make use of PyPi package (#5267)" This reverts commit 34a9fb01ac1fb9568f18677be5faf3d23ab7dc2a. Revert "Add support for NAD receivers (#5191)" This reverts commit 3b59e169f1bc11b3887bc98b2f8425f6c70a0df2. Revert "Bugfix async device_tracker see callback (#5259)" This reverts commit 71fddd26eb9c9ffe6cbd809298f07e17aad152a4. Revert "Use SHA hash to make token harder to guess (#5258)" This reverts commit 922308bc1f7a2a0a769a8c29d663c90a97a0583b. * Revert "Revert "Bugfix camera streams (#5306)"" This reverts commit 2ee8c44021cf9c3a91d20f9ee26752aa8369d2e6. * Update darksky.py
2017-01-27 06:32:45 +00:00
self.assertEqual(len(self.entities), 7)