init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1 @@
"""Tests for specific devices."""

View File

@@ -0,0 +1,60 @@
"""Test against characteristics captured from a eufycam."""
from homeassistant.core import HomeAssistant
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
EntityTestInfo,
assert_devices_and_entities_created,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_eufycam_setup(hass: HomeAssistant) -> None:
"""Test that a eufycam can be correctly setup in HA."""
accessories = await setup_accessories_from_file(hass, "anker_eufycam.json")
await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="eufy HomeBase2-0AAA",
model="T8010",
manufacturer="Anker",
sw_version="2.1.6",
hw_version="2.0.0",
serial_number="A0000A000000000A",
devices=[
DeviceTestInfo(
name="eufyCam2-0000",
model="T8113",
manufacturer="Anker",
sw_version="1.6.7",
hw_version="1.0.0",
serial_number="A0000A000000000D",
unique_id="00:00:00:00:00:00:aid:4",
devices=[],
entities=[
EntityTestInfo(
entity_id="camera.eufycam2_0000",
friendly_name="eufyCam2-0000",
unique_id="00:00:00:00:00:00_4",
state="idle",
),
],
),
],
entities=[],
),
)
# There are multiple rtsp services, we only want to create 1
# camera entity per accessory, not 1 camera per service.
cameras_count = 0
for state in hass.states.async_all():
if state.entity_id.startswith("camera."):
cameras_count += 1
assert cameras_count == 3

View File

@@ -0,0 +1,57 @@
"""Regression tests for Aqara AR004.
This device has a non-standard programmable stateless switch service that has a
service-label-index despite not being linked to a service-label.
https://github.com/home-assistant/core/pull/39090
"""
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import PERCENTAGE, EntityCategory
from homeassistant.core import HomeAssistant
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
DeviceTriggerInfo,
EntityTestInfo,
assert_devices_and_entities_created,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_aqara_switch_setup(hass: HomeAssistant) -> None:
"""Test that a Aqara Switch can be correctly setup in HA."""
accessories = await setup_accessories_from_file(hass, "aqara_switch.json")
await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="Programmable Switch",
model="AR004",
manufacturer="Aqara",
sw_version="9",
hw_version="1.0",
serial_number="111a1111a1a111",
devices=[],
entities=[
EntityTestInfo(
entity_id="sensor.programmable_switch_battery_sensor",
friendly_name="Programmable Switch Battery Sensor",
unique_id="00:00:00:00:00:00_1_5",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
entity_category=EntityCategory.DIAGNOSTIC,
unit_of_measurement=PERCENTAGE,
state="100",
),
],
stateless_triggers=[
DeviceTriggerInfo(type="button1", subtype="single_press"),
DeviceTriggerInfo(type="button1", subtype="double_press"),
DeviceTriggerInfo(type="button1", subtype="long_press"),
],
),
)

View File

@@ -0,0 +1,175 @@
"""Make sure that ConnectSense Smart Outlet2 / In-Wall Outlet is enumerated properly."""
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import UnitOfElectricCurrent, UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
EntityTestInfo,
assert_devices_and_entities_created,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_connectsense_setup(hass: HomeAssistant) -> None:
"""Test that the accessory can be correctly setup in HA."""
accessories = await setup_accessories_from_file(hass, "connectsense.json")
config_entry, pairing = await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="InWall Outlet-0394DE",
model="CS-IWO",
manufacturer="ConnectSense",
sw_version="1.0.0",
hw_version="",
serial_number="1020301376",
devices=[],
entities=[
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_current",
friendly_name="InWall Outlet-0394DE Current",
unique_id="00:00:00:00:00:00_1_13_18",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state="0.03",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_power",
friendly_name="InWall Outlet-0394DE Power",
unique_id="00:00:00:00:00:00_1_13_19",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfPower.WATT,
state="0.8",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_energy_kwh",
friendly_name="InWall Outlet-0394DE Energy kWh",
unique_id="00:00:00:00:00:00_1_13_20",
capabilities={"state_class": SensorStateClass.TOTAL_INCREASING},
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state="379.69299",
),
EntityTestInfo(
entity_id="switch.inwall_outlet_0394de_outlet_a",
friendly_name="InWall Outlet-0394DE Outlet A",
unique_id="00:00:00:00:00:00_1_13",
state="on",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_current_2",
friendly_name="InWall Outlet-0394DE Current",
unique_id="00:00:00:00:00:00_1_25_30",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state="0.05",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_power_2",
friendly_name="InWall Outlet-0394DE Power",
unique_id="00:00:00:00:00:00_1_25_31",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfPower.WATT,
state="0.8",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_energy_kwh_2",
friendly_name="InWall Outlet-0394DE Energy kWh",
unique_id="00:00:00:00:00:00_1_25_32",
capabilities={"state_class": SensorStateClass.TOTAL_INCREASING},
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state="175.85001",
),
EntityTestInfo(
entity_id="switch.inwall_outlet_0394de_outlet_b",
friendly_name="InWall Outlet-0394DE Outlet B",
unique_id="00:00:00:00:00:00_1_25",
state="on",
),
],
),
)
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="InWall Outlet-0394DE",
model="CS-IWO",
manufacturer="ConnectSense",
sw_version="1.0.0",
hw_version="",
serial_number="1020301376",
devices=[],
entities=[
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_current",
friendly_name="InWall Outlet-0394DE Current",
unique_id="00:00:00:00:00:00_1_13_18",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state="0.03",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_power",
friendly_name="InWall Outlet-0394DE Power",
unique_id="00:00:00:00:00:00_1_13_19",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfPower.WATT,
state="0.8",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_energy_kwh",
friendly_name="InWall Outlet-0394DE Energy kWh",
unique_id="00:00:00:00:00:00_1_13_20",
capabilities={"state_class": SensorStateClass.TOTAL_INCREASING},
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state="379.69299",
),
EntityTestInfo(
entity_id="switch.inwall_outlet_0394de_outlet_a",
friendly_name="InWall Outlet-0394DE Outlet A",
unique_id="00:00:00:00:00:00_1_13",
state="on",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_current_2",
friendly_name="InWall Outlet-0394DE Current",
unique_id="00:00:00:00:00:00_1_25_30",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state="0.05",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_power_2",
friendly_name="InWall Outlet-0394DE Power",
unique_id="00:00:00:00:00:00_1_25_31",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfPower.WATT,
state="0.8",
),
EntityTestInfo(
entity_id="sensor.inwall_outlet_0394de_energy_kwh_2",
friendly_name="InWall Outlet-0394DE Energy kWh",
unique_id="00:00:00:00:00:00_1_25_32",
capabilities={"state_class": SensorStateClass.TOTAL_INCREASING},
unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state="175.85001",
),
EntityTestInfo(
entity_id="switch.inwall_outlet_0394de_outlet_b",
friendly_name="InWall Outlet-0394DE Outlet B",
unique_id="00:00:00:00:00:00_1_25",
state="on",
),
],
),
)

View File

@@ -0,0 +1,53 @@
"""Test for a Home Assistant bridge that changes cover features at runtime."""
from homeassistant.components.cover import CoverEntityFeature
from homeassistant.const import ATTR_SUPPORTED_FEATURES
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from ..common import (
device_config_changed,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_cover_add_feature_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that new features can be added at runtime."""
# Set up a basic cover that does not support position
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_cover.json"
)
await setup_test_accessories(hass, accessories)
cover = entity_registry.async_get("cover.family_room_north")
assert cover.unique_id == "00:00:00:00:00:00_123016423_166"
cover_state = hass.states.get("cover.family_room_north")
assert (
cover_state.attributes[ATTR_SUPPORTED_FEATURES]
is CoverEntityFeature.OPEN
| CoverEntityFeature.STOP
| CoverEntityFeature.CLOSE
| CoverEntityFeature.SET_POSITION
)
cover = entity_registry.async_get("cover.family_room_north")
assert cover.unique_id == "00:00:00:00:00:00_123016423_166"
# Now change the config to remove stop
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_basic_cover.json"
)
await device_config_changed(hass, accessories)
cover_state = hass.states.get("cover.family_room_north")
assert (
cover_state.attributes[ATTR_SUPPORTED_FEATURES]
is CoverEntityFeature.OPEN
| CoverEntityFeature.CLOSE
| CoverEntityFeature.SET_POSITION
)

View File

@@ -0,0 +1,359 @@
"""Regression tests for Ecobee 3.
https://github.com/home-assistant/core/issues/15336
"""
from typing import Any
from unittest import mock
from aiohomekit import AccessoryNotFoundError
from aiohomekit.testing import FakePairing
from homeassistant.components.climate import ClimateEntityFeature
from homeassistant.components.sensor import SensorStateClass
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
EntityTestInfo,
assert_devices_and_entities_created,
device_config_changed,
setup_accessories_from_file,
setup_test_accessories,
time_changed,
)
async def test_ecobee3_setup(hass: HomeAssistant) -> None:
"""Test that a Ecbobee 3 can be correctly setup in HA."""
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="HomeW",
model="ecobee3",
manufacturer="ecobee Inc.",
sw_version="4.2.394",
hw_version="",
serial_number="123456789012",
devices=[
DeviceTestInfo(
name="Kitchen",
model="REMOTE SENSOR",
manufacturer="ecobee Inc.",
sw_version="1.0.0",
hw_version="",
serial_number="AB1C",
unique_id="00:00:00:00:00:00:aid:2",
devices=[],
entities=[
EntityTestInfo(
entity_id="binary_sensor.kitchen",
friendly_name="Kitchen",
unique_id="00:00:00:00:00:00_2_56",
state="off",
),
],
),
DeviceTestInfo(
name="Porch",
model="REMOTE SENSOR",
manufacturer="ecobee Inc.",
sw_version="1.0.0",
hw_version="",
serial_number="AB2C",
unique_id="00:00:00:00:00:00:aid:3",
devices=[],
entities=[
EntityTestInfo(
entity_id="binary_sensor.porch",
friendly_name="Porch",
unique_id="00:00:00:00:00:00_3_56",
state="off",
),
],
),
DeviceTestInfo(
name="Basement",
model="REMOTE SENSOR",
manufacturer="ecobee Inc.",
sw_version="1.0.0",
hw_version="",
serial_number="AB3C",
unique_id="00:00:00:00:00:00:aid:4",
devices=[],
entities=[
EntityTestInfo(
entity_id="binary_sensor.basement",
friendly_name="Basement",
unique_id="00:00:00:00:00:00_4_56",
state="off",
),
],
),
],
entities=[
EntityTestInfo(
entity_id="climate.homew",
friendly_name="HomeW",
unique_id="00:00:00:00:00:00_1_16",
supported_features=(
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
| ClimateEntityFeature.TARGET_HUMIDITY
| ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON
),
capabilities={
"hvac_modes": ["off", "heat", "cool", "heat_cool"],
"min_temp": 7.2,
"max_temp": 33.3,
"min_humidity": 20,
"max_humidity": 50,
},
state="heat",
),
EntityTestInfo(
entity_id="sensor.homew_current_temperature",
friendly_name="HomeW Current Temperature",
unique_id="00:00:00:00:00:00_1_16_19",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
unit_of_measurement=UnitOfTemperature.CELSIUS,
state="21.8",
),
EntityTestInfo(
entity_id="select.homew_current_mode",
friendly_name="HomeW Current Mode",
unique_id="00:00:00:00:00:00_1_16_33",
capabilities={"options": ["home", "sleep", "away"]},
state="home",
),
],
),
)
async def test_ecobee3_setup_from_cache(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
hass_storage: dict[str, Any],
) -> None:
"""Test that Ecbobee can be correctly setup from its cached entity map."""
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
hass_storage["homekit_controller-entity-map"] = {
"version": 1,
"data": {
"pairings": {
HUB_TEST_ACCESSORY_ID: {
"config_num": 1,
"accessories": [
a.to_accessory_and_service_list() for a in accessories
],
}
}
},
}
await setup_test_accessories(hass, accessories)
climate = entity_registry.async_get("climate.homew")
assert climate.unique_id == "00:00:00:00:00:00_1_16"
occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1.unique_id == "00:00:00:00:00:00_2_56"
occ2 = entity_registry.async_get("binary_sensor.porch")
assert occ2.unique_id == "00:00:00:00:00:00_3_56"
occ3 = entity_registry.async_get("binary_sensor.basement")
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
async def test_ecobee3_setup_connection_failure(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that Ecbobee can be correctly setup from its cached entity map."""
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
# Test that the connection fails during initial setup.
# No entities should be created.
with mock.patch.object(FakePairing, "async_populate_accessories_state") as laac:
laac.side_effect = AccessoryNotFoundError("Connection failed")
# If there is no cached entity map and the accessory connection is
# failing then we have to fail the config entry setup.
config_entry, pairing = await setup_test_accessories(hass, accessories)
assert config_entry.state is ConfigEntryState.SETUP_RETRY
climate = entity_registry.async_get("climate.homew")
assert climate is None
# When accessory raises ConfigEntryNoteReady HA will retry - lets make
# sure there is no cruft causing conflicts left behind by now doing
# a successful setup.
# We just advance time by 5 minutes so that the retry happens, rather
# than manually invoking async_setup_entry.
await time_changed(hass, 5 * 60)
await hass.async_block_till_done(wait_background_tasks=True)
climate = entity_registry.async_get("climate.homew")
assert climate.unique_id == "00:00:00:00:00:00_1_16"
occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1.unique_id == "00:00:00:00:00:00_2_56"
occ2 = entity_registry.async_get("binary_sensor.porch")
assert occ2.unique_id == "00:00:00:00:00:00_3_56"
occ3 = entity_registry.async_get("binary_sensor.basement")
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
async def test_ecobee3_add_sensors_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that new sensors are automatically added."""
# Set up a base Ecobee 3 with no additional sensors.
# There shouldn't be any entities but climate visible.
accessories = await setup_accessories_from_file(hass, "ecobee3_no_sensors.json")
await setup_test_accessories(hass, accessories)
climate = entity_registry.async_get("climate.homew")
assert climate.unique_id == "00:00:00:00:00:00_1_16"
occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1 is None
occ2 = entity_registry.async_get("binary_sensor.porch")
assert occ2 is None
occ3 = entity_registry.async_get("binary_sensor.basement")
assert occ3 is None
# Now added 3 new sensors at runtime - sensors should appear and climate
# shouldn't be duplicated.
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
await device_config_changed(hass, accessories)
occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1.unique_id == "00:00:00:00:00:00_2_56"
occ2 = entity_registry.async_get("binary_sensor.porch")
assert occ2.unique_id == "00:00:00:00:00:00_3_56"
occ3 = entity_registry.async_get("binary_sensor.basement")
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
async def test_ecobee3_remove_sensors_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that sensors are automatically removed."""
# Set up a base Ecobee 3 with additional sensors.
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
await setup_test_accessories(hass, accessories)
climate = entity_registry.async_get("climate.homew")
assert climate.unique_id == "00:00:00:00:00:00_1_16"
occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1.unique_id == "00:00:00:00:00:00_2_56"
occ2 = entity_registry.async_get("binary_sensor.porch")
assert occ2.unique_id == "00:00:00:00:00:00_3_56"
occ3 = entity_registry.async_get("binary_sensor.basement")
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
assert hass.states.get("binary_sensor.kitchen") is not None
assert hass.states.get("binary_sensor.porch") is not None
assert hass.states.get("binary_sensor.basement") is not None
# Now remove 3 new sensors at runtime - sensors should disappear and climate
# shouldn't be duplicated.
accessories = await setup_accessories_from_file(hass, "ecobee3_no_sensors.json")
await device_config_changed(hass, accessories)
assert hass.states.get("binary_sensor.kitchen") is None
assert entity_registry.async_get("binary_sensor.kitchen") is None
assert hass.states.get("binary_sensor.porch") is None
assert entity_registry.async_get("binary_sensor.porch") is None
assert hass.states.get("binary_sensor.basement") is None
assert entity_registry.async_get("binary_sensor.basement") is None
# Now add the sensors back
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
await device_config_changed(hass, accessories)
occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1.unique_id == "00:00:00:00:00:00_2_56"
occ2 = entity_registry.async_get("binary_sensor.porch")
assert occ2.unique_id == "00:00:00:00:00:00_3_56"
occ3 = entity_registry.async_get("binary_sensor.basement")
assert occ3.unique_id == "00:00:00:00:00:00_4_56"
# Ensure the sensors are back
assert hass.states.get("binary_sensor.kitchen") is not None
assert occ1.id == entity_registry.async_get("binary_sensor.kitchen").id
assert hass.states.get("binary_sensor.porch") is not None
assert occ2.id == entity_registry.async_get("binary_sensor.porch").id
assert hass.states.get("binary_sensor.basement") is not None
assert occ3.id == entity_registry.async_get("binary_sensor.basement").id
async def test_ecobee3_services_and_chars_removed(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test handling removal of some services and chars."""
# Set up a base Ecobee 3 with additional sensors.
accessories = await setup_accessories_from_file(hass, "ecobee3.json")
await setup_test_accessories(hass, accessories)
climate = entity_registry.async_get("climate.homew")
assert climate.unique_id == "00:00:00:00:00:00_1_16"
assert hass.states.get("sensor.basement_temperature") is not None
assert hass.states.get("sensor.kitchen_temperature") is not None
assert hass.states.get("sensor.porch_temperature") is not None
assert hass.states.get("select.homew_current_mode") is not None
assert hass.states.get("button.homew_clear_hold") is not None
# Reconfigure with some of the chars removed and the basement temperature sensor
accessories = await setup_accessories_from_file(
hass, "ecobee3_service_removed.json"
)
await device_config_changed(hass, accessories)
# Make sure the climate entity is still there
assert hass.states.get("climate.homew") is not None
assert entity_registry.async_get("climate.homew") is not None
# Make sure the basement temperature sensor is gone
assert hass.states.get("sensor.basement_temperature") is None
assert entity_registry.async_get("select.basement_temperature") is None
# Make sure the current mode select and clear hold button are gone
assert hass.states.get("select.homew_current_mode") is None
assert entity_registry.async_get("select.homew_current_mode") is None
assert hass.states.get("button.homew_clear_hold") is None
assert entity_registry.async_get("button.homew_clear_hold") is None

View File

@@ -0,0 +1,186 @@
"""Test for a Home Assistant bridge that changes fan features at runtime."""
from homeassistant.components.fan import FanEntityFeature
from homeassistant.const import ATTR_SUPPORTED_FEATURES
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from ..common import (
device_config_changed,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_fan_add_feature_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that new features can be added at runtime."""
# Set up a basic fan that does not support oscillation
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_basic_fan.json"
)
await setup_test_accessories(hass, accessories)
fan = entity_registry.async_get("fan.living_room_fan")
assert fan.unique_id == "00:00:00:00:00:00_1256851357_8"
fan_state = hass.states.get("fan.living_room_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.DIRECTION
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
fan = entity_registry.async_get("fan.ceiling_fan")
assert fan.unique_id == "00:00:00:00:00:00_766313939_8"
fan_state = hass.states.get("fan.ceiling_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
# Now change the config to add oscillation
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_fan.json"
)
await device_config_changed(hass, accessories)
fan_state = hass.states.get("fan.living_room_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.DIRECTION
| FanEntityFeature.OSCILLATE
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
fan_state = hass.states.get("fan.ceiling_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
async def test_fan_remove_feature_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that features can be removed at runtime."""
# Set up a basic fan that does not support oscillation
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_fan.json"
)
await setup_test_accessories(hass, accessories)
fan = entity_registry.async_get("fan.living_room_fan")
assert fan.unique_id == "00:00:00:00:00:00_1256851357_8"
fan_state = hass.states.get("fan.living_room_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.DIRECTION
| FanEntityFeature.OSCILLATE
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
fan = entity_registry.async_get("fan.ceiling_fan")
assert fan.unique_id == "00:00:00:00:00:00_766313939_8"
fan_state = hass.states.get("fan.ceiling_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
# Now change the config to add oscillation
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_basic_fan.json"
)
await device_config_changed(hass, accessories)
fan_state = hass.states.get("fan.living_room_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.DIRECTION
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
fan_state = hass.states.get("fan.ceiling_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
async def test_bridge_with_two_fans_one_removed(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test a bridge with two fans and one gets removed."""
# Set up a basic fan that does not support oscillation
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_fan.json"
)
await setup_test_accessories(hass, accessories)
fan = entity_registry.async_get("fan.living_room_fan")
assert fan.unique_id == "00:00:00:00:00:00_1256851357_8"
fan_state = hass.states.get("fan.living_room_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.DIRECTION
| FanEntityFeature.OSCILLATE
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
fan = entity_registry.async_get("fan.ceiling_fan")
assert fan.unique_id == "00:00:00:00:00:00_766313939_8"
fan_state = hass.states.get("fan.ceiling_fan")
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
# Now change the config to remove one of the fans
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_fan_one_removed.json"
)
await device_config_changed(hass, accessories)
# Verify the first fan is still there
fan_state = hass.states.get("fan.living_room_fan")
assert entity_registry.async_get("fan.living_room_fan") is not None
assert (
fan_state.attributes[ATTR_SUPPORTED_FEATURES]
is FanEntityFeature.SET_SPEED
| FanEntityFeature.DIRECTION
| FanEntityFeature.OSCILLATE
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
# The second fan should have been removed
assert not hass.states.get("fan.ceiling_fan")
assert not entity_registry.async_get("fan.ceiling_fan")

View File

@@ -0,0 +1,52 @@
"""Test for a Home Assistant bridge that changes climate features at runtime."""
from homeassistant.components.climate import ATTR_SWING_MODES, ClimateEntityFeature
from homeassistant.const import ATTR_SUPPORTED_FEATURES
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from ..common import (
device_config_changed,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_cover_add_feature_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that new features can be added at runtime."""
# Set up a basic heater cooler that does not support swing mode
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_basic_heater_cooler.json"
)
await setup_test_accessories(hass, accessories)
climate = entity_registry.async_get("climate.89_living_room")
assert climate.unique_id == "00:00:00:00:00:00_1233851541_169"
climate_state = hass.states.get("climate.89_living_room")
assert (
climate_state.attributes[ATTR_SUPPORTED_FEATURES]
is ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON
)
assert ATTR_SWING_MODES not in climate_state.attributes
climate = entity_registry.async_get("climate.89_living_room")
assert climate.unique_id == "00:00:00:00:00:00_1233851541_169"
# Now change the config to add swing mode
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_heater_cooler.json"
)
await device_config_changed(hass, accessories)
climate_state = hass.states.get("climate.89_living_room")
assert (
climate_state.attributes[ATTR_SUPPORTED_FEATURES]
is ClimateEntityFeature.SWING_MODE
| ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON
)
assert climate_state.attributes[ATTR_SWING_MODES] == ["off", "vertical"]

View File

@@ -0,0 +1,64 @@
"""Tests for handling accessories on a Hue bridge via HomeKit."""
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import PERCENTAGE, EntityCategory
from homeassistant.core import HomeAssistant
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
DeviceTriggerInfo,
EntityTestInfo,
assert_devices_and_entities_created,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_hue_bridge_setup(hass: HomeAssistant) -> None:
"""Test that a Hue hub can be correctly setup in HA via HomeKit."""
accessories = await setup_accessories_from_file(hass, "hue_bridge.json")
await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="Philips hue - 482544",
model="BSB002",
manufacturer="Philips Lighting",
sw_version="1.32.1932126170",
hw_version="",
serial_number="123456",
devices=[
DeviceTestInfo(
name="Hue dimmer switch",
model="RWL021",
manufacturer="Philips",
sw_version="45.1.17846",
hw_version="",
serial_number="6623462389072572",
unique_id="00:00:00:00:00:00:aid:6623462389072572",
devices=[],
entities=[
EntityTestInfo(
entity_id="sensor.hue_dimmer_switch_battery",
capabilities={"state_class": SensorStateClass.MEASUREMENT},
friendly_name="Hue dimmer switch battery",
entity_category=EntityCategory.DIAGNOSTIC,
unique_id="00:00:00:00:00:00_6623462389072572_644245094400",
unit_of_measurement=PERCENTAGE,
state="100",
)
],
stateless_triggers=[
DeviceTriggerInfo(type="button1", subtype="single_press"),
DeviceTriggerInfo(type="button2", subtype="single_press"),
DeviceTriggerInfo(type="button3", subtype="single_press"),
DeviceTriggerInfo(type="button4", subtype="single_press"),
],
),
],
entities=[],
),
)

View File

@@ -0,0 +1,43 @@
"""Test for a Home Assistant bridge that changes humidifier min/max at runtime."""
from homeassistant.components.humidifier import ATTR_MAX_HUMIDITY, ATTR_MIN_HUMIDITY
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from ..common import (
device_config_changed,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_humidifier_change_range_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that min max can be changed at runtime."""
# Set up a basic humidifier
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_humidifier.json"
)
await setup_test_accessories(hass, accessories)
humidifier = entity_registry.async_get("humidifier.humidifier_182a")
assert humidifier.unique_id == "00:00:00:00:00:00_293334836_8"
humidifier_state = hass.states.get("humidifier.humidifier_182a")
assert humidifier_state.attributes[ATTR_MIN_HUMIDITY] == 0
assert humidifier_state.attributes[ATTR_MAX_HUMIDITY] == 100
cover = entity_registry.async_get("humidifier.humidifier_182a")
assert cover.unique_id == "00:00:00:00:00:00_293334836_8"
# Now change min/max values
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_humidifier_new_range.json"
)
await device_config_changed(hass, accessories)
humidifier_state = hass.states.get("humidifier.humidifier_182a")
assert humidifier_state.attributes[ATTR_MIN_HUMIDITY] == 20
assert humidifier_state.attributes[ATTR_MAX_HUMIDITY] == 80

View File

@@ -0,0 +1,77 @@
"""Make sure that existing Koogeek LS1 support isn't broken."""
from datetime import timedelta
from unittest import mock
from aiohomekit.exceptions import AccessoryDisconnectedError, EncryptionError
from aiohomekit.model import CharacteristicsTypes, ServicesTypes
from aiohomekit.testing import FakeController, FakePairing
import pytest
from homeassistant.components.homekit_controller.connection import (
MAX_POLL_FAILURES_TO_DECLARE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util
from ..common import Helper, setup_accessories_from_file, setup_test_accessories
from tests.common import async_fire_time_changed
LIGHT_ON = ("lightbulb", "on")
@pytest.mark.parametrize("failure_cls", [AccessoryDisconnectedError, EncryptionError])
async def test_recover_from_failure(hass: HomeAssistant, failure_cls) -> None:
"""Test that entity actually recovers from a network connection drop.
See https://github.com/home-assistant/core/issues/18949
"""
accessories = await setup_accessories_from_file(hass, "koogeek_ls1.json")
config_entry, pairing = await setup_test_accessories(hass, accessories)
helper = Helper(
hass,
"light.koogeek_ls1_20833f_light_strip",
pairing,
accessories[0],
config_entry,
)
# Set light state on fake device to off
state = await helper.async_update(
ServicesTypes.LIGHTBULB, {CharacteristicsTypes.ON: False}
)
# Test that entity starts off in a known state
assert state.state == "off"
# Test that entity remains in the same state if there is a network error
next_update = dt_util.utcnow() + timedelta(seconds=60)
with (
mock.patch.object(FakePairing, "get_characteristics") as get_char,
mock.patch.object(
FakeController,
"async_reachable",
return_value=False,
),
):
get_char.side_effect = failure_cls("Disconnected")
# Test that a poll triggers unavailable
for _ in range(MAX_POLL_FAILURES_TO_DECLARE_UNAVAILABLE + 2):
state = await helper.poll_and_get_state()
assert state.state == "unavailable"
chars = get_char.call_args[0][0]
assert set(chars) == {(1, 8), (1, 9), (1, 10), (1, 11)}
# Test that entity changes state when network error goes away
next_update += timedelta(seconds=60)
async_fire_time_changed(hass, next_update)
await hass.async_block_till_done()
state = await helper.async_update(
ServicesTypes.LIGHTBULB, {CharacteristicsTypes.ON: True}
)
assert state.state == "on"

View File

@@ -0,0 +1,61 @@
"""Make sure that existing Koogeek SW2 is enumerated correctly.
This Koogeek device has a custom power sensor that extra handling.
It should have 2 entities - the actual switch and a sensor for power usage.
"""
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import UnitOfPower
from homeassistant.core import HomeAssistant
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
EntityTestInfo,
assert_devices_and_entities_created,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_koogeek_sw2_setup(hass: HomeAssistant) -> None:
"""Test that a Koogeek LS1 can be correctly setup in HA."""
accessories = await setup_accessories_from_file(hass, "koogeek_sw2.json")
await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="Koogeek-SW2-187A91",
model="KH02CN",
manufacturer="Koogeek",
sw_version="1.0.3",
hw_version="",
serial_number="CNNT061751001372",
devices=[],
entities=[
EntityTestInfo(
entity_id="switch.koogeek_sw2_187a91_switch_1",
friendly_name="Koogeek-SW2-187A91 Switch 1",
unique_id="00:00:00:00:00:00_1_8",
state="off",
),
EntityTestInfo(
entity_id="switch.koogeek_sw2_187a91_switch_2",
friendly_name="Koogeek-SW2-187A91 Switch 2",
unique_id="00:00:00:00:00:00_1_11",
state="off",
),
EntityTestInfo(
entity_id="sensor.koogeek_sw2_187a91_power",
friendly_name="Koogeek-SW2-187A91 Power",
unique_id="00:00:00:00:00:00_1_14_18",
unit_of_measurement=UnitOfPower.WATT,
capabilities={"state_class": SensorStateClass.MEASUREMENT},
state="0",
),
],
),
)

View File

@@ -0,0 +1,44 @@
"""Test for a Home Assistant bridge that changes light features at runtime."""
from homeassistant.components.light import ATTR_SUPPORTED_COLOR_MODES, ColorMode
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from ..common import (
device_config_changed,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_light_add_feature_at_runtime(
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test that new features can be added at runtime."""
# Set up a basic light that does not support color
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_basic_light.json"
)
await setup_test_accessories(hass, accessories)
light = entity_registry.async_get("light.laundry_smoke_ed78")
assert light.unique_id == "00:00:00:00:00:00_3982136094_608"
light_state = hass.states.get("light.laundry_smoke_ed78")
assert light_state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.BRIGHTNESS]
light = entity_registry.async_get("light.laundry_smoke_ed78")
assert light.unique_id == "00:00:00:00:00:00_3982136094_608"
# Now add hue and saturation
accessories = await setup_accessories_from_file(
hass, "home_assistant_bridge_light.json"
)
await device_config_changed(hass, accessories)
light_state = hass.states.get("light.laundry_smoke_ed78")
assert light_state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [
ColorMode.COLOR_TEMP,
ColorMode.HS,
]

View File

@@ -0,0 +1,49 @@
"""Regression tests for Netamo Doorbell.
https://github.com/home-assistant/core/issues/44596
"""
from homeassistant.core import HomeAssistant
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
DeviceTriggerInfo,
EntityTestInfo,
assert_devices_and_entities_created,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_netamo_doorbell_setup(hass: HomeAssistant) -> None:
"""Test that a Netamo Doorbell can be correctly setup in HA."""
accessories = await setup_accessories_from_file(hass, "netamo_doorbell.json")
await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="Netatmo-Doorbell-g738658",
model="Netatmo Doorbell",
manufacturer="Netatmo",
sw_version="80.0.0",
hw_version="",
serial_number="g738658",
devices=[],
entities=[
EntityTestInfo(
entity_id="camera.netatmo_doorbell_g738658",
friendly_name="Netatmo-Doorbell-g738658",
unique_id="00:00:00:00:00:00_1",
state="idle",
),
],
stateless_triggers=[
DeviceTriggerInfo(type="doorbell", subtype="single_press"),
DeviceTriggerInfo(type="doorbell", subtype="double_press"),
DeviceTriggerInfo(type="doorbell", subtype="long_press"),
],
),
)

View File

@@ -0,0 +1,77 @@
"""Make sure that existing VOCOlinc VP3 support isn't broken."""
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from ..common import (
HUB_TEST_ACCESSORY_ID,
DeviceTestInfo,
EntityTestInfo,
assert_devices_and_entities_created,
setup_accessories_from_file,
setup_test_accessories,
)
async def test_vocolinc_vp3_setup(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that a VOCOlinc VP3 can be correctly setup in HA."""
outlet = entity_registry.async_get_or_create(
"switch",
"homekit_controller",
"homekit-EU0121203xxxxx07-48",
suggested_object_id="original_vocolinc_vp3_outlet",
)
sensor = entity_registry.async_get_or_create(
"sensor",
"homekit_controller",
"homekit-EU0121203xxxxx07-aid:1-sid:48-cid:97",
suggested_object_id="original_vocolinc_vp3_power",
)
accessories = await setup_accessories_from_file(hass, "vocolinc_vp3.json")
await setup_test_accessories(hass, accessories)
await assert_devices_and_entities_created(
hass,
DeviceTestInfo(
unique_id=HUB_TEST_ACCESSORY_ID,
name="VOCOlinc-VP3-123456",
model="VP3",
manufacturer="VOCOlinc",
sw_version="1.101.2",
hw_version="1.0.3",
serial_number="EU0121203xxxxx07",
devices=[],
entities=[
EntityTestInfo(
entity_id="switch.original_vocolinc_vp3_outlet",
friendly_name="VOCOlinc-VP3-123456 Outlet",
unique_id="00:00:00:00:00:00_1_48",
state="on",
),
EntityTestInfo(
entity_id="sensor.original_vocolinc_vp3_power",
friendly_name="VOCOlinc-VP3-123456 Power",
unique_id="00:00:00:00:00:00_1_48_97",
unit_of_measurement=UnitOfPower.WATT,
capabilities={"state_class": SensorStateClass.MEASUREMENT},
state="0",
),
],
),
)
assert (
entity_registry.async_get(outlet.entity_id).unique_id
== "00:00:00:00:00:00_1_48"
)
assert (
entity_registry.async_get(sensor.entity_id).unique_id
== "00:00:00:00:00:00_1_48_97"
)