Entity Component to no longer generate automatic groups (#23789)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-01-07 17:30:53 +01:00
1 parent 13e6479b6e
commit 9aa02e35a7
41 files changed
+149 -377

No files matched your search

@@ -125,9 +125,7 @@ def is_on(hass, entity_id):
async def async_setup(hass, config):
"""Set up the automation."""
component = EntityComponent(
_LOGGER, DOMAIN, hass, group_name=GROUP_NAME_ALL_AUTOMATIONS
)
component = EntityComponent(_LOGGER, DOMAIN, hass)
await _async_process_config(hass, config, component)
@@ -29,7 +29,7 @@ SCAN_INTERVAL = timedelta(seconds=60)
async def async_setup(hass, config):
"""Track states and offer events for calendars."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, DOMAIN
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
)
hass.http.register_view(CalendarListView(component))
+2 -7
View File
@@ -6,7 +6,6 @@ from typing import Any
import voluptuous as vol
from homeassistant.components import group
from homeassistant.const import (
SERVICE_CLOSE_COVER,
SERVICE_CLOSE_COVER_TILT,
@@ -38,9 +37,6 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = "cover"
SCAN_INTERVAL = timedelta(seconds=15)
GROUP_NAME_ALL_COVERS = "all covers"
ENTITY_ID_ALL_COVERS = group.ENTITY_ID_FORMAT.format("all_covers")
ENTITY_ID_FORMAT = DOMAIN + ".{}"
# Refer to the cover dev docs for device class descriptions
@@ -82,16 +78,15 @@ ATTR_TILT_POSITION = "tilt_position"
@bind_hass
def is_closed(hass, entity_id=None):
def is_closed(hass, entity_id):
"""Return if the cover is closed based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_COVERS
return hass.states.is_state(entity_id, STATE_CLOSED)
async def async_setup(hass, config):
"""Track states and offer events for covers."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_COVERS
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
)
await component.async_setup(config)
@@ -66,23 +66,41 @@ async def async_setup(hass, config):
person = hass.components.person
conf = config[DOMAIN]
disable_turn_off = conf.get(CONF_DISABLE_TURN_OFF)
light_group = conf.get(CONF_LIGHT_GROUP, light.ENTITY_ID_ALL_LIGHTS)
light_group = conf.get(CONF_LIGHT_GROUP)
light_profile = conf.get(CONF_LIGHT_PROFILE)
device_group = conf.get(CONF_DEVICE_GROUP, device_tracker.ENTITY_ID_ALL_DEVICES)
device_entity_ids = group.get_entity_ids(device_group, device_tracker.DOMAIN)
device_entity_ids.extend(group.get_entity_ids(device_group, person.DOMAIN))
device_group = conf.get(CONF_DEVICE_GROUP)
if device_group is None:
device_entity_ids = hass.states.async_entity_ids(device_tracker.DOMAIN)
else:
device_entity_ids = group.get_entity_ids(device_group, device_tracker.DOMAIN)
device_entity_ids.extend(group.get_entity_ids(device_group, person.DOMAIN))
if not device_entity_ids:
logger.error("No devices found to track")
return False
# Get the light IDs from the specified group
light_ids = group.get_entity_ids(light_group, light.DOMAIN)
if light_group is None:
light_ids = hass.states.async_entity_ids(light.DOMAIN)
else:
light_ids = group.get_entity_ids(light_group, light.DOMAIN)
if not light_ids:
logger.error("No lights found to turn on")
return False
@callback
def anyone_home():
"""Test if anyone is home."""
return any(device_tracker.is_on(dt_id) for dt_id in device_entity_ids)
@callback
def any_light_on():
"""Test if any light on."""
return any(light.is_on(light_id) for light_id in light_ids)
def calc_time_for_light_when_sunset():
"""Calculate the time when to start fading lights in when sun sets.
@@ -97,7 +115,7 @@ async def async_setup(hass, config):
def async_turn_on_before_sunset(light_id):
"""Turn on lights."""
if not device_tracker.is_on() or light.is_on(light_id):
if not anyone_home() or light.is_on(light_id):
return
hass.async_create_task(
hass.services.async_call(
@@ -153,7 +171,7 @@ async def async_setup(hass, config):
@callback
def check_light_on_dev_state_change(entity, old_state, new_state):
"""Handle tracked device state changes."""
lights_are_on = group.is_on(light_group)
lights_are_on = any_light_on()
light_needed = not (lights_are_on or is_up(hass))
# These variables are needed for the elif check
@@ -208,7 +226,12 @@ async def async_setup(hass, config):
@callback
def turn_off_lights_when_all_leave(entity, old_state, new_state):
"""Handle device group state change."""
if not group.is_on(light_group):
# Make sure there is not someone home
if anyone_home():
return
# Check if any light is on
if not any_light_on():
return
logger.info("Everyone has left but there are lights on. Turning them off")
@@ -219,7 +242,11 @@ async def async_setup(hass, config):
)
async_track_state_change(
hass, device_group, turn_off_lights_when_all_leave, STATE_HOME, STATE_NOT_HOME
hass,
device_entity_ids,
turn_off_lights_when_all_leave,
STATE_HOME,
STATE_NOT_HOME,
)
return True
@@ -3,7 +3,6 @@ import asyncio
import voluptuous as vol
from homeassistant.components import group
from homeassistant.const import ATTR_GPS_ACCURACY, STATE_HOME
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
@@ -43,8 +42,6 @@ from .const import (
)
from .legacy import DeviceScanner # noqa: F401 pylint: disable=unused-import
ENTITY_ID_ALL_DEVICES = group.ENTITY_ID_FORMAT.format("all_devices")
SERVICE_SEE = "see"
SOURCE_TYPES = (
@@ -97,11 +94,9 @@ SERVICE_SEE_PAYLOAD_SCHEMA = vol.Schema(
@bind_hass
def is_on(hass: HomeAssistantType, entity_id: str = None):
def is_on(hass: HomeAssistantType, entity_id: str):
"""Return the state if any or a specified device is home."""
entity = entity_id or ENTITY_ID_ALL_DEVICES
return hass.states.is_state(entity, STATE_HOME)
return hass.states.is_state(entity_id, STATE_HOME)
def see(
@@ -148,8 +143,6 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
if setup_tasks:
await asyncio.wait(setup_tasks)
tracker.async_setup_group()
async def async_platform_discovered(p_type, info):
"""Load a platform."""
platform = await setup.async_create_platform_type(hass, config, p_type, {})
@@ -8,15 +8,6 @@ import voluptuous as vol
from homeassistant import util
from homeassistant.components import zone
from homeassistant.components.group import (
ATTR_ADD_ENTITIES,
ATTR_ENTITIES,
ATTR_OBJECT_ID,
ATTR_VISIBLE,
DOMAIN as DOMAIN_GROUP,
SERVICE_SET,
)
from homeassistant.components.zone import async_active_zone
from homeassistant.config import async_log_exception, load_yaml_config_file
from homeassistant.const import (
ATTR_ENTITY_ID,
@@ -60,7 +51,6 @@ from .const import (
)
YAML_DEVICES = "known_devices.yaml"
GROUP_NAME_ALL_DEVICES = "all devices"
EVENT_NEW_DEVICE = "device_tracker_new_device"
@@ -104,7 +94,6 @@ class DeviceTracker:
else defaults.get(CONF_TRACK_NEW, DEFAULT_TRACK_NEW)
)
self.defaults = defaults
self.group = None
self._is_updating = asyncio.Lock()
for dev in devices:
@@ -230,21 +219,6 @@ class DeviceTracker:
if device.track:
await device.async_update_ha_state()
# During init, we ignore the group
if self.group and self.track_new:
self.hass.async_create_task(
self.hass.async_call(
DOMAIN_GROUP,
SERVICE_SET,
{
ATTR_OBJECT_ID: util.slugify(GROUP_NAME_ALL_DEVICES),
ATTR_VISIBLE: False,
ATTR_NAME: GROUP_NAME_ALL_DEVICES,
ATTR_ADD_ENTITIES: [device.entity_id],
},
)
)
self.hass.bus.async_fire(
EVENT_NEW_DEVICE,
{
@@ -271,27 +245,6 @@ class DeviceTracker:
update_config, self.hass.config.path(YAML_DEVICES), dev_id, device
)
@callback
def async_setup_group(self):
"""Initialize group for all tracked devices.
This method must be run in the event loop.
"""
entity_ids = [dev.entity_id for dev in self.devices.values() if dev.track]
self.hass.async_create_task(
self.hass.services.async_call(
DOMAIN_GROUP,
SERVICE_SET,
{
ATTR_OBJECT_ID: util.slugify(GROUP_NAME_ALL_DEVICES),
ATTR_VISIBLE: False,
ATTR_NAME: GROUP_NAME_ALL_DEVICES,
ATTR_ENTITIES: entity_ids,
},
)
)
@callback
def async_update_stale(self, now: dt_util.dt.datetime):
"""Update stale devices.
@@ -489,7 +442,7 @@ class Device(RestoreEntity):
if self.location_name:
self._state = self.location_name
elif self.gps is not None and self.source_type == SOURCE_TYPE_GPS:
zone_state = async_active_zone(
zone_state = zone.async_active_zone(
self.hass, self.gps[0], self.gps[1], self.gps_accuracy
)
if zone_state is None:
+2 -7
View File
@@ -6,7 +6,6 @@ from typing import Optional
import voluptuous as vol
from homeassistant.components import group
from homeassistant.const import SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import ( # noqa: F401
@@ -22,9 +21,6 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = "fan"
SCAN_INTERVAL = timedelta(seconds=30)
GROUP_NAME_ALL_FANS = "all fans"
ENTITY_ID_ALL_FANS = group.ENTITY_ID_FORMAT.format(GROUP_NAME_ALL_FANS)
ENTITY_ID_FORMAT = DOMAIN + ".{}"
# Bitfield of features supported by the fan entity
@@ -58,9 +54,8 @@ PROP_TO_ATTR = {
@bind_hass
def is_on(hass, entity_id: Optional[str] = None) -> bool:
def is_on(hass, entity_id: str) -> bool:
"""Return if the fans are on based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_FANS
state = hass.states.get(entity_id)
return state.attributes[ATTR_SPEED] not in [SPEED_OFF, None]
@@ -68,7 +63,7 @@ def is_on(hass, entity_id: Optional[str] = None) -> bool:
async def async_setup(hass, config: dict):
"""Expose fan control via statemachine and services."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_FANS
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
)
await component.async_setup(config)
+3 -8
View File
@@ -9,7 +9,6 @@ from typing import Dict, Optional, Tuple
import voluptuous as vol
from homeassistant.auth.permissions.const import POLICY_CONTROL
from homeassistant.components.group import ENTITY_ID_FORMAT as GROUP_ENTITY_ID_FORMAT
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TOGGLE,
@@ -34,9 +33,6 @@ import homeassistant.util.color as color_util
DOMAIN = "light"
SCAN_INTERVAL = timedelta(seconds=30)
GROUP_NAME_ALL_LIGHTS = "all lights"
ENTITY_ID_ALL_LIGHTS = GROUP_ENTITY_ID_FORMAT.format("all_lights")
ENTITY_ID_FORMAT = DOMAIN + ".{}"
# Bitfield of features supported by the light entity
@@ -131,9 +127,8 @@ _LOGGER = logging.getLogger(__name__)
@bind_hass
def is_on(hass, entity_id=None):
def is_on(hass, entity_id):
"""Return if the lights are on based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_LIGHTS
return hass.states.is_state(entity_id, STATE_ON)
@@ -183,7 +178,7 @@ def preprocess_turn_off(params):
async def async_setup(hass, config):
"""Expose light control via state machine and services."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LIGHTS
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
)
await component.async_setup(config)
@@ -336,7 +331,7 @@ class Profiles:
name = entity_id + ".default"
if name in cls._all:
return name
name = ENTITY_ID_ALL_LIGHTS + ".default"
name = "group.all_lights.default"
if name in cls._all:
return name
return None
+2 -7
View File
@@ -5,7 +5,6 @@ import logging
import voluptuous as vol
from homeassistant.components import group
from homeassistant.const import (
ATTR_CODE,
ATTR_CODE_FORMAT,
@@ -32,11 +31,8 @@ ATTR_CHANGED_BY = "changed_by"
DOMAIN = "lock"
SCAN_INTERVAL = timedelta(seconds=30)
ENTITY_ID_ALL_LOCKS = group.ENTITY_ID_FORMAT.format("all_locks")
ENTITY_ID_FORMAT = DOMAIN + ".{}"
GROUP_NAME_ALL_LOCKS = "all locks"
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
LOCK_SERVICE_SCHEMA = make_entity_service_schema({vol.Optional(ATTR_CODE): cv.string})
@@ -50,16 +46,15 @@ PROP_TO_ATTR = {"changed_by": ATTR_CHANGED_BY, "code_format": ATTR_CODE_FORMAT}
@bind_hass
def is_locked(hass, entity_id=None):
def is_locked(hass, entity_id):
"""Return if the lock is locked based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_LOCKS
return hass.states.is_state(entity_id, STATE_LOCKED)
async def async_setup(hass, config):
"""Track states and offer events for locks."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LOCKS
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
)
await component.async_setup(config)
+1 -4
View File
@@ -5,7 +5,6 @@ import logging
import voluptuous as vol
from homeassistant.components import group
from homeassistant.components.recorder.models import States
from homeassistant.components.recorder.util import execute, session_scope
from homeassistant.const import (
@@ -101,8 +100,6 @@ PLANT_SCHEMA = vol.Schema(
)
DOMAIN = "plant"
GROUP_NAME_ALL_PLANTS = "all plants"
ENTITY_ID_ALL_PLANTS = group.ENTITY_ID_FORMAT.format("all_plants")
CONFIG_SCHEMA = vol.Schema({DOMAIN: {cv.string: PLANT_SCHEMA}}, extra=vol.ALLOW_EXTRA)
@@ -114,7 +111,7 @@ ENABLE_LOAD_HISTORY = False
async def async_setup(hass, config):
"""Set up the Plant component."""
component = EntityComponent(_LOGGER, DOMAIN, hass, group_name=GROUP_NAME_ALL_PLANTS)
component = EntityComponent(_LOGGER, DOMAIN, hass)
entities = []
for plant_name, plant_config in config[DOMAIN].items():
@@ -17,7 +17,6 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = "remember_the_milk"
DEFAULT_NAME = DOMAIN
GROUP_NAME_RTM = "remember the milk accounts"
CONF_SHARED_SECRET = "shared_secret"
CONF_ID_MAP = "id_map"
@@ -50,7 +49,7 @@ SERVICE_SCHEMA_COMPLETE_TASK = vol.Schema({vol.Required(CONF_ID): cv.string})
def setup(hass, config):
"""Set up the Remember the milk component."""
component = EntityComponent(_LOGGER, DOMAIN, hass, group_name=GROUP_NAME_RTM)
component = EntityComponent(_LOGGER, DOMAIN, hass)
stored_rtm_config = RememberTheMilkConfiguration(hass)
for rtm_config in config[DOMAIN]:
+2 -9
View File
@@ -5,7 +5,6 @@ import logging
import voluptuous as vol
from homeassistant.components import group
from homeassistant.const import (
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
@@ -38,11 +37,8 @@ ATTR_TIMEOUT = "timeout"
DOMAIN = "remote"
SCAN_INTERVAL = timedelta(seconds=30)
ENTITY_ID_ALL_REMOTES = group.ENTITY_ID_FORMAT.format("all_remotes")
ENTITY_ID_FORMAT = DOMAIN + ".{}"
GROUP_NAME_ALL_REMOTES = "all remotes"
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
SERVICE_SEND_COMMAND = "send_command"
@@ -61,17 +57,14 @@ REMOTE_SERVICE_ACTIVITY_SCHEMA = make_entity_service_schema(
@bind_hass
def is_on(hass, entity_id=None):
def is_on(hass, entity_id):
"""Return if the remote is on based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_REMOTES
return hass.states.is_state(entity_id, STATE_ON)
async def async_setup(hass, config):
"""Track states and offer events for remotes."""
component = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_REMOTES
)
component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL)
await component.async_setup(config)
component.async_register_entity_service(
+1 -5
View File
@@ -38,8 +38,6 @@ CONF_SEQUENCE = "sequence"
ENTITY_ID_FORMAT = DOMAIN + ".{}"
GROUP_NAME_ALL_SCRIPTS = "all scripts"
SCRIPT_ENTRY_SCHEMA = vol.Schema(
{
CONF_ALIAS: cv.string,
@@ -73,9 +71,7 @@ def is_on(hass, entity_id):
async def async_setup(hass, config):
"""Load the scripts from the configuration."""
component = EntityComponent(
_LOGGER, DOMAIN, hass, group_name=GROUP_NAME_ALL_SCRIPTS
)
component = EntityComponent(_LOGGER, DOMAIN, hass)
await _async_process_config(hass, config, component)
+2 -7
View File
@@ -4,7 +4,6 @@ import logging
import voluptuous as vol
from homeassistant.components import group
from homeassistant.const import (
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
@@ -24,9 +23,6 @@ from homeassistant.loader import bind_hass
DOMAIN = "switch"
SCAN_INTERVAL = timedelta(seconds=30)
GROUP_NAME_ALL_SWITCHES = "all switches"
ENTITY_ID_ALL_SWITCHES = group.ENTITY_ID_FORMAT.format("all_switches")
ENTITY_ID_FORMAT = DOMAIN + ".{}"
ATTR_TODAY_ENERGY_KWH = "today_energy_kwh"
@@ -50,19 +46,18 @@ _LOGGER = logging.getLogger(__name__)
@bind_hass
def is_on(hass, entity_id=None):
def is_on(hass, entity_id):
"""Return if the switch is on based on the statemachine.
Async friendly.
"""
entity_id = entity_id or ENTITY_ID_ALL_SWITCHES
return hass.states.is_state(entity_id, STATE_ON)
async def async_setup(hass, config):
"""Track states and offer events for switches."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_SWITCHES
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
)
await component.async_setup(config)
+2 -7
View File
@@ -5,7 +5,6 @@ import logging
import voluptuous as vol
from homeassistant.components import group
from homeassistant.const import ( # noqa: F401 # STATE_PAUSED/IDLE are API
ATTR_BATTERY_LEVEL,
ATTR_COMMAND,
@@ -34,9 +33,6 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = "vacuum"
SCAN_INTERVAL = timedelta(seconds=20)
GROUP_NAME_ALL_VACUUMS = "all vacuum cleaners"
ENTITY_ID_ALL_VACUUMS = group.ENTITY_ID_FORMAT.format("all_vacuum_cleaners")
ATTR_BATTERY_ICON = "battery_icon"
ATTR_CLEANED_AREA = "cleaned_area"
ATTR_FAN_SPEED = "fan_speed"
@@ -81,16 +77,15 @@ SUPPORT_START = 8192
@bind_hass
def is_on(hass, entity_id=None):
def is_on(hass, entity_id):
"""Return if the vacuum is on based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_VACUUMS
return hass.states.is_state(entity_id, STATE_ON)
async def async_setup(hass, config):
"""Set up the vacuum component."""
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_VACUUMS
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
)
await component.async_setup(config)
@@ -440,7 +440,6 @@ async def async_setup_entry(hass, config_entry):
platform=None,
scan_interval=DEFAULT_SCAN_INTERVAL,
entity_namespace=None,
async_entities_added_callback=lambda: None,
)
platform.config_entry = config_entry