Move unused function in withings (#84089)
* Remove unused function in withings * Move to tests * Remove reference to attribute.platform * Move WITHINGS_MEASUREMENTS_MAP to tests * measute_type > measure_type * One morepull/84095/head
parent
7f3f271a59
commit
14ee3f1ddc
|
@ -42,7 +42,7 @@ from homeassistant.const import (
|
|||
)
|
||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_entry_oauth2_flow, entity_registry as er
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
from homeassistant.helpers.config_entry_oauth2_flow import (
|
||||
AbstractOAuth2Implementation,
|
||||
OAuth2Session,
|
||||
|
@ -85,7 +85,7 @@ class WithingsAttribute:
|
|||
"""Immutable class for describing withings sensor data."""
|
||||
|
||||
measurement: Measurement
|
||||
measute_type: Enum
|
||||
measure_type: NotifyAppli | GetSleepSummaryField | MeasureType
|
||||
friendly_name: str
|
||||
unit_of_measurement: str
|
||||
icon: str | None
|
||||
|
@ -463,13 +463,10 @@ WITHINGS_ATTRIBUTES = [
|
|||
),
|
||||
]
|
||||
|
||||
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsAttribute] = {
|
||||
attr.measurement: attr for attr in WITHINGS_ATTRIBUTES
|
||||
}
|
||||
|
||||
WITHINGS_MEASURE_TYPE_MAP: dict[
|
||||
NotifyAppli | GetSleepSummaryField | MeasureType, WithingsAttribute
|
||||
] = {attr.measute_type: attr for attr in WITHINGS_ATTRIBUTES}
|
||||
] = {attr.measure_type: attr for attr in WITHINGS_ATTRIBUTES}
|
||||
|
||||
|
||||
class ConfigEntryWithingsApi(AbstractWithingsApi):
|
||||
|
@ -889,24 +886,6 @@ def get_attribute_unique_id(attribute: WithingsAttribute, user_id: int) -> str:
|
|||
return f"withings_{user_id}_{attribute.measurement.value}"
|
||||
|
||||
|
||||
async def async_get_entity_id(
|
||||
hass: HomeAssistant, attribute: WithingsAttribute, user_id: int
|
||||
) -> str | None:
|
||||
"""Get an entity id for a user's attribute."""
|
||||
entity_registry = er.async_get(hass)
|
||||
unique_id = get_attribute_unique_id(attribute, user_id)
|
||||
|
||||
entity_id = entity_registry.async_get_entity_id(
|
||||
attribute.platform, const.DOMAIN, unique_id
|
||||
)
|
||||
|
||||
if entity_id is None:
|
||||
_LOGGER.error("Cannot find entity id for unique_id: %s", unique_id)
|
||||
return None
|
||||
|
||||
return entity_id
|
||||
|
||||
|
||||
class BaseWithingsSensor(Entity):
|
||||
"""Base class for withings sensors."""
|
||||
|
||||
|
|
|
@ -24,7 +24,9 @@ from homeassistant.components.withings import async_unload_entry
|
|||
from homeassistant.components.withings.common import (
|
||||
ConfigEntryWithingsApi,
|
||||
DataManager,
|
||||
WithingsAttribute,
|
||||
get_all_data_managers,
|
||||
get_attribute_unique_id,
|
||||
)
|
||||
import homeassistant.components.withings.const as const
|
||||
from homeassistant.config import async_process_ha_core_config
|
||||
|
@ -37,7 +39,7 @@ from homeassistant.const import (
|
|||
CONF_UNIT_SYSTEM_METRIC,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
from homeassistant.helpers import config_entry_oauth2_flow, entity_registry as er
|
||||
from homeassistant.helpers.config_entry_oauth2_flow import AUTH_CALLBACK_PATH
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
@ -320,3 +322,13 @@ def get_data_manager_by_user_id(
|
|||
),
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
async def async_get_entity_id(
|
||||
hass: HomeAssistant, attribute: WithingsAttribute, user_id: int, platform: str
|
||||
) -> str | None:
|
||||
"""Get an entity id for a user's attribute."""
|
||||
entity_registry = er.async_get(hass)
|
||||
unique_id = get_attribute_unique_id(attribute, user_id)
|
||||
|
||||
return entity_registry.async_get_entity_id(platform, const.DOMAIN, unique_id)
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
"""Tests for the Withings component."""
|
||||
from withings_api.common import NotifyAppli
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||
from homeassistant.components.withings.common import (
|
||||
WITHINGS_MEASUREMENTS_MAP,
|
||||
async_get_entity_id,
|
||||
WITHINGS_ATTRIBUTES,
|
||||
WithingsAttribute,
|
||||
)
|
||||
from homeassistant.components.withings.const import Measurement
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
||||
|
@ -11,7 +12,13 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
|
||||
from .common import ComponentFactory, new_profile_config
|
||||
from .common import ComponentFactory, async_get_entity_id, new_profile_config
|
||||
|
||||
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsAttribute] = {
|
||||
attr.measurement: attr
|
||||
for attr in WITHINGS_ATTRIBUTES
|
||||
if attr.platform == BINARY_SENSOR_DOMAIN
|
||||
}
|
||||
|
||||
|
||||
async def test_binary_sensor(
|
||||
|
@ -25,15 +32,23 @@ async def test_binary_sensor(
|
|||
entity_registry: EntityRegistry = er.async_get(hass)
|
||||
|
||||
await component_factory.configure_component(profile_configs=(person0, person1))
|
||||
assert not await async_get_entity_id(hass, in_bed_attribute, person0.user_id)
|
||||
assert not await async_get_entity_id(hass, in_bed_attribute, person1.user_id)
|
||||
assert not await async_get_entity_id(
|
||||
hass, in_bed_attribute, person0.user_id, BINARY_SENSOR_DOMAIN
|
||||
)
|
||||
assert not await async_get_entity_id(
|
||||
hass, in_bed_attribute, person1.user_id, BINARY_SENSOR_DOMAIN
|
||||
)
|
||||
|
||||
# person 0
|
||||
await component_factory.setup_profile(person0.user_id)
|
||||
await component_factory.setup_profile(person1.user_id)
|
||||
|
||||
entity_id0 = await async_get_entity_id(hass, in_bed_attribute, person0.user_id)
|
||||
entity_id1 = await async_get_entity_id(hass, in_bed_attribute, person1.user_id)
|
||||
entity_id0 = await async_get_entity_id(
|
||||
hass, in_bed_attribute, person0.user_id, BINARY_SENSOR_DOMAIN
|
||||
)
|
||||
entity_id1 = await async_get_entity_id(
|
||||
hass, in_bed_attribute, person1.user_id, BINARY_SENSOR_DOMAIN
|
||||
)
|
||||
assert entity_id0
|
||||
assert entity_id1
|
||||
|
||||
|
|
|
@ -17,10 +17,10 @@ from withings_api.common import (
|
|||
SleepModel,
|
||||
)
|
||||
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.components.withings.common import (
|
||||
WITHINGS_MEASUREMENTS_MAP,
|
||||
WITHINGS_ATTRIBUTES,
|
||||
WithingsAttribute,
|
||||
async_get_entity_id,
|
||||
get_platform_attributes,
|
||||
)
|
||||
from homeassistant.components.withings.const import Measurement
|
||||
|
@ -30,7 +30,13 @@ from homeassistant.helpers import entity_registry as er
|
|||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .common import ComponentFactory, new_profile_config
|
||||
from .common import ComponentFactory, async_get_entity_id, new_profile_config
|
||||
|
||||
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsAttribute] = {
|
||||
attr.measurement: attr
|
||||
for attr in WITHINGS_ATTRIBUTES
|
||||
if attr.platform == SENSOR_DOMAIN
|
||||
}
|
||||
|
||||
PERSON0 = new_profile_config(
|
||||
"person0",
|
||||
|
@ -311,14 +317,18 @@ async def test_sensor_default_enabled_entities(
|
|||
|
||||
# Assert entities should not exist yet.
|
||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
||||
assert not await async_get_entity_id(hass, attribute, PERSON0.user_id)
|
||||
assert not await async_get_entity_id(
|
||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||
)
|
||||
|
||||
# person 0
|
||||
await component_factory.setup_profile(PERSON0.user_id)
|
||||
|
||||
# Assert entities should exist.
|
||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
||||
entity_id = await async_get_entity_id(hass, attribute, PERSON0.user_id)
|
||||
entity_id = await async_get_entity_id(
|
||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||
)
|
||||
assert entity_id
|
||||
assert entity_registry.async_is_registered(entity_id)
|
||||
|
||||
|
@ -330,7 +340,9 @@ async def test_sensor_default_enabled_entities(
|
|||
|
||||
for person, measurement, expected in EXPECTED_DATA:
|
||||
attribute = WITHINGS_MEASUREMENTS_MAP[measurement]
|
||||
entity_id = await async_get_entity_id(hass, attribute, person.user_id)
|
||||
entity_id = await async_get_entity_id(
|
||||
hass, attribute, person.user_id, SENSOR_DOMAIN
|
||||
)
|
||||
state_obj = hass.states.get(entity_id)
|
||||
|
||||
if attribute.enabled_by_default:
|
||||
|
@ -357,14 +369,18 @@ async def test_all_entities(
|
|||
|
||||
# Assert entities should not exist yet.
|
||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
||||
assert not await async_get_entity_id(hass, attribute, PERSON0.user_id)
|
||||
assert not await async_get_entity_id(
|
||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||
)
|
||||
|
||||
# person 0
|
||||
await component_factory.setup_profile(PERSON0.user_id)
|
||||
|
||||
# Assert entities should exist.
|
||||
for attribute in get_platform_attributes(Platform.SENSOR):
|
||||
entity_id = await async_get_entity_id(hass, attribute, PERSON0.user_id)
|
||||
entity_id = await async_get_entity_id(
|
||||
hass, attribute, PERSON0.user_id, SENSOR_DOMAIN
|
||||
)
|
||||
assert entity_id
|
||||
assert entity_registry.async_is_registered(entity_id)
|
||||
|
||||
|
@ -376,7 +392,9 @@ async def test_all_entities(
|
|||
|
||||
for person, measurement, expected in EXPECTED_DATA:
|
||||
attribute = WITHINGS_MEASUREMENTS_MAP[measurement]
|
||||
entity_id = await async_get_entity_id(hass, attribute, person.user_id)
|
||||
entity_id = await async_get_entity_id(
|
||||
hass, attribute, person.user_id, SENSOR_DOMAIN
|
||||
)
|
||||
state_obj = hass.states.get(entity_id)
|
||||
|
||||
async_assert_state_equals(entity_id, state_obj, expected, attribute)
|
||||
|
|
Loading…
Reference in New Issue