Restore states for RFLink devices (#18816)

* Merge branch 'master' of https://github.com/home-assistant/home-assistant into dev

# Conflicts:
#	homeassistant/components/binary_sensor/point.py
#	homeassistant/components/cloud/__init__.py
#	homeassistant/components/cloud/prefs.py
#	homeassistant/components/frontend/__init__.py
#	homeassistant/components/light/fibaro.py
#	homeassistant/components/logbook.py
#	homeassistant/components/point/__init__.py
#	homeassistant/config_entries.py
#	homeassistant/const.py
#	homeassistant/helpers/service.py
#	requirements_all.txt
#	requirements_test_all.txt

* one 'async_get_last_state' refactor left behind

* Remove RestoreEntity inheritance (already in parent class)

* # pylint: disable=too-many-ancestors

* code predictor can be a bitch

* lint corrections

* # pylint: disable=too-many-ancestors

* recover from dict[key]

* Remove all 'coroutine' decorator, replace for 'async def'
Replace all 'yield from' for 'await'
Replace 'hass.async_add_job' for 'hass.async_create_task'
This commit is contained in:
javicalle 2018-12-11 17:20:30 +01:00 committed by Martin Hjelmare
parent 3e7b908a61
commit 61ca9bb8e4
7 changed files with 649 additions and 16 deletions

View file

@ -1,7 +1,7 @@
"""Test for RFlink light components.
"""Test for RFLink light components.
Test setup of rflink lights component/platform. State tracking and
control of Rflink switch devices.
Test setup of RFLink lights component/platform. State tracking and
control of RFLink switch devices.
"""
@ -10,9 +10,10 @@ import asyncio
from homeassistant.components.light import ATTR_BRIGHTNESS
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON)
from homeassistant.core import callback
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON, STATE_OFF)
from homeassistant.core import callback, State, CoreState
from tests.common import mock_restore_cache
from ..test_rflink import mock_rflink
DOMAIN = 'light'
@ -44,7 +45,7 @@ CONFIG = {
@asyncio.coroutine
def test_default_setup(hass, monkeypatch):
"""Test all basic functionality of the rflink switch component."""
"""Test all basic functionality of the RFLink switch component."""
# setup mocking rflink module
event_callback, create, protocol, _ = yield from mock_rflink(
hass, CONFIG, DOMAIN, monkeypatch)
@ -119,7 +120,7 @@ def test_default_setup(hass, monkeypatch):
assert hass.states.get(DOMAIN + '.protocol2_0_1').state == 'on'
# test changing state from HA propagates to Rflink
# test changing state from HA propagates to RFLink
hass.async_add_job(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
@ -175,7 +176,7 @@ def test_default_setup(hass, monkeypatch):
@asyncio.coroutine
def test_firing_bus_event(hass, monkeypatch):
"""Incoming Rflink command events should be put on the HA event bus."""
"""Incoming RFLink command events should be put on the HA event bus."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
@ -560,3 +561,56 @@ def test_disable_automatic_add(hass, monkeypatch):
# make sure new device is not added
assert not hass.states.get(DOMAIN + '.protocol_0_0')
@asyncio.coroutine
def test_restore_state(hass, monkeypatch):
"""Ensure states are restored on startup."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'devices': {
'NewKaku_12345678_0': {
'name': 'l1',
'type': 'hybrid',
},
'test_restore_2': {
'name': 'l2',
},
'test_restore_3': {
'name': 'l3',
},
},
},
}
mock_restore_cache(hass, (
State(DOMAIN + '.l1', STATE_ON, {ATTR_BRIGHTNESS: "123", }),
State(DOMAIN + '.l2', STATE_ON, {ATTR_BRIGHTNESS: "321", }),
State(DOMAIN + '.l3', STATE_OFF, ),
))
hass.state = CoreState.starting
# setup mocking rflink module
_, _, _, _ = yield from mock_rflink(hass, config, DOMAIN, monkeypatch)
# dimmable light must restore brightness
state = hass.states.get(DOMAIN + '.l1')
assert state
assert state.state == STATE_ON
assert state.attributes[ATTR_BRIGHTNESS] == 123
# normal light do NOT must restore brightness
state = hass.states.get(DOMAIN + '.l2')
assert state
assert state.state == STATE_ON
assert not state.attributes.get(ATTR_BRIGHTNESS)
# OFF state also restores (or not)
state = hass.states.get(DOMAIN + '.l3')
assert state
assert state.state == STATE_OFF