Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions homeassistant/helpers/entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,24 @@ async def _async_add_entity(self, entity, update_before_add,
device_info = entity.device_info

if config_entry_id is not None and device_info is not None:
processed_dev_info = {
'config_entry_id': config_entry_id
}
for key in (
'connections',
'identifiers',
'manufacturer',
'model',
'name',
'sw_version',
'via_hub',
):
if key in device_info:

@OttoWinter OttoWinter Oct 6, 2018

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be if device_info.get(key) is not None:?

I think I've seen some device info implementations setting the values of keys to None to indicate absence. Or should that be changed per implementation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be changed in the implementation. If you set something to None, you indicate that it shouldn't be any value.

processed_dev_info[key] = device_info[key]

device = device_registry.async_get_or_create(
config_entry_id=config_entry_id,
connections=device_info.get('connections') or set(),
identifiers=device_info.get('identifiers') or set(),
manufacturer=device_info.get('manufacturer'),
model=device_info.get('model'),
name=device_info.get('name'),
sw_version=device_info.get('sw_version'),
via_hub=device_info.get('via_hub'))
if device:
device_id = device.id
**processed_dev_info)
device_id = device.id
else:
device_id = None

Expand Down
42 changes: 42 additions & 0 deletions tests/helpers/test_entity_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,45 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
assert device.name == 'test-name'
assert device.sw_version == 'test-sw'
assert device.hub_device_id == hub.id


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'
)

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')},
}),
])
return True

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
)

assert await entity_platform.async_setup_entry(config_entry)
await hass.async_block_till_done()

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'