Skip to content

Commit 706d5cc

Browse files
authored
Merge pull request #109 from c-st/fix-deprecations
Fix deprecation issues
2 parents 042cd0e + cfe428d commit 706d5cc

File tree

10 files changed

+74
-50
lines changed

10 files changed

+74
-50
lines changed

.devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "c-st/auto_areas",
3-
"image": "mcr.microsoft.com/vscode/devcontainers/python:0-3.10-bullseye",
3+
"image": "mcr.microsoft.com/devcontainers/python:3.12",
44
"postCreateCommand": "scripts/setup",
55
"forwardPorts": [
66
8123

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: "Set up Python"
1717
uses: actions/setup-python@v5.0.0
1818
with:
19-
python-version: "3.10"
19+
python-version: "3.12"
2020
cache: "pip"
2121

2222
- name: "Install requirements"

.github/workflows/validate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- name: "Set up Python"
3838
uses: actions/setup-python@v5.0.0
3939
with:
40-
python-version: "3.10"
40+
python-version: "3.12"
4141
cache: "pip"
4242

4343
- name: "Install requirements"

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Pull requests are the best way to propose changes to the codebase.
1616
1. Fork the repo and create your branch from `main`.
1717
2. If you've changed something, update the documentation.
1818
3. Make sure your code lints (using `scripts/lint`).
19-
4. Test you contribution.
20-
5. Issue that pull request!
19+
4. Test your contribution.
20+
5. Create that pull request!
2121

2222
## Any contributions you make will be under the MIT Software License
2323

custom_components/auto_areas/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
from .const import DOMAIN, LOGGER, ISSUE_TYPE_YAML_DETECTED
1717

18-
PLATFORMS: list[Platform] = [Platform.SWITCH, Platform.BINARY_SENSOR, Platform.SENSOR]
18+
PLATFORMS: list[Platform] = [Platform.SWITCH,
19+
Platform.BINARY_SENSOR, Platform.SENSOR]
1920

2021

2122
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
@@ -36,19 +37,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3637

3738
return True
3839

40+
3941
async def async_init(hass: HomeAssistant, entry: ConfigEntry, auto_area: AutoArea):
42+
"""Initialize component."""
4043
await auto_area.initialize()
4144
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
4245
hass.async_create_task(
4346
hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
4447
)
4548

49+
4650
def initialize(hass: HomeAssistant, entry: ConfigEntry, auto_area: AutoArea):
4751
"""Initialize area after HA has started."""
4852
return asyncio.run_coroutine_threadsafe(
4953
async_init(hass, entry, auto_area), hass.loop
5054
).result()
5155

56+
5257
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5358
"""Handle removal of an entry."""
5459
# unsubscribe from changes:

custom_components/auto_areas/auto_area.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""Core entity functionality."""
22
from __future__ import annotations
33
from homeassistant.core import HomeAssistant
4+
from homeassistant.helpers.area_registry import async_get as async_get_area_registry
5+
from homeassistant.helpers.device_registry import async_get as async_get_device_registry
6+
from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry
7+
48

59
from homeassistant.config_entries import ConfigEntry
610

@@ -28,16 +32,15 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
2832
self.hass = hass
2933
self.config_entry = entry
3034

31-
self.area_registry = self.hass.helpers.area_registry.async_get(self.hass)
32-
self.device_registry = self.hass.helpers.device_registry.async_get(self.hass)
33-
self.entity_registry = self.hass.helpers.entity_registry.async_get(self.hass)
35+
self.area_registry = async_get_area_registry(self.hass)
36+
self.device_registry = async_get_device_registry(self.hass)
37+
self.entity_registry = async_get_entity_registry(self.hass)
3438

3539
self.area_id: str = entry.data.get("area")
3640
self.area: AreaEntry = self.area_registry.async_get_area(self.area_id)
3741

3842
LOGGER.info('🤖 Auto Area "%s" (%s)', entry.title, entry.options)
3943

40-
4144
async def initialize(self):
4245
"""Subscribe to area changes and reload if necessary."""
4346
LOGGER.info("%s: Initializing after HA start", self.area.name)

custom_components/auto_areas/auto_lights.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Auto lights."""
2-
from homeassistant.core import State
32
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
4-
from homeassistant.helpers.event import async_track_state_change
3+
from homeassistant.helpers.event import async_track_state_change_event
54
from homeassistant.const import (
65
STATE_ON,
76
SERVICE_TURN_ON,
87
SERVICE_TURN_OFF,
98
ATTR_ENTITY_ID,
109
)
1110
from homeassistant.util import slugify
11+
from homeassistant.core import Event
1212

1313
from .ha_helpers import get_all_entities
1414

@@ -37,25 +37,31 @@ def __init__(self, auto_area) -> None:
3737

3838
# Config
3939
self.illuminance_threshold = (
40-
self.auto_area.config_entry.options.get(CONFIG_AUTO_LIGHTS_MAX_ILLUMINANCE)
40+
self.auto_area.config_entry.options.get(
41+
CONFIG_AUTO_LIGHTS_MAX_ILLUMINANCE)
4142
or 0
4243
)
4344
self.is_sleeping_area = (
44-
self.auto_area.config_entry.options.get(CONFIG_IS_SLEEPING_AREA) or False
45+
self.auto_area.config_entry.options.get(
46+
CONFIG_IS_SLEEPING_AREA) or False
4547
)
4648
self.excluded_light_entities = (
47-
self.auto_area.config_entry.options.get(CONFIG_EXCLUDED_LIGHT_ENTITIES)
49+
self.auto_area.config_entry.options.get(
50+
CONFIG_EXCLUDED_LIGHT_ENTITIES)
4851
or []
4952
)
5053

5154
self.sleep_mode_entity_id = (
52-
f"{SLEEP_MODE_SWITCH_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}"
55+
f"{SLEEP_MODE_SWITCH_ENTITY_PREFIX}{
56+
slugify(self.auto_area.area.name)}"
5357
)
5458
self.presence_entity_id = (
55-
f"{PRESENCE_BINARY_SENSOR_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}"
59+
f"{PRESENCE_BINARY_SENSOR_ENTITY_PREFIX}{
60+
slugify(self.auto_area.area.name)}"
5661
)
5762
self.illuminance_entity_id = (
58-
f"{ILLUMINANCE_SENSOR_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}"
63+
f"{ILLUMINANCE_SENSOR_ENTITY_PREFIX}{
64+
slugify(self.auto_area.area.name)}"
5965
)
6066

6167
self.light_entity_ids = [
@@ -91,7 +97,7 @@ async def initialize(self):
9197
sleep_mode_state = self.hass.states.get(self.sleep_mode_entity_id)
9298
if sleep_mode_state:
9399
self.sleep_mode_enabled = sleep_mode_state.state == STATE_ON
94-
self.unsubscribe_sleep_mode = async_track_state_change(
100+
self.unsubscribe_sleep_mode = async_track_state_change_event(
95101
self.hass,
96102
self.sleep_mode_entity_id,
97103
self.handle_sleep_mode_state_change,
@@ -115,22 +121,24 @@ async def initialize(self):
115121
)
116122
self.lights_turned_on = False
117123

118-
self.unsubscribe_presence = async_track_state_change(
124+
self.unsubscribe_presence = async_track_state_change_event(
119125
self.auto_area.hass,
120126
self.presence_entity_id,
121127
self.handle_presence_state_change,
122128
)
123129

124-
self.unsubscribe_illuminance = async_track_state_change(
130+
self.unsubscribe_illuminance = async_track_state_change_event(
125131
self.auto_area.hass,
126132
self.illuminance_entity_id,
127133
self.handle_illuminance_change,
128134
)
129135

130-
async def handle_presence_state_change(
131-
self, entity_id, from_state: State, to_state: State
132-
):
136+
async def handle_presence_state_change(self, event: Event):
133137
"""Handle changes in presence."""
138+
entity_id = event.data.get('entity_id')
139+
from_state = event.data.get('old_state')
140+
to_state = event.data.get('new_state')
141+
134142
previous_state = from_state.state if from_state else ""
135143
current_state = to_state.state
136144

@@ -194,10 +202,12 @@ async def handle_presence_state_change(
194202
)
195203
self.lights_turned_on = False
196204

197-
async def handle_sleep_mode_state_change(
198-
self, entity_id, from_state: State, to_state: State
199-
):
205+
async def handle_sleep_mode_state_change(self, event: Event):
200206
"""Handle changes in sleep mode."""
207+
entity_id = event.data.get('entity_id')
208+
from_state = event.data.get('old_state')
209+
to_state = event.data.get('new_state')
210+
201211
previous_state = from_state.state if from_state else ""
202212
current_state = to_state.state if to_state else ""
203213

@@ -245,9 +255,7 @@ async def handle_sleep_mode_state_change(
245255
)
246256
self.lights_turned_on = True
247257

248-
async def handle_illuminance_change(
249-
self, _entity_id, _from_state: State, _to_state: State
250-
):
258+
async def handle_illuminance_change(self, _event: Event):
251259
"""Handle changes in illuminance."""
252260

253261
# Check for presence

custom_components/auto_areas/binary_sensor.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
BinarySensorDeviceClass,
66
BinarySensorEntity,
77
)
8-
from homeassistant.core import State
9-
from homeassistant.helpers.event import async_track_state_change
8+
from homeassistant.helpers.event import async_track_state_change_event
109
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1110
from homeassistant.helpers.entity import DeviceInfo
1211
from homeassistant.util import slugify
12+
from homeassistant.core import Event
13+
1314

1415
from .ha_helpers import all_states_are_off
1516

@@ -43,7 +44,8 @@ def __init__(self, auto_area: AutoArea) -> None:
4344
self.presence: bool = None
4445
self.presence_entities: list[str] = self.get_presence_entities()
4546
self.unsubscribe = None
46-
LOGGER.info("%s: Initialized presence binary sensor", self.auto_area.area.name)
47+
LOGGER.info("%s: Initialized presence binary sensor",
48+
self.auto_area.area.name)
4749

4850
@property
4951
def name(self):
@@ -79,7 +81,8 @@ def is_on(self) -> bool:
7981
def get_presence_entities(self) -> list(str):
8082
"""Collect entities to be used for determining presence."""
8183
entity_ids = [
82-
f"{PRESENCE_LOCK_SWITCH_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}"
84+
f"{PRESENCE_LOCK_SWITCH_ENTITY_PREFIX}{
85+
slugify(self.auto_area.area.name)}"
8386
]
8487

8588
# include relevant presence entities, but not this sensor:
@@ -108,10 +111,11 @@ async def async_added_to_hass(self):
108111
)
109112
self.schedule_update_ha_state()
110113

111-
LOGGER.info("%s: Initial presence %s", self.auto_area.area.name, self.presence)
114+
LOGGER.info("%s: Initial presence %s",
115+
self.auto_area.area.name, self.presence)
112116

113117
# Subscribe to state changes
114-
self.unsubscribe = async_track_state_change(
118+
self.unsubscribe = async_track_state_change_event(
115119
self.hass,
116120
self.presence_entities,
117121
self.handle_presence_state_change,
@@ -122,10 +126,12 @@ async def async_will_remove_from_hass(self) -> None:
122126
if self.unsubscribe:
123127
self.unsubscribe()
124128

125-
def handle_presence_state_change(
126-
self, entity_id, from_state: State, to_state: State
127-
):
129+
def handle_presence_state_change(self, event: Event):
128130
"""Handle state change of any tracked presence sensors."""
131+
entity_id = event.data.get('entity_id')
132+
from_state = event.data.get('old_state')
133+
to_state = event.data.get('new_state')
134+
129135
previous_state = from_state.state if from_state else ""
130136
current_state = to_state.state if to_state else ""
131137

@@ -152,6 +158,7 @@ def handle_presence_state_change(
152158
PRESENCE_ON_STATES,
153159
):
154160
if self.presence:
155-
LOGGER.info("%s: Presence cleared", self.auto_area.area.name)
161+
LOGGER.info("%s: Presence cleared",
162+
self.auto_area.area.name)
156163
self.presence = False
157164
self.schedule_update_ha_state()

custom_components/auto_areas/sensor.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
SensorDeviceClass,
44
SensorEntity,
55
)
6-
from homeassistant.core import State
76
from homeassistant.helpers.entity_platform import AddEntitiesCallback
8-
from homeassistant.helpers.event import async_track_state_change
7+
from homeassistant.helpers.event import async_track_state_change_event
98
from homeassistant.helpers.entity import DeviceInfo
9+
from homeassistant.core import Event
1010

1111
from .const import DOMAIN, LOGGER, ILLUMINANCE_SENSOR_PREFIX, VERSION, NAME
1212
from .auto_area import AutoArea
@@ -28,7 +28,8 @@ def __init__(self, auto_area: AutoArea) -> None:
2828
self.illuminance_entities: list[str] = self.get_illuminance_entities()
2929
self.unsubscribe = None
3030
self.value = None
31-
LOGGER.info("%s: Initialized illuminance sensor", self.auto_area.area.name)
31+
LOGGER.info("%s: Initialized illuminance sensor",
32+
self.auto_area.area.name)
3233

3334
@property
3435
def name(self):
@@ -102,7 +103,7 @@ async def async_added_to_hass(self):
102103
)
103104

104105
# Subscribe to state changes
105-
self.unsubscribe = async_track_state_change(
106+
self.unsubscribe = async_track_state_change_event(
106107
self.hass,
107108
self.illuminance_entities,
108109
self.handle_illuminance_change,
@@ -113,10 +114,10 @@ async def async_will_remove_from_hass(self) -> None:
113114
if self.unsubscribe:
114115
self.unsubscribe()
115116

116-
def handle_illuminance_change(
117-
self, _entity_id, _from_state: State, to_state: State
118-
):
117+
def handle_illuminance_change(self, event: Event):
119118
"""Handle state change of any tracked illuminance sensors."""
119+
to_state = event.data.get('new_state')
120+
120121
if to_state.state not in [
121122
"unknown",
122123
"unavailable",

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
colorlog==6.7.0
2-
homeassistant==2023.4.0
3-
pip>=21.0,<24.1
1+
# colorlog>=6.8
2+
homeassistant==2024.6.1
3+
pip<23.2,>=21.3.1
44
ruff==0.1.14

0 commit comments

Comments
 (0)