Remove deprecated proximity entity (#123158)

pull/123190/head
Michael 2024-08-05 15:44:00 +02:00 committed by GitHub
parent 42ab8d0445
commit b223931ac0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 119 additions and 655 deletions

View File

@ -3,137 +3,20 @@
from __future__ import annotations
import logging
from typing import cast
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
CONF_DEVICES,
CONF_NAME,
CONF_UNIT_OF_MEASUREMENT,
CONF_ZONE,
STATE_UNKNOWN,
Platform,
)
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.event import (
async_track_entity_registry_updated_event,
async_track_state_change_event,
)
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
ATTR_DIR_OF_TRAVEL,
ATTR_DIST_TO,
ATTR_NEAREST,
CONF_IGNORED_ZONES,
CONF_TOLERANCE,
CONF_TRACKED_ENTITIES,
DEFAULT_PROXIMITY_ZONE,
DEFAULT_TOLERANCE,
DOMAIN,
UNITS,
)
from .const import CONF_TRACKED_ENTITIES
from .coordinator import ProximityConfigEntry, ProximityDataUpdateCoordinator
from .helpers import entity_used_in
_LOGGER = logging.getLogger(__name__)
ZONE_SCHEMA = vol.Schema(
{
vol.Optional(CONF_ZONE, default=DEFAULT_PROXIMITY_ZONE): cv.string,
vol.Optional(CONF_DEVICES, default=[]): vol.All(cv.ensure_list, [cv.entity_id]),
vol.Optional(CONF_IGNORED_ZONES, default=[]): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(CONF_TOLERANCE, default=DEFAULT_TOLERANCE): cv.positive_int,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): vol.All(cv.string, vol.In(UNITS)),
}
)
CONFIG_SCHEMA = vol.Schema(
vol.All(
cv.deprecated(DOMAIN),
{DOMAIN: cv.schema_with_slug_keys(ZONE_SCHEMA)},
),
extra=vol.ALLOW_EXTRA,
)
async def _async_setup_legacy(
hass: HomeAssistant,
entry: ProximityConfigEntry,
coordinator: ProximityDataUpdateCoordinator,
) -> None:
"""Legacy proximity entity handling, can be removed in 2024.8."""
friendly_name = entry.data[CONF_NAME]
proximity = Proximity(hass, friendly_name, coordinator)
await proximity.async_added_to_hass()
proximity.async_write_ha_state()
if used_in := entity_used_in(hass, f"{DOMAIN}.{friendly_name}"):
async_create_issue(
hass,
DOMAIN,
f"deprecated_proximity_entity_{friendly_name}",
breaks_in_ha_version="2024.8.0",
is_fixable=True,
is_persistent=True,
severity=IssueSeverity.WARNING,
translation_key="deprecated_proximity_entity",
translation_placeholders={
"entity": f"{DOMAIN}.{friendly_name}",
"used_in": "\n- ".join([f"`{x}`" for x in used_in]),
},
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Get the zones and offsets from configuration.yaml."""
if DOMAIN in config:
for friendly_name, proximity_config in config[DOMAIN].items():
_LOGGER.debug("import %s with config:%s", friendly_name, proximity_config)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={
CONF_NAME: friendly_name,
CONF_ZONE: f"zone.{proximity_config[CONF_ZONE]}",
CONF_TRACKED_ENTITIES: proximity_config[CONF_DEVICES],
CONF_IGNORED_ZONES: [
f"zone.{zone}"
for zone in proximity_config[CONF_IGNORED_ZONES]
],
CONF_TOLERANCE: proximity_config[CONF_TOLERANCE],
CONF_UNIT_OF_MEASUREMENT: proximity_config.get(
CONF_UNIT_OF_MEASUREMENT, hass.config.units.length_unit
),
},
)
)
async_create_issue(
hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{DOMAIN}",
breaks_in_ha_version="2024.8.0",
is_fixable=False,
issue_domain=DOMAIN,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": DOMAIN,
"integration_title": "Proximity",
},
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ProximityConfigEntry) -> bool:
"""Set up Proximity from a config entry."""
@ -160,9 +43,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ProximityConfigEntry) ->
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator
if entry.source == SOURCE_IMPORT:
await _async_setup_legacy(hass, entry, coordinator)
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
return True
@ -176,45 +56,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)
class Proximity(CoordinatorEntity[ProximityDataUpdateCoordinator]):
"""Representation of a Proximity."""
# This entity is legacy and does not have a platform.
# We can't fix this easily without breaking changes.
_no_platform_reported = True
def __init__(
self,
hass: HomeAssistant,
friendly_name: str,
coordinator: ProximityDataUpdateCoordinator,
) -> None:
"""Initialize the proximity."""
super().__init__(coordinator)
self.hass = hass
self.entity_id = f"{DOMAIN}.{friendly_name}"
self._attr_name = friendly_name
self._attr_unit_of_measurement = self.coordinator.unit_of_measurement
@property
def data(self) -> dict[str, str | int | None]:
"""Get data from coordinator."""
return self.coordinator.data.proximity
@property
def state(self) -> str | float:
"""Return the state."""
if isinstance(distance := self.data[ATTR_DIST_TO], str):
return distance
return self.coordinator.convert_legacy(cast(int, distance))
@property
def extra_state_attributes(self) -> dict[str, str]:
"""Return the state attributes."""
return {
ATTR_DIR_OF_TRAVEL: str(self.data[ATTR_DIR_OF_TRAVEL] or STATE_UNKNOWN),
ATTR_NEAREST: str(self.data[ATTR_NEAREST]),
}

View File

@ -1,12 +0,0 @@
"""Helper functions for proximity."""
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.script import scripts_with_entity
from homeassistant.core import HomeAssistant
def entity_used_in(hass: HomeAssistant, entity_id: str) -> list[str]:
"""Get list of related automations and scripts."""
used_in = automations_with_entity(hass, entity_id)
used_in += scripts_with_entity(hass, entity_id)
return used_in

View File

@ -55,17 +55,6 @@
}
},
"issues": {
"deprecated_proximity_entity": {
"title": "The proximity entity is deprecated",
"fix_flow": {
"step": {
"confirm": {
"title": "[%key:component::proximity::issues::deprecated_proximity_entity::title%]",
"description": "The proximity entity `{entity}` is deprecated and will be removed in `2024.8`. However it is used within the following configurations:\n- {used_in}\n\nPlease adjust any automations or scripts that use this deprecated Proximity entity.\nFor each tracked person or device one sensor for the distance and the direction of travel to/from the monitored zone is created. Additionally for each Proximity configuration one sensor which shows the nearest device or person to the monitored zone is created. With this you can use the Min/Max integration to determine the nearest and furthest distance."
}
}
}
},
"tracked_entity_removed": {
"title": "Tracked entity has been removed",
"fix_flow": {

View File

@ -2,15 +2,12 @@
import pytest
from homeassistant.components import automation, script
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.proximity.const import (
CONF_IGNORED_ZONES,
CONF_TOLERANCE,
CONF_TRACKED_ENTITIES,
DOMAIN,
)
from homeassistant.components.script import scripts_with_entity
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
CONF_ZONE,
@ -20,109 +17,81 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.helpers.issue_registry as ir
from homeassistant.setup import async_setup_component
from homeassistant.util import slugify
from tests.common import MockConfigEntry
async def async_setup_single_entry(
hass: HomeAssistant,
zone: str,
tracked_entites: list[str],
ignored_zones: list[str],
tolerance: int,
) -> MockConfigEntry:
"""Set up the proximity component with a single entry."""
mock_config = MockConfigEntry(
domain=DOMAIN,
title="Home",
data={
CONF_ZONE: zone,
CONF_TRACKED_ENTITIES: tracked_entites,
CONF_IGNORED_ZONES: ignored_zones,
CONF_TOLERANCE: tolerance,
},
)
mock_config.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
return mock_config
@pytest.mark.parametrize(
("friendly_name", "config"),
"config",
[
(
"home",
{
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"tolerance": "1",
},
),
(
"work",
{
"devices": ["device_tracker.test1"],
"tolerance": "1",
"zone": "work",
},
),
{
CONF_IGNORED_ZONES: ["zone.work"],
CONF_TRACKED_ENTITIES: ["device_tracker.test1", "device_tracker.test2"],
CONF_TOLERANCE: 1,
CONF_ZONE: "zone.home",
},
{
CONF_IGNORED_ZONES: [],
CONF_TRACKED_ENTITIES: ["device_tracker.test1"],
CONF_TOLERANCE: 1,
CONF_ZONE: "zone.work",
},
],
)
async def test_proximities(
hass: HomeAssistant, friendly_name: str, config: dict
) -> None:
async def test_proximities(hass: HomeAssistant, config: dict) -> None:
"""Test a list of proximities."""
assert await async_setup_component(
hass, DOMAIN, {"proximity": {friendly_name: config}}
title = hass.states.get(config[CONF_ZONE]).name
mock_config = MockConfigEntry(
domain=DOMAIN,
title=title,
data=config,
)
mock_config.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get(f"proximity.{friendly_name}")
assert state.state == "not set"
assert state.attributes.get("nearest") == "not set"
assert state.attributes.get("dir_of_travel") == "not set"
hass.states.async_set(f"proximity.{friendly_name}", "0")
await hass.async_block_till_done()
state = hass.states.get(f"proximity.{friendly_name}")
assert state.state == "0"
zone_name = slugify(title)
# sensor entities
state = hass.states.get(f"sensor.{friendly_name}_nearest_device")
state = hass.states.get(f"sensor.{zone_name}_nearest_device")
assert state.state == STATE_UNKNOWN
for device in config["devices"]:
entity_base_name = f"sensor.{friendly_name}_{slugify(device.split('.')[-1])}"
for device in config[CONF_TRACKED_ENTITIES]:
entity_base_name = f"sensor.{zone_name}_{slugify(device.split('.')[-1])}"
state = hass.states.get(f"{entity_base_name}_distance")
assert state.state == STATE_UNAVAILABLE
state = hass.states.get(f"{entity_base_name}_direction_of_travel")
assert state.state == STATE_UNAVAILABLE
async def test_legacy_setup(hass: HomeAssistant) -> None:
"""Test legacy setup only on imported entries."""
config = {
"proximity": {
"home": {
"devices": ["device_tracker.test1"],
"tolerance": "1",
},
}
}
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
assert hass.states.get("proximity.home")
mock_config = MockConfigEntry(
domain=DOMAIN,
title="work",
data={
CONF_ZONE: "zone.work",
CONF_TRACKED_ENTITIES: ["device_tracker.test2"],
CONF_IGNORED_ZONES: [],
CONF_TOLERANCE: 1,
},
unique_id=f"{DOMAIN}_work",
)
mock_config.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
assert not hass.states.get("proximity.work")
async def test_device_tracker_test1_in_zone(hass: HomeAssistant) -> None:
"""Test for tracker in zone."""
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1"],
"tolerance": "1",
}
}
}
assert await async_setup_component(hass, DOMAIN, config)
await async_setup_single_entry(hass, "zone.home", ["device_tracker.test1"], [], 1)
hass.states.async_set(
"device_tracker.test1",
@ -131,12 +100,6 @@ async def test_device_tracker_test1_in_zone(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.state == "0"
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "arrived"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -150,17 +113,7 @@ async def test_device_tracker_test1_in_zone(hass: HomeAssistant) -> None:
async def test_device_tracker_test1_away(hass: HomeAssistant) -> None:
"""Test for tracker state away."""
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1"],
"tolerance": "1",
}
}
}
assert await async_setup_component(hass, DOMAIN, config)
await async_setup_single_entry(hass, "zone.home", ["device_tracker.test1"], [], 1)
hass.states.async_set(
"device_tracker.test1",
@ -170,11 +123,6 @@ async def test_device_tracker_test1_away(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -190,20 +138,7 @@ async def test_device_tracker_test1_awayfurther(
hass: HomeAssistant, config_zones
) -> None:
"""Test for tracker state away further."""
await hass.async_block_till_done()
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1"],
"tolerance": "1",
}
}
}
assert await async_setup_component(hass, DOMAIN, config)
await async_setup_single_entry(hass, "zone.home", ["device_tracker.test1"], [], 1)
hass.states.async_set(
"device_tracker.test1",
@ -212,11 +147,6 @@ async def test_device_tracker_test1_awayfurther(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -234,11 +164,6 @@ async def test_device_tracker_test1_awayfurther(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "away_from"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -254,19 +179,7 @@ async def test_device_tracker_test1_awaycloser(
hass: HomeAssistant, config_zones
) -> None:
"""Test for tracker state away closer."""
await hass.async_block_till_done()
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1"],
"tolerance": "1",
}
}
}
assert await async_setup_component(hass, DOMAIN, config)
await async_setup_single_entry(hass, "zone.home", ["device_tracker.test1"], [], 1)
hass.states.async_set(
"device_tracker.test1",
@ -275,11 +188,6 @@ async def test_device_tracker_test1_awaycloser(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -297,11 +205,6 @@ async def test_device_tracker_test1_awaycloser(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "towards"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -315,27 +218,11 @@ async def test_device_tracker_test1_awaycloser(
async def test_all_device_trackers_in_ignored_zone(hass: HomeAssistant) -> None:
"""Test for tracker in ignored zone."""
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1"],
"tolerance": "1",
}
}
}
assert await async_setup_component(hass, DOMAIN, config)
await async_setup_single_entry(hass, "zone.home", ["device_tracker.test1"], [], 1)
hass.states.async_set("device_tracker.test1", "work", {"friendly_name": "test1"})
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.state == "not set"
assert state.attributes.get("nearest") == "not set"
assert state.attributes.get("dir_of_travel") == "not set"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == STATE_UNKNOWN
@ -349,28 +236,13 @@ async def test_all_device_trackers_in_ignored_zone(hass: HomeAssistant) -> None:
async def test_device_tracker_test1_no_coordinates(hass: HomeAssistant) -> None:
"""Test for tracker with no coordinates."""
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1"],
"tolerance": "1",
}
}
}
assert await async_setup_component(hass, DOMAIN, config)
await async_setup_single_entry(hass, "zone.home", ["device_tracker.test1"], [], 1)
hass.states.async_set(
"device_tracker.test1", "not_home", {"friendly_name": "test1"}
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "not set"
assert state.attributes.get("dir_of_travel") == "not set"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == STATE_UNKNOWN
@ -384,19 +256,8 @@ async def test_device_tracker_test1_no_coordinates(hass: HomeAssistant) -> None:
async def test_device_tracker_test1_awayfurther_a_bit(hass: HomeAssistant) -> None:
"""Test for tracker states."""
assert await async_setup_component(
hass,
DOMAIN,
{
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1"],
"tolerance": 1000,
"zone": "home",
}
}
},
await async_setup_single_entry(
hass, "zone.home", ["device_tracker.test1"], ["zone.work"], 1000
)
hass.states.async_set(
@ -406,11 +267,6 @@ async def test_device_tracker_test1_awayfurther_a_bit(hass: HomeAssistant) -> No
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -428,11 +284,6 @@ async def test_device_tracker_test1_awayfurther_a_bit(hass: HomeAssistant) -> No
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "stationary"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -446,17 +297,13 @@ async def test_device_tracker_test1_awayfurther_a_bit(hass: HomeAssistant) -> No
async def test_device_trackers_in_zone(hass: HomeAssistant) -> None:
"""Test for trackers in zone."""
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"tolerance": "1",
}
}
}
assert await async_setup_component(hass, DOMAIN, config)
await async_setup_single_entry(
hass,
"zone.home",
["device_tracker.test1", "device_tracker.test2"],
["zone.work"],
1,
)
hass.states.async_set(
"device_tracker.test1",
@ -471,14 +318,6 @@ async def test_device_trackers_in_zone(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.state == "0"
assert (state.attributes.get("nearest") == "test1, test2") or (
state.attributes.get("nearest") == "test2, test1"
)
assert state.attributes.get("dir_of_travel") == "arrived"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1, test2"
@ -495,30 +334,18 @@ async def test_device_tracker_test1_awayfurther_than_test2_first_test1(
hass: HomeAssistant, config_zones
) -> None:
"""Test for tracker ordering."""
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test1", "not_home", {"friendly_name": "test1"}
)
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test2", "not_home", {"friendly_name": "test2"}
)
await hass.async_block_till_done()
assert await async_setup_component(
await async_setup_single_entry(
hass,
DOMAIN,
{
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"tolerance": "1",
"zone": "home",
}
}
},
"zone.home",
["device_tracker.test1", "device_tracker.test2"],
["zone.work"],
1,
)
hass.states.async_set(
@ -528,11 +355,6 @@ async def test_device_tracker_test1_awayfurther_than_test2_first_test1(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -556,11 +378,6 @@ async def test_device_tracker_test1_awayfurther_than_test2_first_test1(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -582,28 +399,19 @@ async def test_device_tracker_test1_awayfurther_than_test2_first_test2(
hass: HomeAssistant, config_zones
) -> None:
"""Test for tracker ordering."""
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test1", "not_home", {"friendly_name": "test1"}
)
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test2", "not_home", {"friendly_name": "test2"}
)
await hass.async_block_till_done()
assert await async_setup_component(
await async_setup_single_entry(
hass,
DOMAIN,
{
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"zone": "home",
}
}
},
"zone.home",
["device_tracker.test1", "device_tracker.test2"],
["zone.work"],
1,
)
hass.states.async_set(
@ -613,11 +421,6 @@ async def test_device_tracker_test1_awayfurther_than_test2_first_test2(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test2"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test2"
@ -641,11 +444,6 @@ async def test_device_tracker_test1_awayfurther_than_test2_first_test2(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -670,23 +468,15 @@ async def test_device_tracker_test1_awayfurther_test2_in_ignored_zone(
hass.states.async_set(
"device_tracker.test1", "not_home", {"friendly_name": "test1"}
)
await hass.async_block_till_done()
hass.states.async_set("device_tracker.test2", "work", {"friendly_name": "test2"})
await hass.async_block_till_done()
assert await async_setup_component(
hass,
DOMAIN,
{
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"zone": "home",
}
}
},
)
await async_setup_single_entry(
hass,
"zone.home",
["device_tracker.test1", "device_tracker.test2"],
["zone.work"],
1,
)
hass.states.async_set(
"device_tracker.test1",
"not_home",
@ -694,11 +484,6 @@ async def test_device_tracker_test1_awayfurther_test2_in_ignored_zone(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -720,29 +505,19 @@ async def test_device_tracker_test1_awayfurther_test2_first(
hass: HomeAssistant, config_zones
) -> None:
"""Test for tracker state."""
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test1", "not_home", {"friendly_name": "test1"}
)
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test2", "not_home", {"friendly_name": "test2"}
)
await hass.async_block_till_done()
assert await async_setup_component(
await async_setup_single_entry(
hass,
DOMAIN,
{
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"zone": "home",
}
}
},
"zone.home",
["device_tracker.test1", "device_tracker.test2"],
["zone.work"],
1,
)
hass.states.async_set(
@ -776,11 +551,6 @@ async def test_device_tracker_test1_awayfurther_test2_first(
hass.states.async_set("device_tracker.test1", "work", {"friendly_name": "test1"})
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test2"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test2"
@ -803,7 +573,6 @@ async def test_device_tracker_test1_nearest_after_test2_in_ignored_zone(
) -> None:
"""Test for tracker states."""
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test1", "not_home", {"friendly_name": "test1"}
)
@ -813,20 +582,28 @@ async def test_device_tracker_test1_nearest_after_test2_in_ignored_zone(
)
await hass.async_block_till_done()
assert await async_setup_component(
await async_setup_single_entry(
hass,
DOMAIN,
{
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"zone": "home",
}
}
},
"zone.home",
["device_tracker.test1", "device_tracker.test2"],
["zone.work"],
1,
)
# assert await async_setup_component(
# hass,
# DOMAIN,
# {
# "proximity": {
# "home": {
# "ignored_zones": ["zone.work"],
# "devices": ["device_tracker.test1", "device_tracker.test2"],
# "zone": "home",
# }
# }
# },
# )
hass.states.async_set(
"device_tracker.test1",
"not_home",
@ -834,11 +611,6 @@ async def test_device_tracker_test1_nearest_after_test2_in_ignored_zone(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -862,11 +634,6 @@ async def test_device_tracker_test1_nearest_after_test2_in_ignored_zone(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test2"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test2"
@ -890,11 +657,6 @@ async def test_device_tracker_test1_nearest_after_test2_in_ignored_zone(
)
await hass.async_block_till_done()
# proximity entity
state = hass.states.get("proximity.home")
assert state.attributes.get("nearest") == "test1"
assert state.attributes.get("dir_of_travel") == "unknown"
# sensor entities
state = hass.states.get("sensor.home_nearest_device")
assert state.state == "test1"
@ -914,22 +676,10 @@ async def test_device_tracker_test1_nearest_after_test2_in_ignored_zone(
async def test_nearest_sensors(hass: HomeAssistant, config_zones) -> None:
"""Test for nearest sensors."""
mock_config = MockConfigEntry(
domain=DOMAIN,
title="home",
data={
CONF_ZONE: "zone.home",
CONF_TRACKED_ENTITIES: ["device_tracker.test1", "device_tracker.test2"],
CONF_IGNORED_ZONES: [],
CONF_TOLERANCE: 1,
},
unique_id=f"{DOMAIN}_home",
await async_setup_single_entry(
hass, "zone.home", ["device_tracker.test1", "device_tracker.test2"], [], 1
)
mock_config.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.test1",
"not_home",
@ -1038,71 +788,6 @@ async def test_nearest_sensors(hass: HomeAssistant, config_zones) -> None:
assert state.state == STATE_UNKNOWN
async def test_create_deprecated_proximity_issue(
hass: HomeAssistant,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test we create an issue for deprecated proximity entities used in automations and scripts."""
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"alias": "test",
"trigger": {"platform": "state", "entity_id": "proximity.home"},
"action": {
"service": "automation.turn_on",
"target": {"entity_id": "automation.test"},
},
}
},
)
assert await async_setup_component(
hass,
script.DOMAIN,
{
script.DOMAIN: {
"test": {
"sequence": [
{
"condition": "state",
"entity_id": "proximity.home",
"state": "home",
},
],
}
}
},
)
config = {
"proximity": {
"home": {
"ignored_zones": ["work"],
"devices": ["device_tracker.test1", "device_tracker.test2"],
"tolerance": "1",
},
"work": {"tolerance": "1", "zone": "work"},
}
}
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
automation_entities = automations_with_entity(hass, "proximity.home")
assert len(automation_entities) == 1
assert automation_entities[0] == "automation.test"
script_entites = scripts_with_entity(hass, "proximity.home")
assert len(script_entites) == 1
assert script_entites[0] == "script.test"
assert issue_registry.async_get_issue(DOMAIN, "deprecated_proximity_entity_home")
assert not issue_registry.async_get_issue(
DOMAIN, "deprecated_proximity_entity_work"
)
async def test_create_removed_tracked_entity_issue(
hass: HomeAssistant,
issue_registry: ir.IssueRegistry,
@ -1119,22 +804,10 @@ async def test_create_removed_tracked_entity_issue(
hass.states.async_set(t1.entity_id, "not_home")
hass.states.async_set(t2.entity_id, "not_home")
mock_config = MockConfigEntry(
domain=DOMAIN,
title="home",
data={
CONF_ZONE: "zone.home",
CONF_TRACKED_ENTITIES: [t1.entity_id, t2.entity_id],
CONF_IGNORED_ZONES: [],
CONF_TOLERANCE: 1,
},
unique_id=f"{DOMAIN}_home",
await async_setup_single_entry(
hass, "zone.home", [t1.entity_id, t2.entity_id], [], 1
)
mock_config.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
sensor_t1 = f"sensor.home_{t1.entity_id.split('.')[-1]}_distance"
sensor_t2 = f"sensor.home_{t2.entity_id.split('.')[-1]}_distance"
@ -1168,22 +841,10 @@ async def test_track_renamed_tracked_entity(
hass.states.async_set(t1.entity_id, "not_home")
mock_config = MockConfigEntry(
domain=DOMAIN,
title="home",
data={
CONF_ZONE: "zone.home",
CONF_TRACKED_ENTITIES: [t1.entity_id],
CONF_IGNORED_ZONES: [],
CONF_TOLERANCE: 1,
},
unique_id=f"{DOMAIN}_home",
mock_config = await async_setup_single_entry(
hass, "zone.home", [t1.entity_id], ["zone.work"], 1
)
mock_config.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
sensor_t1 = f"sensor.home_{t1.entity_id.split('.')[-1]}_distance"
entity = entity_registry.async_get(sensor_t1)
@ -1216,28 +877,16 @@ async def test_sensor_unique_ids(
hass.states.async_set("device_tracker.test2", "not_home")
mock_config = MockConfigEntry(
domain=DOMAIN,
title="home",
data={
CONF_ZONE: "zone.home",
CONF_TRACKED_ENTITIES: [t1.entity_id, "device_tracker.test2"],
CONF_IGNORED_ZONES: [],
CONF_TOLERANCE: 1,
},
unique_id=f"{DOMAIN}_home",
mock_config = await async_setup_single_entry(
hass, "zone.home", [t1.entity_id, "device_tracker.test2"], ["zone.work"], 1
)
mock_config.add_to_hass(hass)
assert await hass.config_entries.async_setup(mock_config.entry_id)
await hass.async_block_till_done()
sensor_t1 = "sensor.home_test_tracker_1_distance"
entity = entity_registry.async_get(sensor_t1)
assert entity
assert entity.unique_id == f"{mock_config.entry_id}_{t1.id}_dist_to_zone"
state = hass.states.get(sensor_t1)
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "home Test tracker 1 Distance"
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Home Test tracker 1 Distance"
entity = entity_registry.async_get("sensor.home_test2_distance")
assert entity