mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Add tests for device_sun_light_trigger
This commit is contained in:
parent
ef470de3ca
commit
fd67e47128
@ -26,6 +26,10 @@ class MockScanner(object):
|
|||||||
""" Make a device leave the house. """
|
""" Make a device leave the house. """
|
||||||
self.devices_home.remove(device)
|
self.devices_home.remove(device)
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
""" Resets which devices are home. """
|
||||||
|
self.devices_home = []
|
||||||
|
|
||||||
def scan_devices(self):
|
def scan_devices(self):
|
||||||
""" Returns a list of fake devices. """
|
""" Returns a list of fake devices. """
|
||||||
|
|
||||||
|
@ -5,10 +5,14 @@ tests.helper
|
|||||||
Helper method for writing tests.
|
Helper method for writing tests.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
import homeassistant as ha
|
import homeassistant as ha
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
from homeassistant.helpers.entity import ToggleEntity
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME
|
from homeassistant.const import (
|
||||||
|
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED)
|
||||||
|
from homeassistant.components import sun
|
||||||
|
|
||||||
|
|
||||||
def get_test_config_dir():
|
def get_test_config_dir():
|
||||||
@ -20,6 +24,8 @@ def get_test_home_assistant():
|
|||||||
""" Returns a Home Assistant object pointing at test config dir. """
|
""" Returns a Home Assistant object pointing at test config dir. """
|
||||||
hass = ha.HomeAssistant()
|
hass = ha.HomeAssistant()
|
||||||
hass.config.config_dir = get_test_config_dir()
|
hass.config.config_dir = get_test_config_dir()
|
||||||
|
hass.config.latitude = 32.87336
|
||||||
|
hass.config.longitude = -117.22743
|
||||||
|
|
||||||
return hass
|
return hass
|
||||||
|
|
||||||
@ -37,6 +43,32 @@ def mock_service(hass, domain, service):
|
|||||||
return calls
|
return calls
|
||||||
|
|
||||||
|
|
||||||
|
def trigger_device_tracker_scan(hass):
|
||||||
|
""" Triggers the device tracker to scan. """
|
||||||
|
hass.bus.fire(
|
||||||
|
EVENT_TIME_CHANGED,
|
||||||
|
{'now':
|
||||||
|
dt_util.utcnow().replace(second=0) + timedelta(hours=1)})
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_sun_risen(hass):
|
||||||
|
""" Trigger sun to rise if below horizon. """
|
||||||
|
if not sun.is_on(hass):
|
||||||
|
hass.bus.fire(
|
||||||
|
EVENT_TIME_CHANGED,
|
||||||
|
{'now':
|
||||||
|
sun.next_rising_utc(hass) + timedelta(seconds=10)})
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_sun_set(hass):
|
||||||
|
""" Trigger sun to set if above horizon. """
|
||||||
|
if sun.is_on(hass):
|
||||||
|
hass.bus.fire(
|
||||||
|
EVENT_TIME_CHANGED,
|
||||||
|
{'now':
|
||||||
|
sun.next_setting_utc(hass) + timedelta(seconds=10)})
|
||||||
|
|
||||||
|
|
||||||
class MockModule(object):
|
class MockModule(object):
|
||||||
""" Provides a fake module. """
|
""" Provides a fake module. """
|
||||||
|
|
||||||
|
127
tests/test_component_device_sun_light_trigger.py
Normal file
127
tests/test_component_device_sun_light_trigger.py
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
"""
|
||||||
|
tests.test_component_device_sun_light_trigger
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests device sun light trigger component.
|
||||||
|
"""
|
||||||
|
# pylint: disable=too-many-public-methods,protected-access
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import homeassistant.loader as loader
|
||||||
|
from homeassistant.const import CONF_PLATFORM
|
||||||
|
from homeassistant.components import (
|
||||||
|
device_tracker, light, sun, device_sun_light_trigger)
|
||||||
|
|
||||||
|
|
||||||
|
from helpers import (
|
||||||
|
get_test_home_assistant, ensure_sun_risen, ensure_sun_set,
|
||||||
|
trigger_device_tracker_scan)
|
||||||
|
|
||||||
|
|
||||||
|
KNOWN_DEV_PATH = None
|
||||||
|
|
||||||
|
|
||||||
|
def setUpModule(): # pylint: disable=invalid-name
|
||||||
|
""" Initalizes a Home Assistant server. """
|
||||||
|
global KNOWN_DEV_PATH
|
||||||
|
|
||||||
|
hass = get_test_home_assistant()
|
||||||
|
|
||||||
|
loader.prepare(hass)
|
||||||
|
KNOWN_DEV_PATH = hass.config.path(
|
||||||
|
device_tracker.KNOWN_DEVICES_FILE)
|
||||||
|
|
||||||
|
hass.stop()
|
||||||
|
|
||||||
|
with open(KNOWN_DEV_PATH, 'w') as fil:
|
||||||
|
fil.write('device,name,track,picture\n')
|
||||||
|
fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n')
|
||||||
|
fil.write('DEV2,device 2,1,http://example.com/dev2.jpg\n')
|
||||||
|
|
||||||
|
|
||||||
|
def tearDownModule(): # pylint: disable=invalid-name
|
||||||
|
""" Stops the Home Assistant server. """
|
||||||
|
os.remove(KNOWN_DEV_PATH)
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeviceSunLightTrigger(unittest.TestCase):
|
||||||
|
""" Test the device sun light trigger module. """
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
self.hass = get_test_home_assistant()
|
||||||
|
|
||||||
|
self.scanner = loader.get_component(
|
||||||
|
'device_tracker.test').get_scanner(None, None)
|
||||||
|
|
||||||
|
self.scanner.reset()
|
||||||
|
self.scanner.come_home('DEV1')
|
||||||
|
|
||||||
|
loader.get_component('light.test').init()
|
||||||
|
|
||||||
|
device_tracker.setup(self.hass, {
|
||||||
|
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}
|
||||||
|
})
|
||||||
|
|
||||||
|
light.setup(self.hass, {
|
||||||
|
light.DOMAIN: {CONF_PLATFORM: 'test'}
|
||||||
|
})
|
||||||
|
|
||||||
|
sun.setup(self.hass, {})
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
""" Stop down stuff we started. """
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_lights_on_when_sun_sets(self):
|
||||||
|
""" Test lights go on when there is someone home and the sun sets. """
|
||||||
|
|
||||||
|
device_sun_light_trigger.setup(
|
||||||
|
self.hass, {device_sun_light_trigger.DOMAIN: {}})
|
||||||
|
|
||||||
|
ensure_sun_risen(self.hass)
|
||||||
|
|
||||||
|
light.turn_off(self.hass)
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
ensure_sun_set(self.hass)
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertTrue(light.is_on(self.hass))
|
||||||
|
|
||||||
|
def test_lights_turn_off_when_everyone_leaves(self):
|
||||||
|
""" Test lights turn off when everyone leaves the house. """
|
||||||
|
light.turn_on(self.hass)
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
device_sun_light_trigger.setup(
|
||||||
|
self.hass, {device_sun_light_trigger.DOMAIN: {}})
|
||||||
|
|
||||||
|
self.scanner.leave_home('DEV1')
|
||||||
|
|
||||||
|
trigger_device_tracker_scan(self.hass)
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertFalse(light.is_on(self.hass))
|
||||||
|
|
||||||
|
def test_lights_turn_on_when_coming_home_after_sun_set(self):
|
||||||
|
""" Test lights turn on when coming home after sun set. """
|
||||||
|
light.turn_off(self.hass)
|
||||||
|
|
||||||
|
ensure_sun_set(self.hass)
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
device_sun_light_trigger.setup(
|
||||||
|
self.hass, {device_sun_light_trigger.DOMAIN: {}})
|
||||||
|
|
||||||
|
self.scanner.come_home('DEV2')
|
||||||
|
trigger_device_tracker_scan(self.hass)
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertTrue(light.is_on(self.hass))
|
@ -81,6 +81,8 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
|||||||
scanner = loader.get_component(
|
scanner = loader.get_component(
|
||||||
'device_tracker.test').get_scanner(None, None)
|
'device_tracker.test').get_scanner(None, None)
|
||||||
|
|
||||||
|
scanner.reset()
|
||||||
|
|
||||||
scanner.come_home('DEV1')
|
scanner.come_home('DEV1')
|
||||||
scanner.come_home('DEV2')
|
scanner.come_home('DEV2')
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user