Decouple Withings binary sensor test from YAML (#100120)

pull/100130/head
Joost Lekkerkerker 2023-09-11 13:58:47 +02:00 committed by GitHub
parent 5781e5e03e
commit a6e9bf830c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 59 deletions

View File

@ -1,76 +1,51 @@
"""Tests for the Withings component.""" """Tests for the Withings component."""
from unittest.mock import patch
from withings_api.common import NotifyAppli from withings_api.common import NotifyAppli
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.withings.binary_sensor import BINARY_SENSORS
from homeassistant.components.withings.common import WithingsEntityDescription
from homeassistant.components.withings.const import Measurement
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_registry import EntityRegistry
from .common import ComponentFactory, async_get_entity_id, new_profile_config from . import MockWithings, call_webhook
from .conftest import USER_ID, WEBHOOK_ID, ComponentSetup
WITHINGS_MEASUREMENTS_MAP: dict[Measurement, WithingsEntityDescription] = { from tests.typing import ClientSessionGenerator
attr.measurement: attr for attr in BINARY_SENSORS
}
async def test_binary_sensor( async def test_binary_sensor(
hass: HomeAssistant, hass: HomeAssistant,
component_factory: ComponentFactory, setup_integration: ComponentSetup,
current_request_with_host: None, hass_client_no_auth: ClientSessionGenerator,
) -> None: ) -> None:
"""Test binary sensor.""" """Test binary sensor."""
in_bed_attribute = WITHINGS_MEASUREMENTS_MAP[Measurement.IN_BED] await setup_integration()
person0 = new_profile_config("person0", 0) mock = MockWithings()
person1 = new_profile_config("person1", 1) with patch(
"homeassistant.components.withings.common.ConfigEntryWithingsApi",
return_value=mock,
):
client = await hass_client_no_auth()
entity_registry: EntityRegistry = er.async_get(hass) entity_id = "binary_sensor.henk_in_bed"
await component_factory.configure_component(profile_configs=(person0, person1)) assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
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 resp = await call_webhook(
await component_factory.setup_profile(person0.user_id) hass,
await component_factory.setup_profile(person1.user_id) WEBHOOK_ID,
{"userid": USER_ID, "appli": NotifyAppli.BED_IN},
client,
)
assert resp.message_code == 0
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_ON
entity_id0 = await async_get_entity_id( resp = await call_webhook(
hass, in_bed_attribute, person0.user_id, BINARY_SENSOR_DOMAIN hass,
) WEBHOOK_ID,
entity_id1 = await async_get_entity_id( {"userid": USER_ID, "appli": NotifyAppli.BED_OUT},
hass, in_bed_attribute, person1.user_id, BINARY_SENSOR_DOMAIN client,
) )
assert entity_id0 assert resp.message_code == 0
assert entity_id1 await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_OFF
assert entity_registry.async_is_registered(entity_id0)
assert hass.states.get(entity_id0).state == STATE_UNAVAILABLE
resp = await component_factory.call_webhook(person0.user_id, NotifyAppli.BED_IN)
assert resp.message_code == 0
await hass.async_block_till_done()
assert hass.states.get(entity_id0).state == STATE_ON
resp = await component_factory.call_webhook(person0.user_id, NotifyAppli.BED_OUT)
assert resp.message_code == 0
await hass.async_block_till_done()
assert hass.states.get(entity_id0).state == STATE_OFF
# person 1
assert hass.states.get(entity_id1).state == STATE_UNAVAILABLE
resp = await component_factory.call_webhook(person1.user_id, NotifyAppli.BED_IN)
assert resp.message_code == 0
await hass.async_block_till_done()
assert hass.states.get(entity_id1).state == STATE_ON
# Unload
await component_factory.unload(person0)
await component_factory.unload(person1)