mirror of
https://github.com/home-assistant/core.git
synced 2026-07-25 15:36:30 +01:00
7f4cc99a3e
* Shelly RPC sub-devices
* Better varaible name
* Add get_rpc_device_info helper
* Revert channel name changes
* Use get_rpc_device_info
* Add get_rpc_device_info helper
* Use get_block_device_info
* Use helpers in the button platform
* Fix channel name and roller mode for block devices
* Fix EM3 gen1
* Fix channel name for RPC devices
* Revert test changes
* Fix/improve test_block_get_block_channel_name
* Fix test_get_rpc_channel_name_multiple_components
* Fix tests
* Fix tests
* Fix tests
* Use key instead of index to generate sub-device identifier
* Improve logic for Pro RGBWW PM
* Split channels for em1
* Better channel name
* Cleaning
* has_entity_name is True
* Add get_block_sub_device_name() function
* Improve block functions
* Add get_rpc_sub_device_name() function
* Remove _attr_name
* Remove name for button with device class
* Fix names of virtual components
* Better Input name
* Fix get_rpc_channel_name()
* Fix names for Inputs
* get_rpc_channel_name() improvement
* Better variable name
* Clean RPC functions
* Fix input_name type
* Fix test
* Fix entity_ids for Blu Trv
* Fix get_block_channel_name()
* Fix for Blu Trv, once again
* Revert name for reboot button
* Fix button tests
* Fix tests
* Fix coordinator tests
* Fix tests for cover platform
* Fix tests for event platform
* Fix entity_ids in init tests
* Fix get_block_channel_name() for lights
* Fix tests for light platform
* Fix test for logbook
* Update snapshots for number platform
* Fix tests for sensor platform
* Fix tests for switch platform
* Fix tests for utils
* Uncomment
* Fix tests for flood
* Fix Valve entity name
* Fix climate tests
* Fix test for diagnostics
* Fix tests for init
* Remove old snapshots
* Add tests for 2PM Gen3
* Add comment
* More tests
* Cleaning
* Clean fixtures
* Update tests
* Anonymize coordinates in fixtures
* Split Pro 3EM entities into sub-devices
* Make sub-device names more unique
* 3EM (gen1) does not support sub-devices
* Coverage
* Rename "device temperature" sensor to the "relay temperature"
* Update tests after rebase
* Support sub-devices for 3EM (gen1)
* Mark has-entity-name rule as done 🎉
* Rename `relay temperature` to `temperature`
480 lines
14 KiB
Python
480 lines
14 KiB
Python
"""Test real devices."""
|
|
|
|
from unittest.mock import Mock
|
|
|
|
from aioshelly.const import MODEL_2PM_G3, MODEL_PRO_EM3
|
|
import pytest
|
|
|
|
from homeassistant.components.shelly.const import DOMAIN
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.device_registry import DeviceRegistry
|
|
from homeassistant.helpers.entity_registry import EntityRegistry
|
|
|
|
from . import init_integration
|
|
|
|
from tests.common import load_json_object_fixture
|
|
|
|
|
|
async def test_shelly_2pm_gen3_no_relay_names(
|
|
hass: HomeAssistant,
|
|
mock_rpc_device: Mock,
|
|
entity_registry: EntityRegistry,
|
|
device_registry: DeviceRegistry,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Test Shelly 2PM Gen3 without relay names.
|
|
|
|
This device has two relays/channels,we should get a main device and two sub
|
|
devices.
|
|
"""
|
|
device_fixture = load_json_object_fixture("2pm_gen3.json", DOMAIN)
|
|
monkeypatch.setattr(mock_rpc_device, "shelly", device_fixture["shelly"])
|
|
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
|
|
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
|
|
|
|
await init_integration(hass, gen=3, model=MODEL_2PM_G3)
|
|
|
|
# Relay 0 sub-device
|
|
entity_id = "switch.test_name_switch_0"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Switch 0"
|
|
|
|
entity_id = "sensor.test_name_switch_0_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Switch 0"
|
|
|
|
# Relay 1 sub-device
|
|
entity_id = "switch.test_name_switch_1"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Switch 1"
|
|
|
|
entity_id = "sensor.test_name_switch_1_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Switch 1"
|
|
|
|
# Main device
|
|
entity_id = "update.test_name_firmware"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
|
|
async def test_shelly_2pm_gen3_relay_names(
|
|
hass: HomeAssistant,
|
|
mock_rpc_device: Mock,
|
|
entity_registry: EntityRegistry,
|
|
device_registry: DeviceRegistry,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Test Shelly 2PM Gen3 with relay names.
|
|
|
|
This device has two relays/channels,we should get a main device and two sub
|
|
devices.
|
|
"""
|
|
device_fixture = load_json_object_fixture("2pm_gen3.json", DOMAIN)
|
|
device_fixture["config"]["switch:0"]["name"] = "Kitchen light"
|
|
device_fixture["config"]["switch:1"]["name"] = "Living room light"
|
|
monkeypatch.setattr(mock_rpc_device, "shelly", device_fixture["shelly"])
|
|
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
|
|
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
|
|
|
|
await init_integration(hass, gen=3, model=MODEL_2PM_G3)
|
|
|
|
# Relay 0 sub-device
|
|
entity_id = "switch.kitchen_light"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Kitchen light"
|
|
|
|
entity_id = "sensor.kitchen_light_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Kitchen light"
|
|
|
|
# Relay 1 sub-device
|
|
entity_id = "switch.living_room_light"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Living room light"
|
|
|
|
entity_id = "sensor.living_room_light_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Living room light"
|
|
|
|
# Main device
|
|
entity_id = "update.test_name_firmware"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
|
|
async def test_shelly_2pm_gen3_cover(
|
|
hass: HomeAssistant,
|
|
mock_rpc_device: Mock,
|
|
entity_registry: EntityRegistry,
|
|
device_registry: DeviceRegistry,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Test Shelly 2PM Gen3 with cover profile.
|
|
|
|
With the cover profile we should only get the main device and no subdevices.
|
|
"""
|
|
device_fixture = load_json_object_fixture("2pm_gen3_cover.json", DOMAIN)
|
|
monkeypatch.setattr(mock_rpc_device, "shelly", device_fixture["shelly"])
|
|
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
|
|
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
|
|
|
|
await init_integration(hass, gen=3, model=MODEL_2PM_G3)
|
|
|
|
entity_id = "cover.test_name"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
entity_id = "sensor.test_name_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
entity_id = "update.test_name_firmware"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
|
|
async def test_shelly_2pm_gen3_cover_with_name(
|
|
hass: HomeAssistant,
|
|
mock_rpc_device: Mock,
|
|
entity_registry: EntityRegistry,
|
|
device_registry: DeviceRegistry,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Test Shelly 2PM Gen3 with cover profile and the cover name.
|
|
|
|
With the cover profile we should only get the main device and no subdevices.
|
|
"""
|
|
device_fixture = load_json_object_fixture("2pm_gen3_cover.json", DOMAIN)
|
|
device_fixture["config"]["cover:0"]["name"] = "Bedroom blinds"
|
|
monkeypatch.setattr(mock_rpc_device, "shelly", device_fixture["shelly"])
|
|
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
|
|
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
|
|
|
|
await init_integration(hass, gen=3, model=MODEL_2PM_G3)
|
|
|
|
entity_id = "cover.test_name_bedroom_blinds"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
entity_id = "sensor.test_name_bedroom_blinds_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
entity_id = "update.test_name_firmware"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
|
|
async def test_shelly_pro_3em(
|
|
hass: HomeAssistant,
|
|
mock_rpc_device: Mock,
|
|
entity_registry: EntityRegistry,
|
|
device_registry: DeviceRegistry,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Test Shelly Pro 3EM.
|
|
|
|
We should get the main device and three subdevices, one subdevice per one phase.
|
|
"""
|
|
device_fixture = load_json_object_fixture("pro_3em.json", DOMAIN)
|
|
monkeypatch.setattr(mock_rpc_device, "shelly", device_fixture["shelly"])
|
|
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
|
|
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
|
|
|
|
await init_integration(hass, gen=2, model=MODEL_PRO_EM3)
|
|
|
|
# Main device
|
|
entity_id = "sensor.test_name_total_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
# Phase A sub-device
|
|
entity_id = "sensor.test_name_phase_a_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Phase A"
|
|
|
|
# Phase B sub-device
|
|
entity_id = "sensor.test_name_phase_b_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Phase B"
|
|
|
|
# Phase C sub-device
|
|
entity_id = "sensor.test_name_phase_c_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Phase C"
|
|
|
|
|
|
async def test_shelly_pro_3em_with_emeter_name(
|
|
hass: HomeAssistant,
|
|
mock_rpc_device: Mock,
|
|
entity_registry: EntityRegistry,
|
|
device_registry: DeviceRegistry,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Test Shelly Pro 3EM when the name for Emeter is set.
|
|
|
|
We should get the main device and three subdevices, one subdevice per one phase.
|
|
"""
|
|
device_fixture = load_json_object_fixture("pro_3em.json", DOMAIN)
|
|
device_fixture["config"]["em:0"]["name"] = "Emeter name"
|
|
monkeypatch.setattr(mock_rpc_device, "shelly", device_fixture["shelly"])
|
|
monkeypatch.setattr(mock_rpc_device, "status", device_fixture["status"])
|
|
monkeypatch.setattr(mock_rpc_device, "config", device_fixture["config"])
|
|
|
|
await init_integration(hass, gen=2, model=MODEL_PRO_EM3)
|
|
|
|
# Main device
|
|
entity_id = "sensor.test_name_total_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|
|
|
|
# Phase A sub-device
|
|
entity_id = "sensor.test_name_phase_a_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Phase A"
|
|
|
|
# Phase B sub-device
|
|
entity_id = "sensor.test_name_phase_b_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Phase B"
|
|
|
|
# Phase C sub-device
|
|
entity_id = "sensor.test_name_phase_c_active_power"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name Phase C"
|
|
|
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
|
async def test_block_channel_with_name(
|
|
hass: HomeAssistant,
|
|
mock_block_device: Mock,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
entity_registry: EntityRegistry,
|
|
device_registry: DeviceRegistry,
|
|
) -> None:
|
|
"""Test block channel with name."""
|
|
monkeypatch.setitem(
|
|
mock_block_device.settings["relays"][0], "name", "Kitchen light"
|
|
)
|
|
|
|
await init_integration(hass, 1)
|
|
|
|
# channel 1 sub-device; num_outputs is 2 so the name of the channel should be used
|
|
entity_id = "switch.kitchen_light"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Kitchen light"
|
|
|
|
# main device
|
|
entity_id = "update.test_name_firmware"
|
|
|
|
state = hass.states.get(entity_id)
|
|
assert state
|
|
|
|
entry = entity_registry.async_get(entity_id)
|
|
assert entry
|
|
|
|
device_entry = device_registry.async_get(entry.device_id)
|
|
assert device_entry
|
|
assert device_entry.name == "Test name"
|