Skip to content

Commit

Permalink
Swap the names of the device battery sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
allenporter committed Oct 22, 2023
1 parent fe6bf2d commit c9516f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 53 deletions.
11 changes: 5 additions & 6 deletions homeassistant/components/fitbit/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,7 @@ class FitbitSensorEntityDescription(SensorEntityDescription):
has_entity_name=True,
)
FITBIT_RESOURCE_BATTERY_LEVEL = FitbitSensorEntityDescription(
key="devices/battery_level",
translation_key="battery_level",
key="devices/battery_device_class",
scope=FitbitScope.DEVICE,
entity_category=EntityCategory.DIAGNOSTIC,
has_entity_name=True,
Expand Down Expand Up @@ -669,7 +668,7 @@ def is_allowed_resource(description: FitbitSensorEntityDescription) -> bool:

if data.device_coordinator and is_allowed_resource(FITBIT_RESOURCE_BATTERY):
battery_entities: list[SensorEntity] = [
FitbitBatterySensor(
FitbitBatteryStringSensor(
data.device_coordinator,
user_profile.encoded_id,
FITBIT_RESOURCE_BATTERY,
Expand All @@ -679,7 +678,7 @@ def is_allowed_resource(description: FitbitSensorEntityDescription) -> bool:
for device in data.device_coordinator.data.values()
]
battery_entities.extend(
FitbitBatteryLevelSensor(
FitbitBatterySensor(
data.device_coordinator,
user_profile.encoded_id,
FITBIT_RESOURCE_BATTERY_LEVEL,
Expand Down Expand Up @@ -734,7 +733,7 @@ async def async_update(self) -> None:
self._attr_native_value = self.entity_description.value_fn(result)


class FitbitBatterySensor(CoordinatorEntity, SensorEntity):
class FitbitBatteryStringSensor(CoordinatorEntity, SensorEntity):
"""Implementation of a Fitbit battery sensor."""

entity_description: FitbitSensorEntityDescription
Expand Down Expand Up @@ -790,7 +789,7 @@ def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()


class FitbitBatteryLevelSensor(CoordinatorEntity, SensorEntity):
class FitbitBatterySensor(CoordinatorEntity, SensorEntity):
"""Implementation of a Fitbit battery sensor."""

entity_description: FitbitSensorEntityDescription
Expand Down
3 changes: 0 additions & 3 deletions homeassistant/components/fitbit/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
"entity": {
"sensor": {
"battery": {
"name": "Battery"
},
"battery_level": {
"name": "Battery level"
}
}
Expand Down
71 changes: 27 additions & 44 deletions tests/components/fitbit/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ async def test_sensors(
("devices_response", "monitored_resources"),
[([DEVICE_RESPONSE_CHARGE_2, DEVICE_RESPONSE_ARIA_AIR], ["devices/battery"])],
)
async def test_device_battery(
async def test_device_battery_level(
hass: HomeAssistant,
fitbit_config_setup: None,
sensor_platform_setup: Callable[[], Awaitable[bool]],
Expand All @@ -242,72 +242,55 @@ async def test_device_battery(

state = hass.states.get("sensor.charge_2_battery")
assert state
assert state.state == "Medium"
assert state.state == "60"
assert state.attributes == {
"attribution": "Data provided by Fitbit.com",
"friendly_name": "Charge 2 Battery",
"device_class": "battery",
"unit_of_measurement": "%",
}

state = hass.states.get("sensor.charge_2_battery_level")
assert state
assert state.state == "Medium"
assert state.attributes == {
"attribution": "Data provided by Fitbit.com",
"friendly_name": "Charge 2 Battery level",
"icon": "mdi:battery-50",
"model": "Charge 2",
"type": "tracker",
}

entry = entity_registry.async_get("sensor.charge_2_battery")
entry = entity_registry.async_get("sensor.charge_2_battery_level")
assert entry
assert entry.unique_id == f"{PROFILE_USER_ID}_devices/battery_816713257"

state = hass.states.get("sensor.aria_air_battery")
assert state
assert state.state == "High"
assert state.state == "95"
assert state.attributes == {
"attribution": "Data provided by Fitbit.com",
"friendly_name": "Aria Air Battery",
"icon": "mdi:battery",
"model": "Aria Air",
"type": "scale",
}

entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.aria_air_battery")
assert entry
assert entry.unique_id == f"{PROFILE_USER_ID}_devices/battery_016713257"


@pytest.mark.parametrize(
("devices_response", "monitored_resources"),
[([DEVICE_RESPONSE_CHARGE_2, DEVICE_RESPONSE_ARIA_AIR], ["devices/battery"])],
)
async def test_device_battery_level(
hass: HomeAssistant,
fitbit_config_setup: None,
sensor_platform_setup: Callable[[], Awaitable[bool]],
entity_registry: er.EntityRegistry,
) -> None:
"""Test battery level sensor for devices."""

assert await sensor_platform_setup()
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1

state = hass.states.get("sensor.charge_2_battery_level")
assert state
assert state.state == "60"
assert state.attributes == {
"attribution": "Data provided by Fitbit.com",
"friendly_name": "Charge 2 Battery level",
"device_class": "battery",
"unit_of_measurement": "%",
}

state = hass.states.get("sensor.aria_air_battery_level")
assert state
assert state.state == "95"
assert state.state == "High"
assert state.attributes == {
"attribution": "Data provided by Fitbit.com",
"friendly_name": "Aria Air Battery level",
"device_class": "battery",
"unit_of_measurement": "%",
"icon": "mdi:battery",
"model": "Aria Air",
"type": "scale",
}

entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.aria_air_battery_level")
assert entry
assert entry.unique_id == f"{PROFILE_USER_ID}_devices/battery_016713257"


@pytest.mark.parametrize(
(
Expand Down Expand Up @@ -587,10 +570,10 @@ async def test_settings_scope_config_entry(
assert await integration_setup()

states = hass.states.async_all()
assert [s.entity_id for s in states] == [
assert {s.entity_id for s in states} == {
"sensor.charge_2_battery",
"sensor.charge_2_battery_level",
]
}


@pytest.mark.parametrize(
Expand Down Expand Up @@ -694,7 +677,7 @@ async def test_device_battery_level_update_failed(

state = hass.states.get("sensor.charge_2_battery")
assert state
assert state.state == "Medium"
assert state.state == "60"

# Request an update for the entity which will fail
await async_update_entity(hass, "sensor.charge_2_battery")
Expand Down Expand Up @@ -744,7 +727,7 @@ async def test_device_battery_level_reauth_required(

state = hass.states.get("sensor.charge_2_battery")
assert state
assert state.state == "Medium"
assert state.state == "60"

# Request an update for the entity which will fail
await async_update_entity(hass, "sensor.charge_2_battery")
Expand Down

0 comments on commit c9516f6

Please sign in to comment.