Skip to content

Commit 2079727

Browse files
committed
fixing #375 and changes to the threshold sensor on 2024.7
1 parent 209f74e commit 2079727

File tree

8 files changed

+102
-44
lines changed

8 files changed

+102
-44
lines changed

custom_components/magic_areas/__init__.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,11 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
102102
}
103103

104104
# Setup platforms
105-
for platform in magic_area.available_platforms():
106-
_LOGGER.debug("%s: Loading platform '%s'...", magic_area.name, platform)
107-
hass.async_create_task(
108-
hass.config_entries.async_forward_entry_setup(config_entry, platform)
109-
)
110-
magic_area.loaded_platforms.append(platform)
105+
await hass.config_entries.async_forward_entry_setups(
106+
config_entry, magic_area.available_platforms()
107+
)
111108

112109
# Conditional reload of related meta-areas
113-
114110
# Populate dict with all meta-areas with ID as key
115111
meta_areas = defaultdict()
116112

@@ -157,28 +153,21 @@ async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -
157153
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
158154
"""Unload a config entry."""
159155

160-
platforms_unloaded = []
161156
data = hass.data[MODULE_DATA]
162157
area_data = data[config_entry.entry_id]
163158
area = area_data[DATA_AREA_OBJECT]
164159

165-
for platform in area.loaded_platforms:
166-
unload_ok = await hass.config_entries.async_forward_entry_unload(
167-
config_entry, platform
168-
)
169-
platforms_unloaded.append(unload_ok)
160+
await hass.config_entries.async_unload_platforms(
161+
config_entry, area.available_platforms()
162+
)
170163

171164
area_data[DATA_UNDO_UPDATE_LISTENER]()
172-
173-
all_unloaded = all(platforms_unloaded)
174-
175-
if all_unloaded:
176-
data.pop(config_entry.entry_id)
165+
data.pop(config_entry.entry_id)
177166

178167
if not data:
179168
hass.data.pop(MODULE_DATA)
180169

181-
return all_unloaded
170+
return True
182171

183172

184173
def migrate_seconds_to_minutes(config_data: dict) -> dict:

custom_components/magic_areas/binary_sensor.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,10 @@ async def async_setup_entry(
4242
) -> None:
4343
"""Set up the Area config entry."""
4444

45-
add_entities_when_ready(
46-
hass, async_add_entities, config_entry, add_sensors, with_hass=True
47-
)
45+
add_entities_when_ready(hass, async_add_entities, config_entry, add_sensors)
4846

4947

50-
def add_sensors(
51-
area: MagicArea, hass: HomeAssistant, async_add_entities: AddEntitiesCallback
52-
) -> None:
48+
def add_sensors(area: MagicArea, async_add_entities: AddEntitiesCallback) -> None:
5349
"""Add the basic sensors for the area."""
5450
entities = []
5551

@@ -59,7 +55,7 @@ def add_sensors(
5955
# Create extra sensors
6056
if area.has_feature(CONF_FEATURE_AGGREGATION):
6157
entities.extend(create_aggregate_sensors(area))
62-
illuminance_threshold_sensor = create_illuminance_threshold(area, hass)
58+
illuminance_threshold_sensor = create_illuminance_threshold(area)
6359
if illuminance_threshold_sensor:
6460
entities.append(illuminance_threshold_sensor)
6561

custom_components/magic_areas/light.py

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
"""Platform file for Magic Area's light entities."""
22

3+
import asyncio
34
import logging
45

56
from homeassistant.components.group.light import LightGroup
6-
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
7+
from homeassistant.components.light import (
8+
ATTR_COLOR_TEMP,
9+
ATTR_HS_COLOR,
10+
DOMAIN as LIGHT_DOMAIN,
11+
SUPPORT_COLOR,
12+
SUPPORT_COLOR_TEMP,
13+
)
714
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
815
from homeassistant.const import (
916
ATTR_ENTITY_ID,
17+
ATTR_SUPPORTED_FEATURES,
1018
SERVICE_TURN_OFF,
1119
SERVICE_TURN_ON,
1220
STATE_OFF,
1321
STATE_ON,
1422
)
1523
from homeassistant.helpers.dispatcher import async_dispatcher_connect
1624
from homeassistant.helpers.event import async_track_state_change_event
25+
from homeassistant.util import color as color_util
1726

1827
from .add_entities_when_ready import add_entities_when_ready
1928
from .base.entities import MagicEntity
@@ -153,6 +162,7 @@ async def async_turn_on(self, **kwargs) -> None:
153162

154163
# Active lights
155164
active_lights = self._get_active_lights()
165+
targeted_lights = self._entity_ids
156166

157167
if active_lights:
158168
_LOGGER.debug(
@@ -161,17 +171,84 @@ async def async_turn_on(self, **kwargs) -> None:
161171
str(active_lights),
162172
)
163173

164-
data[ATTR_ENTITY_ID] = active_lights if active_lights else self._entity_ids
174+
targeted_lights = active_lights
165175

166-
# Forward call
167-
await self.hass.services.async_call(
168-
LIGHT_DOMAIN,
169-
SERVICE_TURN_ON,
170-
data,
171-
blocking=True,
172-
context=self._context,
176+
# Split entities by supported features
177+
entity_map = {SUPPORT_COLOR: [], SUPPORT_COLOR_TEMP: []}
178+
for entity_id in targeted_lights:
179+
state = self.hass.states.get(entity_id)
180+
if not state:
181+
continue
182+
support = state.attributes.get(ATTR_SUPPORTED_FEATURES)
183+
184+
if bool(support & SUPPORT_COLOR):
185+
if bool(support & SUPPORT_COLOR_TEMP):
186+
entity_map[SUPPORT_COLOR_TEMP].append(entity_id)
187+
else:
188+
entity_map[SUPPORT_COLOR].append(entity_id)
189+
190+
no_color_support = list(
191+
set(targeted_lights)
192+
- set(entity_map[SUPPORT_COLOR])
193+
- set(entity_map[SUPPORT_COLOR_TEMP])
173194
)
174195

196+
service_calls = []
197+
198+
if entity_map[SUPPORT_COLOR_TEMP]:
199+
service_data = data.copy()
200+
service_data[ATTR_ENTITY_ID] = entity_map[SUPPORT_COLOR_TEMP]
201+
service_call = self.hass.services.async_call(
202+
LIGHT_DOMAIN,
203+
SERVICE_TURN_ON,
204+
service_data,
205+
blocking=True,
206+
context=self._context,
207+
)
208+
service_calls.append(service_call)
209+
210+
if entity_map[SUPPORT_COLOR]:
211+
service_data = data.copy()
212+
service_data[ATTR_ENTITY_ID] = entity_map[SUPPORT_COLOR]
213+
214+
# Perform color_temp emulation if ATTR_COLOR_TEMP is passed
215+
if ATTR_COLOR_TEMP in service_data:
216+
temp_k = color_util.color_temperature_mired_to_kelvin(
217+
service_data[ATTR_COLOR_TEMP]
218+
)
219+
hs_color = color_util.color_temperature_to_hs(temp_k)
220+
service_data[ATTR_HS_COLOR] = hs_color
221+
del service_data[ATTR_COLOR_TEMP]
222+
223+
service_call = self.hass.services.async_call(
224+
LIGHT_DOMAIN,
225+
SERVICE_TURN_ON,
226+
service_data,
227+
blocking=True,
228+
context=self._context,
229+
)
230+
service_calls.append(service_call)
231+
232+
if no_color_support:
233+
service_data = data.copy()
234+
service_data[ATTR_ENTITY_ID] = no_color_support
235+
if ATTR_COLOR_TEMP in service_data:
236+
del service_data[ATTR_COLOR_TEMP]
237+
if ATTR_HS_COLOR in service_data:
238+
del service_data[ATTR_HS_COLOR]
239+
240+
service_call = self.hass.services.async_call(
241+
LIGHT_DOMAIN,
242+
SERVICE_TURN_ON,
243+
service_data,
244+
blocking=True,
245+
context=self._context,
246+
)
247+
service_calls.append(service_call)
248+
249+
# Perform calls
250+
await asyncio.gather(*service_calls)
251+
175252

176253
class AreaLightGroup(MagicLightGroup):
177254
"""Magic Light Group."""

custom_components/magic_areas/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
"iot_class": "calculated",
2121
"issue_tracker": "https://github.com/jseidl/hass-magic_areas/issues",
2222
"requirements": [],
23-
"version": "4.0.0"
23+
"version": "4.0.1"
2424
}

custom_components/magic_areas/threshold.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass
1010
from homeassistant.components.threshold.binary_sensor import ThresholdSensor
1111
from homeassistant.const import ATTR_DEVICE_CLASS
12-
from homeassistant.core import HomeAssistant
1312
from homeassistant.helpers.entity import Entity
1413

1514
from .base.entities import MagicEntity
@@ -26,7 +25,7 @@
2625
_LOGGER = logging.getLogger(__name__)
2726

2827

29-
def create_illuminance_threshold(area: MagicArea, hass: HomeAssistant) -> Entity:
28+
def create_illuminance_threshold(area: MagicArea) -> Entity:
3029
"""Create threhsold light binary sensor based off illuminance aggregate."""
3130

3231
if not area.has_feature(CONF_FEATURE_AGGREGATION):
@@ -65,7 +64,6 @@ def create_illuminance_threshold(area: MagicArea, hass: HomeAssistant) -> Entity
6564

6665
threshold_sensor = AreaThresholdSensor(
6766
area,
68-
hass=hass,
6967
device_class=BinarySensorDeviceClass.LIGHT,
7068
entity_id=illuminance_aggregate_entity_id,
7169
upper=illuminance_threshold,
@@ -82,7 +80,6 @@ class AreaThresholdSensor(MagicEntity, ThresholdSensor):
8280
def __init__(
8381
self,
8482
area: MagicArea,
85-
hass: HomeAssistant,
8683
device_class: BinarySensorDeviceClass,
8784
entity_id: str,
8885
upper: int | None = None,
@@ -95,7 +92,6 @@ def __init__(
9592
)
9693
ThresholdSensor.__init__(
9794
self,
98-
hass=hass,
9995
entity_id=entity_id,
10096
name=None,
10197
unique_id=self.unique_id,

custom_components/magic_areas/translations/nl-NL.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,4 @@
267267
}
268268
}
269269
}
270-
}
270+
}

hacs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Magic Areas",
33
"filename": "magic_areas.zip",
44
"hide_default_branch": true,
5-
"homeassistant": "2024.5.0",
5+
"homeassistant": "2024.7.0",
66
"render_readme": true,
77
"zip_release": true
88
}

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
homeassistant>=2023.8.0
1+
homeassistant>=2024.7.0
22
pip

0 commit comments

Comments
 (0)