Set a suggested_area on nest devices based on the Google Home room name (#62871)

This commit is contained in:
Allen Porter 2021-12-27 11:39:57 -08:00 committed by GitHub
parent 7fc5605639
commit 17fbfe2eed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 6 deletions

View file

@ -35,6 +35,7 @@ class NestDeviceInfo:
manufacturer=self.device_brand,
model=self.device_model,
name=self.device_name,
suggested_area=self.suggested_area,
)
@property
@ -44,12 +45,9 @@ class NestDeviceInfo:
trait: InfoTrait = self._device.traits[InfoTrait.NAME]
if trait.custom_name:
return str(trait.custom_name)
# Build a name from the room/structure. Note: This room/structure name
# is not associated with a home assistant Area.
if parent_relations := self._device.parent_relations:
items = sorted(parent_relations.items())
names = [name for id, name in items]
return " ".join(names)
# Build a name from the room/structure if not set explicitly
if area := self.suggested_area:
return area
return self.device_model
@property
@ -59,3 +57,12 @@ class NestDeviceInfo:
# devices, instead relying on traits, but we can infer a generic model
# name based on the type
return DEVICE_TYPE_MAP.get(self._device.type)
@property
def suggested_area(self) -> str | None:
"""Return device suggested area based on the Google Home room."""
if parent_relations := self._device.parent_relations:
items = sorted(parent_relations.items())
names = [name for id, name in items]
return " ".join(names)
return None

View file

@ -8,6 +8,7 @@ from homeassistant.const import (
ATTR_MANUFACTURER,
ATTR_MODEL,
ATTR_NAME,
ATTR_SUGGESTED_AREA,
)
@ -35,6 +36,7 @@ def test_device_custom_name():
ATTR_NAME: "My Doorbell",
ATTR_MANUFACTURER: "Google Nest",
ATTR_MODEL: "Doorbell",
ATTR_SUGGESTED_AREA: None,
}
@ -60,6 +62,7 @@ def test_device_name_room():
ATTR_NAME: "Some Room",
ATTR_MANUFACTURER: "Google Nest",
ATTR_MODEL: "Doorbell",
ATTR_SUGGESTED_AREA: "Some Room",
}
@ -79,6 +82,7 @@ def test_device_no_name():
ATTR_NAME: "Doorbell",
ATTR_MANUFACTURER: "Google Nest",
ATTR_MODEL: "Doorbell",
ATTR_SUGGESTED_AREA: None,
}
@ -106,4 +110,36 @@ def test_device_invalid_type():
ATTR_NAME: "My Doorbell",
ATTR_MANUFACTURER: "Google Nest",
ATTR_MODEL: None,
ATTR_SUGGESTED_AREA: None,
}
def test_suggested_area():
"""Test the suggested area with different device name and room name."""
device = Device.MakeDevice(
{
"name": "some-device-id",
"type": "sdm.devices.types.DOORBELL",
"traits": {
"sdm.devices.traits.Info": {
"customName": "My Doorbell",
},
},
"parentRelations": [
{"parent": "some-structure-id", "displayName": "Some Room"}
],
},
auth=None,
)
device_info = NestDeviceInfo(device)
assert device_info.device_name == "My Doorbell"
assert device_info.device_model == "Doorbell"
assert device_info.device_brand == "Google Nest"
assert device_info.device_info == {
ATTR_IDENTIFIERS: {("nest", "some-device-id")},
ATTR_NAME: "My Doorbell",
ATTR_MANUFACTURER: "Google Nest",
ATTR_MODEL: "Doorbell",
ATTR_SUGGESTED_AREA: "Some Room",
}