Refactor home --> hap for Homematic IP Cloud (#27368)
* Refactor home to hap for Homematic IP Cloud * Add some tests * Rename ha_entity --> ha_state * use asynctest.Mock
This commit is contained in:
1 parent
0c8e208fd8
commit
8bbf261302
17 files changed
+872
-289
No files matched your search
@@ -6,10 +6,14 @@ import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.homematicip_cloud import (
|
||||
CONF_ACCESSPOINT,
|
||||
CONF_AUTHTOKEN,
|
||||
DOMAIN as HMIPC_DOMAIN,
|
||||
async_setup as hmip_async_setup,
|
||||
const as hmipc,
|
||||
hap as hmip_hap,
|
||||
)
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .helper import AUTH_TOKEN, HAPID, HomeTemplate
|
||||
@@ -19,7 +23,7 @@ from tests.common import MockConfigEntry, mock_coro
|
||||
|
||||
@pytest.fixture(name="mock_connection")
|
||||
def mock_connection_fixture():
|
||||
"""Return a mockked connection."""
|
||||
"""Return a mocked connection."""
|
||||
connection = MagicMock(spec=AsyncConnection)
|
||||
|
||||
def _rest_call_side_effect(path, body=None):
|
||||
@@ -39,7 +43,7 @@ def default_mock_home_fixture(mock_connection):
|
||||
|
||||
@pytest.fixture(name="hmip_config_entry")
|
||||
def hmip_config_entry_fixture():
|
||||
"""Create a fake config entriy for homematic ip cloud."""
|
||||
"""Create a mock config entriy for homematic ip cloud."""
|
||||
entry_data = {
|
||||
hmipc.HMIPC_HAPID: HAPID,
|
||||
hmipc.HMIPC_AUTHTOKEN: AUTH_TOKEN,
|
||||
@@ -67,9 +71,32 @@ async def default_mock_hap_fixture(
|
||||
hap = hmip_hap.HomematicipHAP(hass, hmip_config_entry)
|
||||
with patch.object(hap, "get_hap", return_value=mock_coro(default_mock_home)):
|
||||
assert await hap.async_setup() is True
|
||||
default_mock_home.on_update(hap.async_update)
|
||||
default_mock_home.on_create(hap.async_create_entity)
|
||||
|
||||
hass.data[HMIPC_DOMAIN] = {HAPID: hap}
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return hap
|
||||
|
||||
|
||||
@pytest.fixture(name="hmip_config")
|
||||
def hmip_config_fixture():
|
||||
"""Create a config for homematic ip cloud."""
|
||||
|
||||
entry_data = {CONF_ACCESSPOINT: HAPID, CONF_AUTHTOKEN: AUTH_TOKEN, CONF_NAME: ""}
|
||||
|
||||
return {hmipc.DOMAIN: [entry_data]}
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_hap_with_service")
|
||||
async def mock_hap_with_service_fixture(
|
||||
hass: HomeAssistant, default_mock_hap, hmip_config
|
||||
):
|
||||
"""Create a fake homematic access point with hass services."""
|
||||
|
||||
await hmip_async_setup(hass, hmip_config)
|
||||
await hass.async_block_till_done()
|
||||
hass.data[HMIPC_DOMAIN] = {HAPID: default_mock_hap}
|
||||
return default_mock_hap
|
||||
@@ -1,18 +1,25 @@
|
||||
"""Helper for HomematicIP Cloud Tests."""
|
||||
import json
|
||||
from unittest.mock import Mock
|
||||
from asynctest import Mock
|
||||
|
||||
from homematicip.aio.class_maps import (
|
||||
TYPE_CLASS_MAP,
|
||||
TYPE_GROUP_MAP,
|
||||
TYPE_SECURITY_EVENT_MAP,
|
||||
)
|
||||
from homematicip.aio.device import AsyncDevice
|
||||
from homematicip.aio.group import AsyncGroup
|
||||
from homematicip.aio.home import AsyncHome
|
||||
from homematicip.home import Home
|
||||
|
||||
from homeassistant.components.homematicip_cloud.device import (
|
||||
ATTR_IS_GROUP,
|
||||
ATTR_MODEL_TYPE,
|
||||
)
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
HAPID = "Mock_HAP"
|
||||
HAPID = "3014F7110000000000000001"
|
||||
AUTH_TOKEN = "1234"
|
||||
HOME_JSON = "homematicip_cloud.json"
|
||||
|
||||
@@ -21,28 +28,38 @@ def get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
):
|
||||
"""Get and test basic device."""
|
||||
ha_entity = hass.states.get(entity_id)
|
||||
assert ha_entity is not None
|
||||
assert ha_entity.attributes["model_type"] == device_model
|
||||
assert ha_entity.name == entity_name
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state is not None
|
||||
if device_model:
|
||||
assert ha_state.attributes[ATTR_MODEL_TYPE] == device_model
|
||||
assert ha_state.name == entity_name
|
||||
|
||||
hmip_device = default_mock_hap.home.template.search_mock_device_by_id(
|
||||
ha_entity.attributes["id"]
|
||||
)
|
||||
assert hmip_device is not None
|
||||
return ha_entity, hmip_device
|
||||
hmip_device = default_mock_hap.hmip_device_by_entity_id.get(entity_id)
|
||||
if hmip_device:
|
||||
if isinstance(hmip_device, AsyncDevice):
|
||||
assert ha_state.attributes[ATTR_IS_GROUP] is False
|
||||
elif isinstance(hmip_device, AsyncGroup):
|
||||
assert ha_state.attributes[ATTR_IS_GROUP] is True
|
||||
return ha_state, hmip_device
|
||||
|
||||
|
||||
async def async_manipulate_test_data(
|
||||
hass, hmip_device, attribute, new_value, channel=1
|
||||
hass, hmip_device, attribute, new_value, channel=1, fire_device=None
|
||||
):
|
||||
"""Set new value on hmip device."""
|
||||
if channel == 1:
|
||||
setattr(hmip_device, attribute, new_value)
|
||||
functional_channel = hmip_device.functionalChannels[channel]
|
||||
setattr(functional_channel, attribute, new_value)
|
||||
if hasattr(hmip_device, "functionalChannels"):
|
||||
functional_channel = hmip_device.functionalChannels[channel]
|
||||
setattr(functional_channel, attribute, new_value)
|
||||
|
||||
fire_target = hmip_device if fire_device is None else fire_device
|
||||
|
||||
if isinstance(fire_target, AsyncHome):
|
||||
fire_target.fire_update_event(fire_target._rawJSONData) # pylint: disable=W0212
|
||||
else:
|
||||
fire_target.fire_update_event()
|
||||
|
||||
hmip_device.fire_update_event()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@@ -66,8 +83,8 @@ class HomeTemplate(Home):
|
||||
def __init__(self, connection=None):
|
||||
"""Init template with connection."""
|
||||
super().__init__(connection=connection)
|
||||
self.mock_devices = []
|
||||
self.mock_groups = []
|
||||
self.label = "Access Point"
|
||||
self.model_type = "HmIP-HAP"
|
||||
|
||||
def init_home(self, json_path=HOME_JSON):
|
||||
"""Init template with json."""
|
||||
@@ -78,24 +95,15 @@ class HomeTemplate(Home):
|
||||
|
||||
def _generate_mocks(self):
|
||||
"""Generate mocks for groups and devices."""
|
||||
mock_devices = []
|
||||
for device in self.devices:
|
||||
self.mock_devices.append(_get_mock(device))
|
||||
mock_devices.append(_get_mock(device))
|
||||
self.devices = mock_devices
|
||||
|
||||
mock_groups = []
|
||||
for group in self.groups:
|
||||
self.mock_groups.append(_get_mock(group))
|
||||
|
||||
def search_mock_device_by_id(self, device_id):
|
||||
"""Search a device by given id."""
|
||||
for device in self.mock_devices:
|
||||
if device.id == device_id:
|
||||
return device
|
||||
return None
|
||||
|
||||
def search_mock_group_by_id(self, group_id):
|
||||
"""Search a group by given id."""
|
||||
for group in self.mock_groups:
|
||||
if group.id == group_id:
|
||||
return group
|
||||
return None
|
||||
mock_groups.append(_get_mock(group))
|
||||
self.groups = mock_groups
|
||||
|
||||
def get_async_home_mock(self):
|
||||
"""
|
||||
@@ -105,19 +113,11 @@ class HomeTemplate(Home):
|
||||
and sets reuired attributes.
|
||||
"""
|
||||
mock_home = Mock(
|
||||
check_connection=self._connection,
|
||||
id=HAPID,
|
||||
connected=True,
|
||||
dutyCycle=self.dutyCycle,
|
||||
devices=self.mock_devices,
|
||||
groups=self.mock_groups,
|
||||
weather=self.weather,
|
||||
location=self.location,
|
||||
label="home label",
|
||||
template=self,
|
||||
spec=AsyncHome,
|
||||
spec=AsyncHome, wraps=self, label="Access Point", modelType="HmIP-HAP"
|
||||
)
|
||||
mock_home.__dict__.update(self.__dict__)
|
||||
mock_home.name = ""
|
||||
|
||||
return mock_home
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,289 @@
|
||||
"""Tests for HomematicIP Cloud binary sensor."""
|
||||
from homematicip.base.enums import SmokeDetectorAlarmType, WindowState
|
||||
|
||||
from homeassistant.components.homematicip_cloud.binary_sensor import (
|
||||
ATTR_ACCELERATION_SENSOR_MODE,
|
||||
ATTR_ACCELERATION_SENSOR_NEUTRAL_POSITION,
|
||||
ATTR_ACCELERATION_SENSOR_SENSITIVITY,
|
||||
ATTR_ACCELERATION_SENSOR_TRIGGER_ANGLE,
|
||||
ATTR_LOW_BATTERY,
|
||||
ATTR_MOTION_DETECTED,
|
||||
)
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
|
||||
from .helper import async_manipulate_test_data, get_and_check_entity_basics
|
||||
|
||||
|
||||
async def test_hmip_acceleration_sensor(hass, default_mock_hap):
|
||||
"""Test HomematicipAccelerationSensor."""
|
||||
entity_id = "binary_sensor.garagentor"
|
||||
entity_name = "Garagentor"
|
||||
device_model = "HmIP-SAM"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_ON
|
||||
assert ha_state.attributes[ATTR_ACCELERATION_SENSOR_MODE] == "FLAT_DECT"
|
||||
assert ha_state.attributes[ATTR_ACCELERATION_SENSOR_NEUTRAL_POSITION] == "VERTICAL"
|
||||
assert (
|
||||
ha_state.attributes[ATTR_ACCELERATION_SENSOR_SENSITIVITY] == "SENSOR_RANGE_4G"
|
||||
)
|
||||
assert ha_state.attributes[ATTR_ACCELERATION_SENSOR_TRIGGER_ANGLE] == 45
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
|
||||
await async_manipulate_test_data(
|
||||
hass, hmip_device, "accelerationSensorTriggered", False
|
||||
)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
||||
|
||||
await async_manipulate_test_data(
|
||||
hass, hmip_device, "accelerationSensorTriggered", True
|
||||
)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 2
|
||||
|
||||
|
||||
async def test_hmip_contact_interface(hass, default_mock_hap):
|
||||
"""Test HomematicipContactInterface."""
|
||||
entity_id = "binary_sensor.kontakt_schnittstelle_unterputz_1_fach"
|
||||
entity_name = "Kontakt-Schnittstelle Unterputz – 1-fach"
|
||||
device_model = "HmIP-FCI1"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "windowState", WindowState.OPEN)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "windowState", None)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_hmip_shutter_contact(hass, default_mock_hap):
|
||||
"""Test HomematicipShutterContact."""
|
||||
entity_id = "binary_sensor.fenstergriffsensor"
|
||||
entity_name = "Fenstergriffsensor"
|
||||
device_model = "HmIP-SRH"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_ON
|
||||
await async_manipulate_test_data(
|
||||
hass, hmip_device, "windowState", WindowState.CLOSED
|
||||
)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "windowState", None)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_hmip_motion_detector(hass, default_mock_hap):
|
||||
"""Test HomematicipMotionDetector."""
|
||||
entity_id = "binary_sensor.bewegungsmelder_fur_55er_rahmen_innen"
|
||||
entity_name = "Bewegungsmelder für 55er Rahmen – innen"
|
||||
device_model = "HmIP-SMI55"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "motionDetected", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_hmip_presence_detector(hass, default_mock_hap):
|
||||
"""Test HomematicipPresenceDetector."""
|
||||
entity_id = "binary_sensor.spi_1"
|
||||
entity_name = "SPI_1"
|
||||
device_model = "HmIP-SPI"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "presenceDetected", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_hmip_smoke_detector(hass, default_mock_hap):
|
||||
"""Test HomematicipSmokeDetector."""
|
||||
entity_id = "binary_sensor.rauchwarnmelder"
|
||||
entity_name = "Rauchwarnmelder"
|
||||
device_model = "HmIP-SWSD"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(
|
||||
hass,
|
||||
hmip_device,
|
||||
"smokeDetectorAlarmType",
|
||||
SmokeDetectorAlarmType.PRIMARY_ALARM,
|
||||
)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_hmip_water_detector(hass, default_mock_hap):
|
||||
"""Test HomematicipWaterDetector."""
|
||||
entity_id = "binary_sensor.wassersensor"
|
||||
entity_name = "Wassersensor"
|
||||
device_model = "HmIP-SWD"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "waterlevelDetected", True)
|
||||
await async_manipulate_test_data(hass, hmip_device, "moistureDetected", False)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "waterlevelDetected", True)
|
||||
await async_manipulate_test_data(hass, hmip_device, "moistureDetected", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "waterlevelDetected", False)
|
||||
await async_manipulate_test_data(hass, hmip_device, "moistureDetected", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "waterlevelDetected", False)
|
||||
await async_manipulate_test_data(hass, hmip_device, "moistureDetected", False)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_hmip_storm_sensor(hass, default_mock_hap):
|
||||
"""Test HomematicipStormSensor."""
|
||||
entity_id = "binary_sensor.weather_sensor_plus_storm"
|
||||
entity_name = "Weather Sensor – plus Storm"
|
||||
device_model = "HmIP-SWO-PL"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "storm", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_hmip_rain_sensor(hass, default_mock_hap):
|
||||
"""Test HomematicipRainSensor."""
|
||||
entity_id = "binary_sensor.wettersensor_pro_raining"
|
||||
entity_name = "Wettersensor - pro Raining"
|
||||
device_model = "HmIP-SWO-PR"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "raining", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_hmip_sunshine_sensor(hass, default_mock_hap):
|
||||
"""Test HomematicipSunshineSensor."""
|
||||
entity_id = "binary_sensor.wettersensor_pro_sunshine"
|
||||
entity_name = "Wettersensor - pro Sunshine"
|
||||
device_model = "HmIP-SWO-PR"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_ON
|
||||
assert ha_state.attributes["today_sunshine_duration_in_minutes"] == 100
|
||||
await async_manipulate_test_data(hass, hmip_device, "sunshine", False)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_hmip_battery_sensor(hass, default_mock_hap):
|
||||
"""Test HomematicipSunshineSensor."""
|
||||
entity_id = "binary_sensor.wohnungsture_battery"
|
||||
entity_name = "Wohnungstüre Battery"
|
||||
device_model = "HMIP-SWDO"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "lowBat", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_hmip_security_zone_sensor_group(hass, default_mock_hap):
|
||||
"""Test HomematicipSecurityZoneSensorGroup."""
|
||||
entity_id = "binary_sensor.internal_securityzone"
|
||||
entity_name = "INTERNAL SecurityZone"
|
||||
device_model = "HmIP-SecurityZone"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
await async_manipulate_test_data(hass, hmip_device, "motionDetected", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
assert ha_state.attributes[ATTR_MOTION_DETECTED] is True
|
||||
|
||||
|
||||
async def test_hmip_security_sensor_group(hass, default_mock_hap):
|
||||
"""Test HomematicipSecuritySensorGroup."""
|
||||
entity_id = "binary_sensor.buro_sensors"
|
||||
entity_name = "Büro Sensors"
|
||||
device_model = None
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
assert not ha_state.attributes.get("low_bat")
|
||||
await async_manipulate_test_data(hass, hmip_device, "lowBat", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
assert ha_state.attributes[ATTR_LOW_BATTERY] is True
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "lowBat", False)
|
||||
await async_manipulate_test_data(
|
||||
hass,
|
||||
hmip_device,
|
||||
"smokeDetectorAlarmType",
|
||||
SmokeDetectorAlarmType.PRIMARY_ALARM,
|
||||
)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
assert (
|
||||
ha_state.attributes["smoke_detector_alarm"]
|
||||
== SmokeDetectorAlarmType.PRIMARY_ALARM
|
||||
)
|
||||
@@ -1,41 +0,0 @@
|
||||
"""Tests for HomematicIP Cloud lights."""
|
||||
import logging
|
||||
|
||||
from tests.components.homematicip_cloud.helper import (
|
||||
async_manipulate_test_data,
|
||||
get_and_check_entity_basics,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def test_hmip_sam(hass, default_mock_hap):
|
||||
"""Test HomematicipLight."""
|
||||
entity_id = "binary_sensor.garagentor"
|
||||
entity_name = "Garagentor"
|
||||
device_model = "HmIP-SAM"
|
||||
|
||||
ha_entity, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_entity.state == "on"
|
||||
assert ha_entity.attributes["acceleration_sensor_mode"] == "FLAT_DECT"
|
||||
assert ha_entity.attributes["acceleration_sensor_neutral_position"] == "VERTICAL"
|
||||
assert ha_entity.attributes["acceleration_sensor_sensitivity"] == "SENSOR_RANGE_4G"
|
||||
assert ha_entity.attributes["acceleration_sensor_trigger_angle"] == 45
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
|
||||
await async_manipulate_test_data(
|
||||
hass, hmip_device, "accelerationSensorTriggered", False
|
||||
)
|
||||
ha_entity = hass.states.get(entity_id)
|
||||
assert ha_entity.state == "off"
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
||||
|
||||
await async_manipulate_test_data(
|
||||
hass, hmip_device, "accelerationSensorTriggered", True
|
||||
)
|
||||
ha_entity = hass.states.get(entity_id)
|
||||
assert ha_entity.state == "on"
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 2
|
||||
@@ -0,0 +1,196 @@
|
||||
"""Tests for HomematicIP Cloud light."""
|
||||
from homematicip.base.enums import RGBColorState
|
||||
|
||||
from homeassistant.components.homematicip_cloud.light import (
|
||||
ATTR_ENERGY_COUNTER,
|
||||
ATTR_POWER_CONSUMPTION,
|
||||
)
|
||||
from homeassistant.components.light import ATTR_BRIGHTNESS, ATTR_COLOR_NAME
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
|
||||
from .helper import async_manipulate_test_data, get_and_check_entity_basics
|
||||
|
||||
|
||||
async def test_hmip_light(hass, default_mock_hap):
|
||||
"""Test HomematicipLight."""
|
||||
entity_id = "light.treppe"
|
||||
entity_name = "Treppe"
|
||||
device_model = "HmIP-BSL"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
||||
assert hmip_device.mock_calls[-1][0] == "turn_off"
|
||||
assert hmip_device.mock_calls[-1][1] == ()
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
||||
assert hmip_device.mock_calls[-1][0] == "turn_on"
|
||||
assert hmip_device.mock_calls[-1][1] == ()
|
||||
|
||||
await async_manipulate_test_data(hass, hmip_device, "on", True)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_hmip_notification_light(hass, default_mock_hap):
|
||||
"""Test HomematicipNotificationLight."""
|
||||
entity_id = "light.treppe_top_notification"
|
||||
entity_name = "Treppe Top Notification"
|
||||
device_model = "HmIP-BSL"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
|
||||
# Send all color via service call.
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert hmip_device.mock_calls[-1][0] == "set_rgb_dim_level"
|
||||
assert hmip_device.mock_calls[-1][1] == (2, RGBColorState.RED, 1.0)
|
||||
|
||||
color_list = {
|
||||
RGBColorState.WHITE: [0.0, 0.0],
|
||||
RGBColorState.RED: [0.0, 100.0],
|
||||
RGBColorState.YELLOW: [60.0, 100.0],
|
||||
RGBColorState.GREEN: [120.0, 100.0],
|
||||
RGBColorState.TURQUOISE: [180.0, 100.0],
|
||||
RGBColorState.BLUE: [240.0, 100.0],
|
||||
RGBColorState.PURPLE: [300.0, 100.0],
|
||||
}
|
||||
|
||||
for color, hs_color in color_list.items():
|
||||
await hass.services.async_call(
|
||||
"light",
|
||||
"turn_on",
|
||||
{"entity_id": entity_id, "hs_color": hs_color},
|
||||
blocking=True,
|
||||
)
|
||||
assert hmip_device.mock_calls[-1][0] == "set_rgb_dim_level"
|
||||
assert hmip_device.mock_calls[-1][1] == (2, color, 0.0392156862745098)
|
||||
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 8
|
||||
|
||||
assert hmip_device.mock_calls[-1][0] == "set_rgb_dim_level"
|
||||
assert hmip_device.mock_calls[-1][1] == (
|
||||
2,
|
||||
RGBColorState.PURPLE,
|
||||
0.0392156862745098,
|
||||
)
|
||||
await async_manipulate_test_data(hass, hmip_device, "dimLevel", 1, 2)
|
||||
await async_manipulate_test_data(
|
||||
hass, hmip_device, "simpleRGBColorState", RGBColorState.PURPLE, 2
|
||||
)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
assert ha_state.attributes[ATTR_COLOR_NAME] == RGBColorState.PURPLE
|
||||
assert ha_state.attributes[ATTR_BRIGHTNESS] == 255
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 11
|
||||
assert hmip_device.mock_calls[-1][0] == "set_rgb_dim_level"
|
||||
assert hmip_device.mock_calls[-1][1] == (2, RGBColorState.PURPLE, 0.0)
|
||||
await async_manipulate_test_data(hass, hmip_device, "dimLevel", 0, 2)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_hmip_dimmer(hass, default_mock_hap):
|
||||
"""Test HomematicipDimmer."""
|
||||
entity_id = "light.schlafzimmerlicht"
|
||||
entity_name = "Schlafzimmerlicht"
|
||||
device_model = "HmIP-BDT"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert hmip_device.mock_calls[-1][0] == "set_dim_level"
|
||||
assert hmip_device.mock_calls[-1][1] == (1,)
|
||||
|
||||
await hass.services.async_call(
|
||||
"light",
|
||||
"turn_on",
|
||||
{"entity_id": entity_id, "brightness_pct": "100"},
|
||||
blocking=True,
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 2
|
||||
assert hmip_device.mock_calls[-1][0] == "set_dim_level"
|
||||
assert hmip_device.mock_calls[-1][1] == (1.0,)
|
||||
await async_manipulate_test_data(hass, hmip_device, "dimLevel", 1)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
assert ha_state.attributes[ATTR_BRIGHTNESS] == 255
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 4
|
||||
assert hmip_device.mock_calls[-1][0] == "set_dim_level"
|
||||
assert hmip_device.mock_calls[-1][1] == (0,)
|
||||
await async_manipulate_test_data(hass, hmip_device, "dimLevel", 0)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_hmip_light_measuring(hass, default_mock_hap):
|
||||
"""Test HomematicipLightMeasuring."""
|
||||
entity_id = "light.flur_oben"
|
||||
entity_name = "Flur oben"
|
||||
device_model = "HmIP-BSM"
|
||||
|
||||
ha_state, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_state.state == STATE_OFF
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
||||
assert hmip_device.mock_calls[-1][0] == "turn_on"
|
||||
assert hmip_device.mock_calls[-1][1] == ()
|
||||
await async_manipulate_test_data(hass, hmip_device, "on", True)
|
||||
await async_manipulate_test_data(hass, hmip_device, "currentPowerConsumption", 50)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_ON
|
||||
assert ha_state.attributes[ATTR_POWER_CONSUMPTION] == 50
|
||||
assert ha_state.attributes[ATTR_ENERGY_COUNTER] == 6.33
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 4
|
||||
assert hmip_device.mock_calls[-1][0] == "turn_off"
|
||||
assert hmip_device.mock_calls[-1][1] == ()
|
||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||
ha_state = hass.states.get(entity_id)
|
||||
assert ha_state.state == STATE_OFF
|
||||
@@ -1,77 +0,0 @@
|
||||
"""Tests for HomematicIP Cloud lights."""
|
||||
import logging
|
||||
|
||||
from tests.components.homematicip_cloud.helper import (
|
||||
async_manipulate_test_data,
|
||||
get_and_check_entity_basics,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def test_hmip_light(hass, default_mock_hap):
|
||||
"""Test HomematicipLight."""
|
||||
entity_id = "light.treppe"
|
||||
entity_name = "Treppe"
|
||||
device_model = "HmIP-BSL"
|
||||
|
||||
ha_entity, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_entity.state == "on"
|
||||
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
||||
assert hmip_device.mock_calls[-1][0] == "turn_off"
|
||||
await async_manipulate_test_data(hass, hmip_device, "on", False)
|
||||
ha_entity = hass.states.get(entity_id)
|
||||
assert ha_entity.state == "off"
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
||||
assert hmip_device.mock_calls[-1][0] == "turn_on"
|
||||
await async_manipulate_test_data(hass, hmip_device, "on", True)
|
||||
ha_entity = hass.states.get(entity_id)
|
||||
assert ha_entity.state == "on"
|
||||
|
||||
|
||||
# HomematicipLightMeasuring
|
||||
# HomematicipDimmer
|
||||
|
||||
|
||||
async def test_hmip_notification_light(hass, default_mock_hap):
|
||||
"""Test HomematicipNotificationLight."""
|
||||
entity_id = "light.treppe_top_notification"
|
||||
entity_name = "Treppe Top Notification"
|
||||
device_model = "HmIP-BSL"
|
||||
|
||||
ha_entity, hmip_device = get_and_check_entity_basics(
|
||||
hass, default_mock_hap, entity_id, entity_name, device_model
|
||||
)
|
||||
|
||||
assert ha_entity.state == "off"
|
||||
service_call_counter = len(hmip_device.mock_calls)
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 1
|
||||
assert hmip_device.mock_calls[-1][0] == "set_rgb_dim_level"
|
||||
await async_manipulate_test_data(hass, hmip_device, "dimLevel", 100, 2)
|
||||
ha_entity = hass.states.get(entity_id)
|
||||
assert ha_entity.state == "on"
|
||||
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
assert len(hmip_device.mock_calls) == service_call_counter + 3
|
||||
assert hmip_device.mock_calls[-1][0] == "set_rgb_dim_level"
|
||||
await async_manipulate_test_data(hass, hmip_device, "dimLevel", 0, 2)
|
||||
ha_entity = hass.states.get(entity_id)
|
||||
assert ha_entity.state == "off"
|
||||
Reference in new issue
Block a user