Black
This commit is contained in:
parent
da05dfe708
commit
4de97abc3a
2676 changed files with 163166 additions and 140084 deletions
|
@ -10,24 +10,31 @@ import pytest
|
|||
from homeassistant.exceptions import PlatformNotReady
|
||||
from homeassistant.helpers.entity import async_generate_entity_id
|
||||
from homeassistant.helpers.entity_component import (
|
||||
EntityComponent, DEFAULT_SCAN_INTERVAL)
|
||||
EntityComponent,
|
||||
DEFAULT_SCAN_INTERVAL,
|
||||
)
|
||||
from homeassistant.helpers import entity_platform, entity_registry
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
MockPlatform, async_fire_time_changed, mock_registry,
|
||||
MockEntity, MockEntityPlatform, MockConfigEntry, mock_entity_platform)
|
||||
MockPlatform,
|
||||
async_fire_time_changed,
|
||||
mock_registry,
|
||||
MockEntity,
|
||||
MockEntityPlatform,
|
||||
MockConfigEntry,
|
||||
mock_entity_platform,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DOMAIN = "test_domain"
|
||||
PLATFORM = 'test_platform'
|
||||
PLATFORM = "test_platform"
|
||||
|
||||
|
||||
async def test_polling_only_updates_entities_it_should_poll(hass):
|
||||
"""Test the polling of only updated entities."""
|
||||
component = EntityComponent(
|
||||
_LOGGER, DOMAIN, hass, timedelta(seconds=20))
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
|
||||
|
||||
no_poll_ent = MockEntity(should_poll=False)
|
||||
no_poll_ent.async_update = Mock()
|
||||
|
@ -48,8 +55,7 @@ async def test_polling_only_updates_entities_it_should_poll(hass):
|
|||
|
||||
async def test_polling_updates_entities_with_exception(hass):
|
||||
"""Test the updated entities that not break with an exception."""
|
||||
component = EntityComponent(
|
||||
_LOGGER, DOMAIN, hass, timedelta(seconds=20))
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
|
||||
|
||||
update_ok = []
|
||||
update_err = []
|
||||
|
@ -95,9 +101,7 @@ async def test_update_state_adds_entities(hass):
|
|||
assert len(hass.states.async_entity_ids()) == 1
|
||||
ent2.update = lambda *_: component.add_entities([ent1])
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_entity_ids()) == 2
|
||||
|
@ -131,10 +135,10 @@ async def test_update_state_adds_entities_with_update_before_add_false(hass):
|
|||
assert not ent.update.called
|
||||
|
||||
|
||||
@asynctest.patch('homeassistant.helpers.entity_platform.'
|
||||
'async_track_time_interval')
|
||||
@asynctest.patch("homeassistant.helpers.entity_platform." "async_track_time_interval")
|
||||
async def test_set_scan_interval_via_platform(mock_track, hass):
|
||||
"""Test the setting of the scan interval via platform."""
|
||||
|
||||
def platform_setup(hass, config, add_entities, discovery_info=None):
|
||||
"""Test the platform setup."""
|
||||
add_entities([MockEntity(should_poll=True)])
|
||||
|
@ -142,15 +146,11 @@ async def test_set_scan_interval_via_platform(mock_track, hass):
|
|||
platform = MockPlatform(platform_setup)
|
||||
platform.SCAN_INTERVAL = timedelta(seconds=30)
|
||||
|
||||
mock_entity_platform(hass, 'test_domain.platform', platform)
|
||||
mock_entity_platform(hass, "test_domain.platform", platform)
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
component.setup({
|
||||
DOMAIN: {
|
||||
'platform': 'platform',
|
||||
}
|
||||
})
|
||||
component.setup({DOMAIN: {"platform": "platform"}})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert mock_track.called
|
||||
|
@ -168,8 +168,7 @@ async def test_adding_entities_with_generator_and_thread_callback(hass):
|
|||
def create_entity(number):
|
||||
"""Create entity helper."""
|
||||
entity = MockEntity()
|
||||
entity.entity_id = async_generate_entity_id(DOMAIN + '.{}',
|
||||
'Number', hass=hass)
|
||||
entity.entity_id = async_generate_entity_id(DOMAIN + ".{}", "Number", hass=hass)
|
||||
return entity
|
||||
|
||||
await component.async_add_entities(create_entity(i) for i in range(2))
|
||||
|
@ -179,17 +178,12 @@ async def test_platform_warn_slow_setup(hass):
|
|||
"""Warn we log when platform setup takes a long time."""
|
||||
platform = MockPlatform()
|
||||
|
||||
mock_entity_platform(hass, 'test_domain.platform', platform)
|
||||
mock_entity_platform(hass, "test_domain.platform", platform)
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
with patch.object(hass.loop, 'call_later', MagicMock()) \
|
||||
as mock_call:
|
||||
await component.async_setup({
|
||||
DOMAIN: {
|
||||
'platform': 'platform',
|
||||
}
|
||||
})
|
||||
with patch.object(hass.loop, "call_later", MagicMock()) as mock_call:
|
||||
await component.async_setup({DOMAIN: {"platform": "platform"}})
|
||||
assert mock_call.called
|
||||
|
||||
# mock_calls[0] is the warning message for component setup
|
||||
|
@ -204,7 +198,7 @@ async def test_platform_warn_slow_setup(hass):
|
|||
|
||||
async def test_platform_error_slow_setup(hass, caplog):
|
||||
"""Don't block startup more than SLOW_SETUP_MAX_WAIT."""
|
||||
with patch.object(entity_platform, 'SLOW_SETUP_MAX_WAIT', 0):
|
||||
with patch.object(entity_platform, "SLOW_SETUP_MAX_WAIT", 0):
|
||||
called = []
|
||||
|
||||
async def setup_platform(*args):
|
||||
|
@ -213,15 +207,11 @@ async def test_platform_error_slow_setup(hass, caplog):
|
|||
|
||||
platform = MockPlatform(async_setup_platform=setup_platform)
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
mock_entity_platform(hass, 'test_domain.test_platform', platform)
|
||||
await component.async_setup({
|
||||
DOMAIN: {
|
||||
'platform': 'test_platform',
|
||||
}
|
||||
})
|
||||
mock_entity_platform(hass, "test_domain.test_platform", platform)
|
||||
await component.async_setup({DOMAIN: {"platform": "test_platform"}})
|
||||
assert len(called) == 1
|
||||
assert 'test_domain.test_platform' not in hass.config.components
|
||||
assert 'test_platform is taking longer than 0 seconds' in caplog.text
|
||||
assert "test_domain.test_platform" not in hass.config.components
|
||||
assert "test_platform is taking longer than 0 seconds" in caplog.text
|
||||
|
||||
|
||||
async def test_updated_state_used_for_entity_id(hass):
|
||||
|
@ -233,7 +223,7 @@ async def test_updated_state_used_for_entity_id(hass):
|
|||
|
||||
async def async_update(self):
|
||||
"""Mock update that assigns a name."""
|
||||
self._values['name'] = "Living Room"
|
||||
self._values["name"] = "Living Room"
|
||||
|
||||
await component.async_add_entities([MockEntityNameFetcher()], True)
|
||||
|
||||
|
@ -246,16 +236,12 @@ async def test_parallel_updates_async_platform(hass):
|
|||
"""Test async platform does not have parallel_updates limit by default."""
|
||||
platform = MockPlatform()
|
||||
|
||||
mock_entity_platform(hass, 'test_domain.platform', platform)
|
||||
mock_entity_platform(hass, "test_domain.platform", platform)
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
component._platforms = {}
|
||||
|
||||
await component.async_setup({
|
||||
DOMAIN: {
|
||||
'platform': 'platform',
|
||||
}
|
||||
})
|
||||
await component.async_setup({DOMAIN: {"platform": "platform"}})
|
||||
|
||||
handle = list(component._platforms.values())[-1]
|
||||
assert handle.parallel_updates is None
|
||||
|
@ -276,16 +262,12 @@ async def test_parallel_updates_async_platform_with_constant(hass):
|
|||
platform = MockPlatform()
|
||||
platform.PARALLEL_UPDATES = 2
|
||||
|
||||
mock_entity_platform(hass, 'test_domain.platform', platform)
|
||||
mock_entity_platform(hass, "test_domain.platform", platform)
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
component._platforms = {}
|
||||
|
||||
await component.async_setup({
|
||||
DOMAIN: {
|
||||
'platform': 'platform',
|
||||
}
|
||||
})
|
||||
await component.async_setup({DOMAIN: {"platform": "platform"}})
|
||||
|
||||
handle = list(component._platforms.values())[-1]
|
||||
|
||||
|
@ -307,16 +289,12 @@ async def test_parallel_updates_sync_platform(hass):
|
|||
"""Test sync platform parallel_updates default set to 1."""
|
||||
platform = MockPlatform()
|
||||
|
||||
mock_entity_platform(hass, 'test_domain.platform', platform)
|
||||
mock_entity_platform(hass, "test_domain.platform", platform)
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
component._platforms = {}
|
||||
|
||||
await component.async_setup({
|
||||
DOMAIN: {
|
||||
'platform': 'platform',
|
||||
}
|
||||
})
|
||||
await component.async_setup({DOMAIN: {"platform": "platform"}})
|
||||
|
||||
handle = list(component._platforms.values())[-1]
|
||||
assert handle.parallel_updates is None
|
||||
|
@ -338,16 +316,12 @@ async def test_parallel_updates_sync_platform_with_constant(hass):
|
|||
platform = MockPlatform()
|
||||
platform.PARALLEL_UPDATES = 2
|
||||
|
||||
mock_entity_platform(hass, 'test_domain.platform', platform)
|
||||
mock_entity_platform(hass, "test_domain.platform", platform)
|
||||
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
component._platforms = {}
|
||||
|
||||
await component.async_setup({
|
||||
DOMAIN: {
|
||||
'platform': 'platform',
|
||||
}
|
||||
})
|
||||
await component.async_setup({DOMAIN: {"platform": "platform"}})
|
||||
|
||||
handle = list(component._platforms.values())[-1]
|
||||
assert handle.parallel_updates == 2
|
||||
|
@ -368,8 +342,8 @@ async def test_raise_error_on_update(hass):
|
|||
"""Test the add entity if they raise an error on update."""
|
||||
updates = []
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
entity1 = MockEntity(name='test_1')
|
||||
entity2 = MockEntity(name='test_2')
|
||||
entity1 = MockEntity(name="test_1")
|
||||
entity2 = MockEntity(name="test_2")
|
||||
|
||||
def _raise():
|
||||
"""Raise an exception."""
|
||||
|
@ -387,7 +361,7 @@ async def test_raise_error_on_update(hass):
|
|||
async def test_async_remove_with_platform(hass):
|
||||
"""Remove an entity from a platform."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
entity1 = MockEntity(name='test_1')
|
||||
entity1 = MockEntity(name="test_1")
|
||||
await component.async_add_entities([entity1])
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
await entity1.async_remove()
|
||||
|
@ -398,13 +372,15 @@ async def test_not_adding_duplicate_entities_with_unique_id(hass):
|
|||
"""Test for not adding duplicate entities."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
await component.async_add_entities([
|
||||
MockEntity(name='test1', unique_id='not_very_unique')])
|
||||
await component.async_add_entities(
|
||||
[MockEntity(name="test1", unique_id="not_very_unique")]
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
|
||||
await component.async_add_entities([
|
||||
MockEntity(name='test2', unique_id='not_very_unique')])
|
||||
await component.async_add_entities(
|
||||
[MockEntity(name="test2", unique_id="not_very_unique")]
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
|
||||
|
@ -412,21 +388,22 @@ async def test_not_adding_duplicate_entities_with_unique_id(hass):
|
|||
async def test_using_prescribed_entity_id(hass):
|
||||
"""Test for using predefined entity ID."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
await component.async_add_entities([
|
||||
MockEntity(name='bla', entity_id='hello.world')])
|
||||
assert 'hello.world' in hass.states.async_entity_ids()
|
||||
await component.async_add_entities(
|
||||
[MockEntity(name="bla", entity_id="hello.world")]
|
||||
)
|
||||
assert "hello.world" in hass.states.async_entity_ids()
|
||||
|
||||
|
||||
async def test_using_prescribed_entity_id_with_unique_id(hass):
|
||||
"""Test for ammending predefined entity ID because currently exists."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
|
||||
await component.async_add_entities([
|
||||
MockEntity(entity_id='test_domain.world')])
|
||||
await component.async_add_entities([
|
||||
MockEntity(entity_id='test_domain.world', unique_id='bla')])
|
||||
await component.async_add_entities([MockEntity(entity_id="test_domain.world")])
|
||||
await component.async_add_entities(
|
||||
[MockEntity(entity_id="test_domain.world", unique_id="bla")]
|
||||
)
|
||||
|
||||
assert 'test_domain.world_2' in hass.states.async_entity_ids()
|
||||
assert "test_domain.world_2" in hass.states.async_entity_ids()
|
||||
|
||||
|
||||
async def test_using_prescribed_entity_id_which_is_registered(hass):
|
||||
|
@ -434,14 +411,12 @@ async def test_using_prescribed_entity_id_which_is_registered(hass):
|
|||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
registry = mock_registry(hass)
|
||||
# Register test_domain.world
|
||||
registry.async_get_or_create(
|
||||
DOMAIN, 'test', '1234', suggested_object_id='world')
|
||||
registry.async_get_or_create(DOMAIN, "test", "1234", suggested_object_id="world")
|
||||
|
||||
# This entity_id will be rewritten
|
||||
await component.async_add_entities([
|
||||
MockEntity(entity_id='test_domain.world')])
|
||||
await component.async_add_entities([MockEntity(entity_id="test_domain.world")])
|
||||
|
||||
assert 'test_domain.world_2' in hass.states.async_entity_ids()
|
||||
assert "test_domain.world_2" in hass.states.async_entity_ids()
|
||||
|
||||
|
||||
async def test_name_which_conflict_with_registered(hass):
|
||||
|
@ -450,66 +425,71 @@ async def test_name_which_conflict_with_registered(hass):
|
|||
registry = mock_registry(hass)
|
||||
|
||||
# Register test_domain.world
|
||||
registry.async_get_or_create(
|
||||
DOMAIN, 'test', '1234', suggested_object_id='world')
|
||||
registry.async_get_or_create(DOMAIN, "test", "1234", suggested_object_id="world")
|
||||
|
||||
await component.async_add_entities([
|
||||
MockEntity(name='world')])
|
||||
await component.async_add_entities([MockEntity(name="world")])
|
||||
|
||||
assert 'test_domain.world_2' in hass.states.async_entity_ids()
|
||||
assert "test_domain.world_2" in hass.states.async_entity_ids()
|
||||
|
||||
|
||||
async def test_entity_with_name_and_entity_id_getting_registered(hass):
|
||||
"""Ensure that entity ID is used for registration."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
await component.async_add_entities([
|
||||
MockEntity(unique_id='1234', name='bla',
|
||||
entity_id='test_domain.world')])
|
||||
assert 'test_domain.world' in hass.states.async_entity_ids()
|
||||
await component.async_add_entities(
|
||||
[MockEntity(unique_id="1234", name="bla", entity_id="test_domain.world")]
|
||||
)
|
||||
assert "test_domain.world" in hass.states.async_entity_ids()
|
||||
|
||||
|
||||
async def test_overriding_name_from_registry(hass):
|
||||
"""Test that we can override a name via the Entity Registry."""
|
||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_domain',
|
||||
name='Overridden'
|
||||
)
|
||||
})
|
||||
await component.async_add_entities([
|
||||
MockEntity(unique_id='1234', name='Device Name')])
|
||||
mock_registry(
|
||||
hass,
|
||||
{
|
||||
"test_domain.world": entity_registry.RegistryEntry(
|
||||
entity_id="test_domain.world",
|
||||
unique_id="1234",
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform="test_domain",
|
||||
name="Overridden",
|
||||
)
|
||||
},
|
||||
)
|
||||
await component.async_add_entities(
|
||||
[MockEntity(unique_id="1234", name="Device Name")]
|
||||
)
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
state = hass.states.get("test_domain.world")
|
||||
assert state is not None
|
||||
assert state.name == 'Overridden'
|
||||
assert state.name == "Overridden"
|
||||
|
||||
|
||||
async def test_registry_respect_entity_namespace(hass):
|
||||
"""Test that the registry respects entity namespace."""
|
||||
mock_registry(hass)
|
||||
platform = MockEntityPlatform(hass, entity_namespace='ns')
|
||||
entity = MockEntity(unique_id='1234', name='Device Name')
|
||||
platform = MockEntityPlatform(hass, entity_namespace="ns")
|
||||
entity = MockEntity(unique_id="1234", name="Device Name")
|
||||
await platform.async_add_entities([entity])
|
||||
assert entity.entity_id == 'test_domain.ns_device_name'
|
||||
assert entity.entity_id == "test_domain.ns_device_name"
|
||||
|
||||
|
||||
async def test_registry_respect_entity_disabled(hass):
|
||||
"""Test that the registry respects entity disabled."""
|
||||
mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_platform',
|
||||
disabled_by=entity_registry.DISABLED_USER
|
||||
)
|
||||
})
|
||||
mock_registry(
|
||||
hass,
|
||||
{
|
||||
"test_domain.world": entity_registry.RegistryEntry(
|
||||
entity_id="test_domain.world",
|
||||
unique_id="1234",
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform="test_platform",
|
||||
disabled_by=entity_registry.DISABLED_USER,
|
||||
)
|
||||
},
|
||||
)
|
||||
platform = MockEntityPlatform(hass)
|
||||
entity = MockEntity(unique_id='1234')
|
||||
entity = MockEntity(unique_id="1234")
|
||||
await platform.async_add_entities([entity])
|
||||
assert entity.entity_id is None
|
||||
assert hass.states.async_entity_ids() == []
|
||||
|
@ -517,29 +497,32 @@ async def test_registry_respect_entity_disabled(hass):
|
|||
|
||||
async def test_entity_registry_updates_name(hass):
|
||||
"""Test that updates on the entity registry update platform entities."""
|
||||
registry = mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_platform',
|
||||
name='before update'
|
||||
)
|
||||
})
|
||||
registry = mock_registry(
|
||||
hass,
|
||||
{
|
||||
"test_domain.world": entity_registry.RegistryEntry(
|
||||
entity_id="test_domain.world",
|
||||
unique_id="1234",
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform="test_platform",
|
||||
name="before update",
|
||||
)
|
||||
},
|
||||
)
|
||||
platform = MockEntityPlatform(hass)
|
||||
entity = MockEntity(unique_id='1234')
|
||||
entity = MockEntity(unique_id="1234")
|
||||
await platform.async_add_entities([entity])
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
state = hass.states.get("test_domain.world")
|
||||
assert state is not None
|
||||
assert state.name == 'before update'
|
||||
assert state.name == "before update"
|
||||
|
||||
registry.async_update_entity('test_domain.world', name='after update')
|
||||
registry.async_update_entity("test_domain.world", name="after update")
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
assert state.name == 'after update'
|
||||
state = hass.states.get("test_domain.world")
|
||||
assert state.name == "after update"
|
||||
|
||||
|
||||
async def test_setup_entry(hass):
|
||||
|
@ -548,68 +531,53 @@ async def test_setup_entry(hass):
|
|||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Mock setup entry method."""
|
||||
async_add_entities([
|
||||
MockEntity(name='test1', unique_id='unique')
|
||||
])
|
||||
async_add_entities([MockEntity(name="test1", unique_id="unique")])
|
||||
return True
|
||||
|
||||
platform = MockPlatform(
|
||||
async_setup_entry=async_setup_entry
|
||||
)
|
||||
config_entry = MockConfigEntry(entry_id='super-mock-id')
|
||||
platform = MockPlatform(async_setup_entry=async_setup_entry)
|
||||
config_entry = MockConfigEntry(entry_id="super-mock-id")
|
||||
entity_platform = MockEntityPlatform(
|
||||
hass,
|
||||
platform_name=config_entry.domain,
|
||||
platform=platform
|
||||
hass, platform_name=config_entry.domain, platform=platform
|
||||
)
|
||||
|
||||
assert await entity_platform.async_setup_entry(config_entry)
|
||||
await hass.async_block_till_done()
|
||||
full_name = '{}.{}'.format(entity_platform.domain, config_entry.domain)
|
||||
full_name = "{}.{}".format(entity_platform.domain, config_entry.domain)
|
||||
assert full_name in hass.config.components
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
assert len(registry.entities) == 1
|
||||
assert registry.entities['test_domain.test1'].config_entry_id == \
|
||||
'super-mock-id'
|
||||
assert registry.entities["test_domain.test1"].config_entry_id == "super-mock-id"
|
||||
|
||||
|
||||
async def test_setup_entry_platform_not_ready(hass, caplog):
|
||||
"""Test when an entry is not ready yet."""
|
||||
async_setup_entry = Mock(side_effect=PlatformNotReady)
|
||||
platform = MockPlatform(
|
||||
async_setup_entry=async_setup_entry
|
||||
)
|
||||
platform = MockPlatform(async_setup_entry=async_setup_entry)
|
||||
config_entry = MockConfigEntry()
|
||||
ent_platform = MockEntityPlatform(
|
||||
hass,
|
||||
platform_name=config_entry.domain,
|
||||
platform=platform
|
||||
hass, platform_name=config_entry.domain, platform=platform
|
||||
)
|
||||
|
||||
with patch.object(entity_platform, 'async_call_later') as mock_call_later:
|
||||
with patch.object(entity_platform, "async_call_later") as mock_call_later:
|
||||
assert not await ent_platform.async_setup_entry(config_entry)
|
||||
|
||||
full_name = '{}.{}'.format(ent_platform.domain, config_entry.domain)
|
||||
full_name = "{}.{}".format(ent_platform.domain, config_entry.domain)
|
||||
assert full_name not in hass.config.components
|
||||
assert len(async_setup_entry.mock_calls) == 1
|
||||
assert 'Platform test not ready yet' in caplog.text
|
||||
assert "Platform test not ready yet" in caplog.text
|
||||
assert len(mock_call_later.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_reset_cancels_retry_setup(hass):
|
||||
"""Test that resetting a platform will cancel scheduled a setup retry."""
|
||||
async_setup_entry = Mock(side_effect=PlatformNotReady)
|
||||
platform = MockPlatform(
|
||||
async_setup_entry=async_setup_entry
|
||||
)
|
||||
platform = MockPlatform(async_setup_entry=async_setup_entry)
|
||||
config_entry = MockConfigEntry()
|
||||
ent_platform = MockEntityPlatform(
|
||||
hass,
|
||||
platform_name=config_entry.domain,
|
||||
platform=platform
|
||||
hass, platform_name=config_entry.domain, platform=platform
|
||||
)
|
||||
|
||||
with patch.object(entity_platform, 'async_call_later') as mock_call_later:
|
||||
with patch.object(entity_platform, "async_call_later") as mock_call_later:
|
||||
assert not await ent_platform.async_setup_entry(config_entry)
|
||||
|
||||
assert len(mock_call_later.mock_calls) == 1
|
||||
|
@ -633,112 +601,124 @@ async def test_not_fails_with_adding_empty_entities_(hass):
|
|||
|
||||
async def test_entity_registry_updates_entity_id(hass):
|
||||
"""Test that updates on the entity registry update platform entities."""
|
||||
registry = mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_platform',
|
||||
name='Some name'
|
||||
)
|
||||
})
|
||||
registry = mock_registry(
|
||||
hass,
|
||||
{
|
||||
"test_domain.world": entity_registry.RegistryEntry(
|
||||
entity_id="test_domain.world",
|
||||
unique_id="1234",
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform="test_platform",
|
||||
name="Some name",
|
||||
)
|
||||
},
|
||||
)
|
||||
platform = MockEntityPlatform(hass)
|
||||
entity = MockEntity(unique_id='1234')
|
||||
entity = MockEntity(unique_id="1234")
|
||||
await platform.async_add_entities([entity])
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
state = hass.states.get("test_domain.world")
|
||||
assert state is not None
|
||||
assert state.name == 'Some name'
|
||||
assert state.name == "Some name"
|
||||
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='test_domain.planet')
|
||||
registry.async_update_entity(
|
||||
"test_domain.world", new_entity_id="test_domain.planet"
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('test_domain.world') is None
|
||||
assert hass.states.get('test_domain.planet') is not None
|
||||
assert hass.states.get("test_domain.world") is None
|
||||
assert hass.states.get("test_domain.planet") is not None
|
||||
|
||||
|
||||
async def test_entity_registry_updates_invalid_entity_id(hass):
|
||||
"""Test that we can't update to an invalid entity id."""
|
||||
registry = mock_registry(hass, {
|
||||
'test_domain.world': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.world',
|
||||
unique_id='1234',
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform='test_platform',
|
||||
name='Some name'
|
||||
),
|
||||
'test_domain.existing': entity_registry.RegistryEntry(
|
||||
entity_id='test_domain.existing',
|
||||
unique_id='5678',
|
||||
platform='test_platform',
|
||||
),
|
||||
})
|
||||
registry = mock_registry(
|
||||
hass,
|
||||
{
|
||||
"test_domain.world": entity_registry.RegistryEntry(
|
||||
entity_id="test_domain.world",
|
||||
unique_id="1234",
|
||||
# Using component.async_add_entities is equal to platform "domain"
|
||||
platform="test_platform",
|
||||
name="Some name",
|
||||
),
|
||||
"test_domain.existing": entity_registry.RegistryEntry(
|
||||
entity_id="test_domain.existing",
|
||||
unique_id="5678",
|
||||
platform="test_platform",
|
||||
),
|
||||
},
|
||||
)
|
||||
platform = MockEntityPlatform(hass)
|
||||
entity = MockEntity(unique_id='1234')
|
||||
entity = MockEntity(unique_id="1234")
|
||||
await platform.async_add_entities([entity])
|
||||
|
||||
state = hass.states.get('test_domain.world')
|
||||
state = hass.states.get("test_domain.world")
|
||||
assert state is not None
|
||||
assert state.name == 'Some name'
|
||||
assert state.name == "Some name"
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='test_domain.existing')
|
||||
registry.async_update_entity(
|
||||
"test_domain.world", new_entity_id="test_domain.existing"
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='invalid_entity_id')
|
||||
registry.async_update_entity(
|
||||
"test_domain.world", new_entity_id="invalid_entity_id"
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
registry.async_update_entity('test_domain.world',
|
||||
new_entity_id='diff_domain.world')
|
||||
registry.async_update_entity(
|
||||
"test_domain.world", new_entity_id="diff_domain.world"
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('test_domain.world') is not None
|
||||
assert hass.states.get('invalid_entity_id') is None
|
||||
assert hass.states.get('diff_domain.world') is None
|
||||
assert hass.states.get("test_domain.world") is not None
|
||||
assert hass.states.get("invalid_entity_id") is None
|
||||
assert hass.states.get("diff_domain.world") is None
|
||||
|
||||
|
||||
async def test_device_info_called(hass):
|
||||
"""Test device info is forwarded correctly."""
|
||||
registry = await hass.helpers.device_registry.async_get_registry()
|
||||
via = registry.async_get_or_create(
|
||||
config_entry_id='123',
|
||||
config_entry_id="123",
|
||||
connections=set(),
|
||||
identifiers={('hue', 'via-id')},
|
||||
manufacturer='manufacturer', model='via'
|
||||
identifiers={("hue", "via-id")},
|
||||
manufacturer="manufacturer",
|
||||
model="via",
|
||||
)
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Mock setup entry method."""
|
||||
async_add_entities([
|
||||
# Invalid device info
|
||||
MockEntity(unique_id='abcd', device_info={}),
|
||||
# Valid device info
|
||||
MockEntity(unique_id='qwer', device_info={
|
||||
'identifiers': {('hue', '1234')},
|
||||
'connections': {('mac', 'abcd')},
|
||||
'manufacturer': 'test-manuf',
|
||||
'model': 'test-model',
|
||||
'name': 'test-name',
|
||||
'sw_version': 'test-sw',
|
||||
'via_device': ('hue', 'via-id'),
|
||||
}),
|
||||
])
|
||||
async_add_entities(
|
||||
[
|
||||
# Invalid device info
|
||||
MockEntity(unique_id="abcd", device_info={}),
|
||||
# Valid device info
|
||||
MockEntity(
|
||||
unique_id="qwer",
|
||||
device_info={
|
||||
"identifiers": {("hue", "1234")},
|
||||
"connections": {("mac", "abcd")},
|
||||
"manufacturer": "test-manuf",
|
||||
"model": "test-model",
|
||||
"name": "test-name",
|
||||
"sw_version": "test-sw",
|
||||
"via_device": ("hue", "via-id"),
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
return True
|
||||
|
||||
platform = MockPlatform(
|
||||
async_setup_entry=async_setup_entry
|
||||
)
|
||||
config_entry = MockConfigEntry(entry_id='super-mock-id')
|
||||
platform = MockPlatform(async_setup_entry=async_setup_entry)
|
||||
config_entry = MockConfigEntry(entry_id="super-mock-id")
|
||||
entity_platform = MockEntityPlatform(
|
||||
hass,
|
||||
platform_name=config_entry.domain,
|
||||
platform=platform
|
||||
hass, platform_name=config_entry.domain, platform=platform
|
||||
)
|
||||
|
||||
assert await entity_platform.async_setup_entry(config_entry)
|
||||
|
@ -746,14 +726,14 @@ async def test_device_info_called(hass):
|
|||
|
||||
assert len(hass.states.async_entity_ids()) == 2
|
||||
|
||||
device = registry.async_get_device({('hue', '1234')}, set())
|
||||
device = registry.async_get_device({("hue", "1234")}, set())
|
||||
assert device is not None
|
||||
assert device.identifiers == {('hue', '1234')}
|
||||
assert device.connections == {('mac', 'abcd')}
|
||||
assert device.manufacturer == 'test-manuf'
|
||||
assert device.model == 'test-model'
|
||||
assert device.name == 'test-name'
|
||||
assert device.sw_version == 'test-sw'
|
||||
assert device.identifiers == {("hue", "1234")}
|
||||
assert device.connections == {("mac", "abcd")}
|
||||
assert device.manufacturer == "test-manuf"
|
||||
assert device.model == "test-model"
|
||||
assert device.name == "test-name"
|
||||
assert device.sw_version == "test-sw"
|
||||
assert device.via_device_id == via.id
|
||||
|
||||
|
||||
|
@ -761,39 +741,37 @@ async def test_device_info_not_overrides(hass):
|
|||
"""Test device info is forwarded correctly."""
|
||||
registry = await hass.helpers.device_registry.async_get_registry()
|
||||
device = registry.async_get_or_create(
|
||||
config_entry_id='bla',
|
||||
connections={('mac', 'abcd')},
|
||||
manufacturer='test-manufacturer',
|
||||
model='test-model'
|
||||
config_entry_id="bla",
|
||||
connections={("mac", "abcd")},
|
||||
manufacturer="test-manufacturer",
|
||||
model="test-model",
|
||||
)
|
||||
|
||||
assert device.manufacturer == 'test-manufacturer'
|
||||
assert device.model == 'test-model'
|
||||
assert device.manufacturer == "test-manufacturer"
|
||||
assert device.model == "test-model"
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Mock setup entry method."""
|
||||
async_add_entities([
|
||||
MockEntity(unique_id='qwer', device_info={
|
||||
'connections': {('mac', 'abcd')},
|
||||
}),
|
||||
])
|
||||
async_add_entities(
|
||||
[
|
||||
MockEntity(
|
||||
unique_id="qwer", device_info={"connections": {("mac", "abcd")}}
|
||||
)
|
||||
]
|
||||
)
|
||||
return True
|
||||
|
||||
platform = MockPlatform(
|
||||
async_setup_entry=async_setup_entry
|
||||
)
|
||||
config_entry = MockConfigEntry(entry_id='super-mock-id')
|
||||
platform = MockPlatform(async_setup_entry=async_setup_entry)
|
||||
config_entry = MockConfigEntry(entry_id="super-mock-id")
|
||||
entity_platform = MockEntityPlatform(
|
||||
hass,
|
||||
platform_name=config_entry.domain,
|
||||
platform=platform
|
||||
hass, platform_name=config_entry.domain, platform=platform
|
||||
)
|
||||
|
||||
assert await entity_platform.async_setup_entry(config_entry)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
device2 = registry.async_get_device(set(), {('mac', 'abcd')})
|
||||
device2 = registry.async_get_device(set(), {("mac", "abcd")})
|
||||
assert device2 is not None
|
||||
assert device.id == device2.id
|
||||
assert device2.manufacturer == 'test-manufacturer'
|
||||
assert device2.model == 'test-model'
|
||||
assert device2.manufacturer == "test-manufacturer"
|
||||
assert device2.model == "test-model"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue