Rename MockToggleDevice to MockToggleEntity (#26644)

* Rename MockToggleDevice to MockToggleEntity

* Fix tests
This commit is contained in:
Erik Montnemery 2019-09-15 09:50:17 +02:00 committed by GitHub
parent 57833f5b1e
commit 6a60ebdb30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 200 additions and 198 deletions

View file

@ -602,40 +602,40 @@ class MockEntityPlatform(entity_platform.EntityPlatform):
)
class MockToggleDevice(entity.ToggleEntity):
class MockToggleEntity(entity.ToggleEntity):
"""Provide a mock toggle device."""
def __init__(self, name, state):
"""Initialize the mock device."""
def __init__(self, name, state, unique_id=None):
"""Initialize the mock entity."""
self._name = name or DEVICE_DEFAULT_NAME
self._state = state
self.calls = []
@property
def name(self):
"""Return the name of the device if any."""
"""Return the name of the entity if any."""
self.calls.append(("name", {}))
return self._name
@property
def state(self):
"""Return the name of the device if any."""
"""Return the state of the entity if any."""
self.calls.append(("state", {}))
return self._state
@property
def is_on(self):
"""Return true if device is on."""
"""Return true if entity is on."""
self.calls.append(("is_on", {}))
return self._state == STATE_ON
def turn_on(self, **kwargs):
"""Turn the device on."""
"""Turn the entity on."""
self.calls.append(("turn_on", kwargs))
self._state = STATE_ON
def turn_off(self, **kwargs):
"""Turn the device off."""
"""Turn the entity off."""
self.calls.append(("turn_off", kwargs))
self._state = STATE_OFF

View file

@ -82,10 +82,10 @@ async def test_flux_when_switch_is_off(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -113,7 +113,7 @@ async def test_flux_when_switch_is_off(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
}
},
)
@ -131,10 +131,10 @@ async def test_flux_before_sunrise(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -162,7 +162,7 @@ async def test_flux_before_sunrise(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
}
},
)
@ -184,10 +184,10 @@ async def test_flux_before_sunrise_known_location(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -210,7 +210,7 @@ async def test_flux_before_sunrise_known_location(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
# 'brightness': 255,
# 'disable_brightness_adjust': True,
# 'mode': 'rgb',
@ -237,10 +237,10 @@ async def test_flux_after_sunrise_before_sunset(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -267,7 +267,7 @@ async def test_flux_after_sunrise_before_sunset(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
}
},
)
@ -290,10 +290,10 @@ async def test_flux_after_sunset_before_stop(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -320,7 +320,7 @@ async def test_flux_after_sunset_before_stop(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"stop_time": "22:00",
}
},
@ -344,10 +344,10 @@ async def test_flux_after_stop_before_sunrise(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -374,7 +374,7 @@ async def test_flux_after_stop_before_sunrise(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
}
},
)
@ -397,10 +397,10 @@ async def test_flux_with_custom_start_stop_times(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -427,7 +427,7 @@ async def test_flux_with_custom_start_stop_times(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"start_time": "6:00",
"stop_time": "23:30",
}
@ -454,10 +454,10 @@ async def test_flux_before_sunrise_stop_next_day(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -484,7 +484,7 @@ async def test_flux_before_sunrise_stop_next_day(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"stop_time": "01:00",
}
},
@ -512,10 +512,10 @@ async def test_flux_after_sunrise_before_sunset_stop_next_day(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -542,7 +542,7 @@ async def test_flux_after_sunrise_before_sunset_stop_next_day(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"stop_time": "01:00",
}
},
@ -570,10 +570,10 @@ async def test_flux_after_sunset_before_midnight_stop_next_day(hass, x):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -600,7 +600,7 @@ async def test_flux_after_sunset_before_midnight_stop_next_day(hass, x):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"stop_time": "01:00",
}
},
@ -627,10 +627,10 @@ async def test_flux_after_sunset_after_midnight_stop_next_day(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -657,7 +657,7 @@ async def test_flux_after_sunset_after_midnight_stop_next_day(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"stop_time": "01:00",
}
},
@ -684,10 +684,10 @@ async def test_flux_after_stop_before_sunrise_stop_next_day(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -714,7 +714,7 @@ async def test_flux_after_stop_before_sunrise_stop_next_day(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"stop_time": "01:00",
}
},
@ -738,10 +738,10 @@ async def test_flux_with_custom_colortemps(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -768,7 +768,7 @@ async def test_flux_with_custom_colortemps(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"start_colortemp": "1000",
"stop_colortemp": "6000",
"stop_time": "22:00",
@ -794,10 +794,10 @@ async def test_flux_with_custom_brightness(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -824,7 +824,7 @@ async def test_flux_with_custom_brightness(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"brightness": 255,
"stop_time": "22:00",
}
@ -848,23 +848,23 @@ async def test_flux_with_multiple_lights(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1, dev2, dev3 = platform.DEVICES
common_light.turn_on(hass, entity_id=dev2.entity_id)
ent1, ent2, ent3 = platform.ENTITIES
common_light.turn_on(hass, entity_id=ent2.entity_id)
await hass.async_block_till_done()
common_light.turn_on(hass, entity_id=dev3.entity_id)
common_light.turn_on(hass, entity_id=ent3.entity_id)
await hass.async_block_till_done()
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
state = hass.states.get(dev2.entity_id)
state = hass.states.get(ent2.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
state = hass.states.get(dev3.entity_id)
state = hass.states.get(ent3.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("xy_color") is None
assert state.attributes.get("brightness") is None
@ -893,7 +893,7 @@ async def test_flux_with_multiple_lights(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id, dev2.entity_id, dev3.entity_id],
"lights": [ent1.entity_id, ent2.entity_id, ent3.entity_id],
}
},
)
@ -921,10 +921,10 @@ async def test_flux_with_mired(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("color_temp") is None
@ -950,7 +950,7 @@ async def test_flux_with_mired(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"mode": "mired",
}
},
@ -972,10 +972,10 @@ async def test_flux_with_rgb(hass):
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1 = platform.DEVICES[0]
ent1 = platform.ENTITIES[0]
# Verify initial state of light
state = hass.states.get(dev1.entity_id)
state = hass.states.get(ent1.entity_id)
assert STATE_ON == state.state
assert state.attributes.get("color_temp") is None
@ -1001,7 +1001,7 @@ async def test_flux_with_rgb(hass):
switch.DOMAIN: {
"platform": "flux",
"name": "flux",
"lights": [dev1.entity_id],
"lights": [ent1.entity_id],
"mode": "rgb",
}
},

View file

@ -116,7 +116,7 @@ async def test_heater_switch(hass, setup_comp_1):
"""Test heater switching test switch."""
platform = getattr(hass.components, "test.switch")
platform.init()
switch_1 = platform.DEVICES[1]
switch_1 = platform.ENTITIES[1]
assert await async_setup_component(
hass, switch.DOMAIN, {"switch": {"platform": "test"}}
)

View file

@ -150,7 +150,7 @@ async def test_if_fires_on_state_change(hass, calls):
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
@ -162,7 +162,7 @@ async def test_if_fires_on_state_change(hass, calls):
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_on",
},
"action": {
@ -186,7 +186,7 @@ async def test_if_fires_on_state_change(hass, calls):
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_off",
},
"action": {
@ -209,21 +209,21 @@ async def test_if_fires_on_state_change(hass, calls):
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.states.async_set(dev1.entity_id, STATE_OFF)
hass.states.async_set(ent1.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "turn_off state - {} - on - off - None".format(
dev1.entity_id
ent1.entity_id
)
hass.states.async_set(dev1.entity_id, STATE_ON)
hass.states.async_set(ent1.entity_id, STATE_ON)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "turn_on state - {} - off - on - None".format(
dev1.entity_id
ent1.entity_id
)
@ -234,7 +234,7 @@ async def test_if_state(hass, calls):
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
@ -248,7 +248,7 @@ async def test_if_state(hass, calls):
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "is_on",
}
],
@ -267,7 +267,7 @@ async def test_if_state(hass, calls):
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "is_off",
}
],
@ -283,7 +283,7 @@ async def test_if_state(hass, calls):
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.bus.async_fire("test_event1")
@ -292,7 +292,7 @@ async def test_if_state(hass, calls):
assert len(calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1"
hass.states.async_set(dev1.entity_id, STATE_OFF)
hass.states.async_set(ent1.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
@ -307,7 +307,7 @@ async def test_action(hass, calls):
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
@ -319,7 +319,7 @@ async def test_action(hass, calls):
"action": {
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_off",
},
},
@ -328,7 +328,7 @@ async def test_action(hass, calls):
"action": {
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_on",
},
},
@ -337,7 +337,7 @@ async def test_action(hass, calls):
"action": {
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "toggle",
},
},
@ -345,29 +345,29 @@ async def test_action(hass, calls):
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
assert hass.states.get(ent1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
assert hass.states.get(ent1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
assert hass.states.get(ent1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON

View file

@ -137,39 +137,39 @@ class TestLight(unittest.TestCase):
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1, dev2, dev3 = platform.DEVICES
ent1, ent2, ent3 = platform.ENTITIES
# Test init
assert light.is_on(self.hass, dev1.entity_id)
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
assert light.is_on(self.hass, ent1.entity_id)
assert not light.is_on(self.hass, ent2.entity_id)
assert not light.is_on(self.hass, ent3.entity_id)
# Test basic turn_on, turn_off, toggle services
common.turn_off(self.hass, entity_id=dev1.entity_id)
common.turn_on(self.hass, entity_id=dev2.entity_id)
common.turn_off(self.hass, entity_id=ent1.entity_id)
common.turn_on(self.hass, entity_id=ent2.entity_id)
self.hass.block_till_done()
assert not light.is_on(self.hass, dev1.entity_id)
assert light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, ent1.entity_id)
assert light.is_on(self.hass, ent2.entity_id)
# turn on all lights
common.turn_on(self.hass)
self.hass.block_till_done()
assert light.is_on(self.hass, dev1.entity_id)
assert light.is_on(self.hass, dev2.entity_id)
assert light.is_on(self.hass, dev3.entity_id)
assert light.is_on(self.hass, ent1.entity_id)
assert light.is_on(self.hass, ent2.entity_id)
assert light.is_on(self.hass, ent3.entity_id)
# turn off all lights
common.turn_off(self.hass)
self.hass.block_till_done()
assert not light.is_on(self.hass, dev1.entity_id)
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
assert not light.is_on(self.hass, ent1.entity_id)
assert not light.is_on(self.hass, ent2.entity_id)
assert not light.is_on(self.hass, ent3.entity_id)
# turn off all lights by setting brightness to 0
common.turn_on(self.hass)
@ -180,97 +180,97 @@ class TestLight(unittest.TestCase):
self.hass.block_till_done()
assert not light.is_on(self.hass, dev1.entity_id)
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
assert not light.is_on(self.hass, ent1.entity_id)
assert not light.is_on(self.hass, ent2.entity_id)
assert not light.is_on(self.hass, ent3.entity_id)
# toggle all lights
common.toggle(self.hass)
self.hass.block_till_done()
assert light.is_on(self.hass, dev1.entity_id)
assert light.is_on(self.hass, dev2.entity_id)
assert light.is_on(self.hass, dev3.entity_id)
assert light.is_on(self.hass, ent1.entity_id)
assert light.is_on(self.hass, ent2.entity_id)
assert light.is_on(self.hass, ent3.entity_id)
# toggle all lights
common.toggle(self.hass)
self.hass.block_till_done()
assert not light.is_on(self.hass, dev1.entity_id)
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
assert not light.is_on(self.hass, ent1.entity_id)
assert not light.is_on(self.hass, ent2.entity_id)
assert not light.is_on(self.hass, ent3.entity_id)
# Ensure all attributes process correctly
common.turn_on(
self.hass, dev1.entity_id, transition=10, brightness=20, color_name="blue"
self.hass, ent1.entity_id, transition=10, brightness=20, color_name="blue"
)
common.turn_on(
self.hass, dev2.entity_id, rgb_color=(255, 255, 255), white_value=255
self.hass, ent2.entity_id, rgb_color=(255, 255, 255), white_value=255
)
common.turn_on(self.hass, dev3.entity_id, xy_color=(0.4, 0.6))
common.turn_on(self.hass, ent3.entity_id, xy_color=(0.4, 0.6))
self.hass.block_till_done()
_, data = dev1.last_call("turn_on")
_, data = ent1.last_call("turn_on")
assert {
light.ATTR_TRANSITION: 10,
light.ATTR_BRIGHTNESS: 20,
light.ATTR_HS_COLOR: (240, 100),
} == data
_, data = dev2.last_call("turn_on")
_, data = ent2.last_call("turn_on")
assert {light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255} == data
_, data = dev3.last_call("turn_on")
_, data = ent3.last_call("turn_on")
assert {light.ATTR_HS_COLOR: (71.059, 100)} == data
# Ensure attributes are filtered when light is turned off
common.turn_on(
self.hass, dev1.entity_id, transition=10, brightness=0, color_name="blue"
self.hass, ent1.entity_id, transition=10, brightness=0, color_name="blue"
)
common.turn_on(
self.hass,
dev2.entity_id,
ent2.entity_id,
brightness=0,
rgb_color=(255, 255, 255),
white_value=0,
)
common.turn_on(self.hass, dev3.entity_id, brightness=0, xy_color=(0.4, 0.6))
common.turn_on(self.hass, ent3.entity_id, brightness=0, xy_color=(0.4, 0.6))
self.hass.block_till_done()
assert not light.is_on(self.hass, dev1.entity_id)
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
assert not light.is_on(self.hass, ent1.entity_id)
assert not light.is_on(self.hass, ent2.entity_id)
assert not light.is_on(self.hass, ent3.entity_id)
_, data = dev1.last_call("turn_off")
_, data = ent1.last_call("turn_off")
assert {light.ATTR_TRANSITION: 10} == data
_, data = dev2.last_call("turn_off")
_, data = ent2.last_call("turn_off")
assert {} == data
_, data = dev3.last_call("turn_off")
_, data = ent3.last_call("turn_off")
assert {} == data
# One of the light profiles
prof_name, prof_h, prof_s, prof_bri = "relax", 35.932, 69.412, 144
# Test light profiles
common.turn_on(self.hass, dev1.entity_id, profile=prof_name)
common.turn_on(self.hass, ent1.entity_id, profile=prof_name)
# Specify a profile and a brightness attribute to overwrite it
common.turn_on(self.hass, dev2.entity_id, profile=prof_name, brightness=100)
common.turn_on(self.hass, ent2.entity_id, profile=prof_name, brightness=100)
self.hass.block_till_done()
_, data = dev1.last_call("turn_on")
_, data = ent1.last_call("turn_on")
assert {
light.ATTR_BRIGHTNESS: prof_bri,
light.ATTR_HS_COLOR: (prof_h, prof_s),
} == data
_, data = dev2.last_call("turn_on")
_, data = ent2.last_call("turn_on")
assert {
light.ATTR_BRIGHTNESS: 100,
light.ATTR_HS_COLOR: (prof_h, prof_s),
@ -278,34 +278,34 @@ class TestLight(unittest.TestCase):
# Test bad data
common.turn_on(self.hass)
common.turn_on(self.hass, dev1.entity_id, profile="nonexisting")
common.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5])
common.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2])
common.turn_on(self.hass, ent1.entity_id, profile="nonexisting")
common.turn_on(self.hass, ent2.entity_id, xy_color=["bla-di-bla", 5])
common.turn_on(self.hass, ent3.entity_id, rgb_color=[255, None, 2])
self.hass.block_till_done()
_, data = dev1.last_call("turn_on")
_, data = ent1.last_call("turn_on")
assert {} == data
_, data = dev2.last_call("turn_on")
_, data = ent2.last_call("turn_on")
assert {} == data
_, data = dev3.last_call("turn_on")
_, data = ent3.last_call("turn_on")
assert {} == data
# faulty attributes will not trigger a service call
common.turn_on(
self.hass, dev1.entity_id, profile=prof_name, brightness="bright"
self.hass, ent1.entity_id, profile=prof_name, brightness="bright"
)
common.turn_on(self.hass, dev1.entity_id, rgb_color="yellowish")
common.turn_on(self.hass, dev2.entity_id, white_value="high")
common.turn_on(self.hass, ent1.entity_id, rgb_color="yellowish")
common.turn_on(self.hass, ent2.entity_id, white_value="high")
self.hass.block_till_done()
_, data = dev1.last_call("turn_on")
_, data = ent1.last_call("turn_on")
assert {} == data
_, data = dev2.last_call("turn_on")
_, data = ent2.last_call("turn_on")
assert {} == data
def test_broken_light_profiles(self):
@ -340,24 +340,24 @@ class TestLight(unittest.TestCase):
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1, _, _ = platform.DEVICES
ent1, _, _ = platform.ENTITIES
common.turn_on(self.hass, dev1.entity_id, profile="test")
common.turn_on(self.hass, ent1.entity_id, profile="test")
self.hass.block_till_done()
_, data = dev1.last_call("turn_on")
_, data = ent1.last_call("turn_on")
assert light.is_on(self.hass, dev1.entity_id)
assert light.is_on(self.hass, ent1.entity_id)
assert {light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 100} == data
common.turn_on(self.hass, dev1.entity_id, profile="test_off")
common.turn_on(self.hass, ent1.entity_id, profile="test_off")
self.hass.block_till_done()
_, data = dev1.last_call("turn_off")
_, data = ent1.last_call("turn_off")
assert not light.is_on(self.hass, dev1.entity_id)
assert not light.is_on(self.hass, ent1.entity_id)
assert {} == data
def test_default_profiles_group(self):
@ -387,10 +387,10 @@ class TestLight(unittest.TestCase):
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev, _, _ = platform.DEVICES
common.turn_on(self.hass, dev.entity_id)
ent, _, _ = platform.ENTITIES
common.turn_on(self.hass, ent.entity_id)
self.hass.block_till_done()
_, data = dev.last_call("turn_on")
_, data = ent.last_call("turn_on")
assert {light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 99} == data
def test_default_profiles_light(self):
@ -424,7 +424,9 @@ class TestLight(unittest.TestCase):
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev = next(filter(lambda x: x.entity_id == "light.ceiling_2", platform.DEVICES))
dev = next(
filter(lambda x: x.entity_id == "light.ceiling_2", platform.ENTITIES)
)
common.turn_on(self.hass, dev.entity_id)
self.hass.block_till_done()
_, data = dev.last_call("turn_on")

View file

@ -24,7 +24,7 @@ class TestScene(unittest.TestCase):
self.hass, light.DOMAIN, {light.DOMAIN: {"platform": "test"}}
)
self.light_1, self.light_2 = test_light.DEVICES[0:2]
self.light_1, self.light_2 = test_light.ENTITIES[0:2]
common_light.turn_off(
self.hass, [self.light_1.entity_id, self.light_2.entity_id]

View file

@ -150,7 +150,7 @@ async def test_if_fires_on_state_change(hass, calls):
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
@ -162,7 +162,7 @@ async def test_if_fires_on_state_change(hass, calls):
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_on",
},
"action": {
@ -186,7 +186,7 @@ async def test_if_fires_on_state_change(hass, calls):
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_off",
},
"action": {
@ -209,21 +209,21 @@ async def test_if_fires_on_state_change(hass, calls):
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.states.async_set(dev1.entity_id, STATE_OFF)
hass.states.async_set(ent1.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "turn_off state - {} - on - off - None".format(
dev1.entity_id
ent1.entity_id
)
hass.states.async_set(dev1.entity_id, STATE_ON)
hass.states.async_set(ent1.entity_id, STATE_ON)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "turn_on state - {} - off - on - None".format(
dev1.entity_id
ent1.entity_id
)
@ -234,7 +234,7 @@ async def test_if_state(hass, calls):
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
@ -248,7 +248,7 @@ async def test_if_state(hass, calls):
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "is_on",
}
],
@ -267,7 +267,7 @@ async def test_if_state(hass, calls):
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "is_off",
}
],
@ -283,7 +283,7 @@ async def test_if_state(hass, calls):
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.bus.async_fire("test_event1")
@ -292,7 +292,7 @@ async def test_if_state(hass, calls):
assert len(calls) == 1
assert calls[0].data["some"] == "is_on event - test_event1"
hass.states.async_set(dev1.entity_id, STATE_OFF)
hass.states.async_set(ent1.entity_id, STATE_OFF)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
@ -307,7 +307,7 @@ async def test_action(hass, calls):
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
dev1, dev2, dev3 = platform.DEVICES
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
@ -319,7 +319,7 @@ async def test_action(hass, calls):
"action": {
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_off",
},
},
@ -328,7 +328,7 @@ async def test_action(hass, calls):
"action": {
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "turn_on",
},
},
@ -337,7 +337,7 @@ async def test_action(hass, calls):
"action": {
"domain": DOMAIN,
"device_id": "",
"entity_id": dev1.entity_id,
"entity_id": ent1.entity_id,
"type": "toggle",
},
},
@ -345,29 +345,29 @@ async def test_action(hass, calls):
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
assert hass.states.get(ent1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event1")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
assert hass.states.get(ent1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_OFF
assert hass.states.get(ent1.entity_id).state == STATE_OFF
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert hass.states.get(ent1.entity_id).state == STATE_ON

View file

@ -21,7 +21,7 @@ class TestSwitch(unittest.TestCase):
platform = getattr(self.hass.components, "test.switch")
platform.init()
# Switch 1 is ON, switch 2 is OFF
self.switch_1, self.switch_2, self.switch_3 = platform.DEVICES
self.switch_1, self.switch_2, self.switch_3 = platform.ENTITIES
# pylint: disable=invalid-name
def tearDown(self):

View file

@ -4,23 +4,23 @@ Provide a mock light platform.
Call init before using it in your tests to ensure clean test data.
"""
from homeassistant.const import STATE_ON, STATE_OFF
from tests.common import MockToggleDevice
from tests.common import MockToggleEntity
DEVICES = []
ENTITIES = []
def init(empty=False):
"""Initialize the platform with devices."""
global DEVICES
"""Initialize the platform with entities."""
global ENTITIES
DEVICES = (
ENTITIES = (
[]
if empty
else [
MockToggleDevice("Ceiling", STATE_ON),
MockToggleDevice("Ceiling", STATE_OFF),
MockToggleDevice(None, STATE_OFF),
MockToggleEntity("Ceiling", STATE_ON),
MockToggleEntity("Ceiling", STATE_OFF),
MockToggleEntity(None, STATE_OFF),
]
)
@ -28,5 +28,5 @@ def init(empty=False):
async def async_setup_platform(
hass, config, async_add_entities_callback, discovery_info=None
):
"""Return mock devices."""
async_add_entities_callback(DEVICES)
"""Return mock entities."""
async_add_entities_callback(ENTITIES)

View file

@ -4,23 +4,23 @@ Provide a mock switch platform.
Call init before using it in your tests to ensure clean test data.
"""
from homeassistant.const import STATE_ON, STATE_OFF
from tests.common import MockToggleDevice
from tests.common import MockToggleEntity
DEVICES = []
ENTITIES = []
def init(empty=False):
"""Initialize the platform with devices."""
global DEVICES
"""Initialize the platform with entities."""
global ENTITIES
DEVICES = (
ENTITIES = (
[]
if empty
else [
MockToggleDevice("AC", STATE_ON),
MockToggleDevice("AC", STATE_OFF),
MockToggleDevice(None, STATE_OFF),
MockToggleEntity("AC", STATE_ON),
MockToggleEntity("AC", STATE_OFF),
MockToggleEntity(None, STATE_OFF),
]
)
@ -28,5 +28,5 @@ def init(empty=False):
async def async_setup_platform(
hass, config, async_add_entities_callback, discovery_info=None
):
"""Find and return test switches."""
async_add_entities_callback(DEVICES)
"""Return mock entities."""
async_add_entities_callback(ENTITIES)