Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device name to sensor name for mobile_app #31756

Merged
merged 2 commits into from Feb 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion homeassistant/components/mobile_app/entity.py
Expand Up @@ -7,6 +7,7 @@
from homeassistant.helpers.entity import Entity

from .const import (
ATTR_DEVICE_NAME,
ATTR_SENSOR_ATTRIBUTES,
ATTR_SENSOR_DEVICE_CLASS,
ATTR_SENSOR_ICON,
Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(self, config: dict, device: DeviceEntry, entry: ConfigEntry):
)
self._entity_type = config[ATTR_SENSOR_TYPE]
self.unsub_dispatcher = None
self._name = f"{entry.data[ATTR_DEVICE_NAME]} {config[ATTR_SENSOR_NAME]}"

async def async_added_to_hass(self):
"""Register callbacks."""
Expand All @@ -58,7 +60,7 @@ def should_poll(self) -> bool:
@property
def name(self):
"""Return the name of the mobile app sensor."""
return self._config[ATTR_SENSOR_NAME]
return self._name
Copy link
Member

Choose a reason for hiding this comment

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

For the name, I think the format Sensor Name (Device Name) works better too. That's a suggestion, not a directive, so i'm open to leaving it as is.

Copy link
Member

Choose a reason for hiding this comment

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

I think that we should Device Name Sensor Name, since we've been adding logic to strip off start of names if they match the container name. (ie Living Room area card will rename Living Room Lights -> Lights)

Copy link
Member

Choose a reason for hiding this comment

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

Also we have a unique ID, let the user do their thing :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup I agree we not only have unique ID but we also have the entity registered to the device so its not too difficult to find from device page. I think users are just confused by the generic name it gets when the sensor gets registered.

So the format I chose is good? Not sure if we have a official format to follow :)

Copy link
Member

Choose a reason for hiding this comment

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

What Paulus says goes, i'm fine with it.


@property
def device_class(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/components/mobile_app/test_entity.py
Expand Up @@ -35,15 +35,15 @@ async def test_sensor(hass, create_registrations, webhook_client):
assert json == {"success": True}
await hass.async_block_till_done()

entity = hass.states.get("sensor.battery_state")
entity = hass.states.get("sensor.test_1_battery_state")
assert entity is not None

assert entity.attributes["device_class"] == "battery"
assert entity.attributes["icon"] == "mdi:battery"
assert entity.attributes["unit_of_measurement"] == "%"
assert entity.attributes["foo"] == "bar"
assert entity.domain == "sensor"
assert entity.name == "Battery State"
assert entity.name == "Test 1 Battery State"
assert entity.state == "100"

update_resp = await webhook_client.post(
Expand All @@ -63,7 +63,7 @@ async def test_sensor(hass, create_registrations, webhook_client):

assert update_resp.status == 200

updated_entity = hass.states.get("sensor.battery_state")
updated_entity = hass.states.get("sensor.test_1_battery_state")
assert updated_entity.state == "123"

dev_reg = await device_registry.async_get_registry(hass)
Expand Down