Skip to content

Commit e5669fb

Browse files
authored
Merge pull request #132 from c-st/cover-group
Add support for cover groups
2 parents 56c6f81 + d193f3c commit e5669fb

File tree

5 files changed

+112
-8
lines changed

5 files changed

+112
-8
lines changed

custom_components/auto_areas/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

1717
from .const import DOMAIN, LOGGER, ISSUE_TYPE_YAML_DETECTED
1818

19-
PLATFORMS: list[Platform] = [Platform.SWITCH,
20-
Platform.BINARY_SENSOR, Platform.SENSOR]
19+
PLATFORMS: list[Platform] = [
20+
Platform.SWITCH,
21+
Platform.BINARY_SENSOR,
22+
Platform.SENSOR,
23+
Platform.COVER,
24+
]
2125

2226

2327
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

custom_components/auto_areas/auto_area.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ def get_valid_entities(self) -> list[RegistryEntry]:
9393
]
9494
return entities
9595

96+
def get_area_entity_ids(self, device_classes: list[str]) -> list[str]:
97+
"""Return all entity ids in a list of device classes."""
98+
return [
99+
entity.entity_id
100+
for entity in self.get_valid_entities()
101+
if entity.device_class in device_classes
102+
or entity.original_device_class in device_classes
103+
]
104+
96105
@property
97106
def area_name(self) -> str:
98107
"""Return area name or fallback."""

custom_components/auto_areas/auto_entity.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@
1010
from homeassistant.helpers.device_registry import DeviceInfo
1111
from homeassistant.helpers.typing import StateType
1212
from homeassistant.components.sensor.const import SensorDeviceClass
13+
from homeassistant.components.cover import CoverDeviceClass
1314
from homeassistant.helpers.event import async_track_state_change_event
1415

1516
from custom_components.auto_areas.calculations import get_calculation
1617

1718
from .auto_area import AutoArea
1819
from .const import DOMAIN, LOGGER, NAME, VERSION
1920

20-
_TDeviceClass = TypeVar(
21-
"_TDeviceClass", BinarySensorDeviceClass, SensorDeviceClass)
2221
_TEntity = TypeVar("_TEntity", bound=Entity)
22+
_TDeviceClass = TypeVar(
23+
"_TDeviceClass",
24+
BinarySensorDeviceClass,
25+
SensorDeviceClass,
26+
CoverDeviceClass
27+
)
2328

2429

2530
class AutoEntity(Entity, Generic[_TEntity, _TDeviceClass]):
26-
"""Set up an aggregated entity."""
31+
"""Set up an Auto Area entity."""
2732

2833
def __init__(self,
2934
hass: HomeAssistant,

custom_components/auto_areas/const.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
1111
from homeassistant.components.sensor.const import DOMAIN as SENSOR_DOMAIN
1212
from homeassistant.components.switch.const import DOMAIN as SWITCH_DOMAIN
13+
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
1314
from homeassistant.const import STATE_HOME, STATE_ON, STATE_PLAYING
1415

1516

@@ -44,6 +45,9 @@
4445
HUMIDITY_SENSOR_PREFIX = "Area Humidity "
4546
HUMIDITY_SENSOR_ENTITY_PREFIX = "sensor.area_humidity_"
4647

48+
COVER_GROUP_PREFIX = "Area Covers "
49+
COVER_GROUP_ENTITY_PREFIX = "cover.area_covers_"
50+
4751
#
4852
# Config flow constants
4953
#
@@ -55,15 +59,14 @@
5559
CONFIG_TEMPERATURE_CALCULATION = "temperature_calculation"
5660
CONFIG_ILLUMINANCE_CALCULATION = "illuminance_calculation"
5761

58-
#
59-
6062

61-
# Fetch entities from these domains
63+
# Fetch entities from these domains:
6264
RELEVANT_DOMAINS = [
6365
BINARY_SENSOR_DOMAIN,
6466
SENSOR_DOMAIN,
6567
SWITCH_DOMAIN,
6668
LIGHT_DOMAIN,
69+
COVER_DOMAIN,
6770
]
6871

6972
# Presence entities
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Cover group."""
2+
3+
from functools import cached_property
4+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
5+
from homeassistant.components.group.cover import CoverGroup
6+
from homeassistant.helpers.device_registry import DeviceInfo
7+
from homeassistant.components.cover import (
8+
CoverDeviceClass,
9+
DEVICE_CLASSES as COVER_DEVICE_CLASSES,
10+
)
11+
12+
from custom_components.auto_areas.auto_area import AutoArea
13+
from custom_components.auto_areas.const import (
14+
COVER_GROUP_ENTITY_PREFIX,
15+
COVER_GROUP_PREFIX,
16+
DOMAIN,
17+
LOGGER,
18+
NAME,
19+
VERSION
20+
)
21+
22+
23+
async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback):
24+
"""Set up the cover platform."""
25+
auto_area: AutoArea = hass.data[DOMAIN][entry.entry_id]
26+
27+
cover_entity_ids: list[str] = auto_area.get_area_entity_ids(
28+
COVER_DEVICE_CLASSES
29+
)
30+
if not cover_entity_ids:
31+
LOGGER.info(
32+
"%s: No covers found in area. Not creating cover group.",
33+
auto_area.area_name,
34+
)
35+
else:
36+
async_add_entities([AutoCoverGroup(
37+
hass,
38+
auto_area,
39+
entity_ids=cover_entity_ids
40+
)])
41+
42+
43+
class AutoCoverGroup(CoverGroup):
44+
"""Cover group with area covers."""
45+
46+
def __init__(self, hass, auto_area: AutoArea, entity_ids: list[str]) -> None:
47+
"""Initialize cover group."""
48+
self.hass = hass
49+
self.auto_area = auto_area
50+
self._device_class = CoverDeviceClass.BLIND
51+
self._name_prefix = COVER_GROUP_PREFIX
52+
self._prefix = COVER_GROUP_ENTITY_PREFIX
53+
self.entity_ids: list[str] = entity_ids
54+
55+
CoverGroup.__init__(
56+
self,
57+
entities=self.entity_ids,
58+
name=None,
59+
unique_id=self._attr_unique_id,
60+
)
61+
62+
LOGGER.debug("Initialized cover group %s", self.entity_ids)
63+
64+
@cached_property
65+
def name(self):
66+
"""Name of this entity."""
67+
return f"{self._name_prefix}{self.auto_area.area_name}"
68+
69+
@cached_property
70+
def device_info(self) -> DeviceInfo:
71+
"""Information about this device."""
72+
return {
73+
"identifiers": {(DOMAIN, self.auto_area.config_entry.entry_id)},
74+
"name": NAME,
75+
"model": VERSION,
76+
"manufacturer": NAME,
77+
"suggested_area": self.auto_area.area_name,
78+
}
79+
80+
@cached_property
81+
def unique_id(self) -> str | None:
82+
"""Return a unique ID."""
83+
return f"{self.auto_area.config_entry.entry_id}_cover_group"

0 commit comments

Comments
 (0)