Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit f333afe

Browse files
committed
move common functions to utils.py
1 parent a4363c0 commit f333afe

File tree

4 files changed

+75
-59
lines changed

4 files changed

+75
-59
lines changed

custom_components/adtpulse/__init__.py

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@
1818
CONF_USERNAME,
1919
EVENT_HOMEASSISTANT_STOP,
2020
)
21-
from homeassistant.core import HomeAssistant, callback
21+
from homeassistant.core import HomeAssistant
2222
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
23-
from homeassistant.helpers import entity_registry
2423
from homeassistant.helpers.config_entry_flow import FlowResult
2524
from homeassistant.helpers.typing import ConfigType
26-
from homeassistant.util import slugify
2725
from pyadtpulse import PyADTPulse
2826
from pyadtpulse.const import (
2927
ADT_DEFAULT_KEEPALIVE_INTERVAL,
3028
ADT_DEFAULT_POLL_INTERVAL,
3129
ADT_DEFAULT_RELOGIN_INTERVAL,
32-
STATE_OK,
33-
STATE_ONLINE,
3430
)
35-
from pyadtpulse.site import ADTPulseSite
36-
from pyadtpulse.zones import ADTPulseZoneData
3731

3832
from .const import (
3933
ADTPULSE_DOMAIN,
@@ -49,52 +43,9 @@
4943
SUPPORTED_PLATFORMS = ["alarm_control_panel", "binary_sensor"]
5044

5145

52-
def get_gateway_unique_id(site: ADTPulseSite) -> str:
53-
"""Get unique ID for gateway."""
54-
return f"adt_pulse_gateway_{site.id}"
55-
56-
57-
def get_alarm_unique_id(site: ADTPulseSite) -> str:
58-
"""Get unique ID for alarm."""
59-
return f"adt_pulse_alarm_{site.id}"
60-
61-
62-
def zone_open(zone: ADTPulseZoneData) -> bool:
63-
"""Determine if a zone is opened."""
64-
return not zone.state == STATE_OK
65-
66-
67-
def zone_trouble(zone: ADTPulseZoneData) -> bool:
68-
"""Determine if a zone is in trouble state."""
69-
return not zone.status == STATE_ONLINE
70-
71-
72-
@callback
73-
def migrate_entity_name(
74-
hass: HomeAssistant, site: ADTPulseSite, platform_name: str, entity_uid: str
75-
) -> None:
76-
"""Migrate old entity names."""
77-
registry = entity_registry.async_get(hass)
78-
if registry is None:
79-
return
80-
# this seems backwards
81-
entity_id = registry.async_get_entity_id(
82-
platform_name,
83-
ADTPULSE_DOMAIN,
84-
entity_uid,
85-
)
86-
if entity_id is not None:
87-
# change has_entity_name to True and set name to None for devices
88-
registry.async_update_entity(entity_id, has_entity_name=True, name=None)
89-
# rename site name to site id for entities which have site name
90-
slugified_site_name = slugify(site.name)
91-
if slugified_site_name in entity_id:
92-
registry.async_update_entity(
93-
entity_id, new_entity_id=entity_id.replace(slugified_site_name, site.id)
94-
)
95-
96-
97-
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
46+
async def async_setup(
47+
hass: HomeAssistant, config: ConfigType # pylint: disable=unused-argument
48+
) -> bool:
9849
"""Start up the ADT Pulse HA integration.
9950
10051
Args:

custom_components/adtpulse/alarm_control_panel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
)
3535
from pyadtpulse.site import ADTPulseSite
3636

37-
from . import (
37+
from .const import ADTPULSE_DATA_ATTRIBUTION, ADTPULSE_DOMAIN
38+
from .coordinator import ADTPulseDataUpdateCoordinator
39+
from .utils import (
3840
get_alarm_unique_id,
3941
get_gateway_unique_id,
4042
migrate_entity_name,
4143
zone_open,
4244
zone_trouble,
4345
)
44-
from .const import ADTPULSE_DATA_ATTRIBUTION, ADTPULSE_DOMAIN
45-
from .coordinator import ADTPulseDataUpdateCoordinator
4646

4747
LOG = getLogger(__name__)
4848

custom_components/adtpulse/binary_sensor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
from pyadtpulse.site import ADTPulseSite
2626
from pyadtpulse.zones import ADTPulseZoneData
2727

28-
from . import (
28+
from .const import ADTPULSE_DATA_ATTRIBUTION, ADTPULSE_DOMAIN
29+
from .coordinator import ADTPulseDataUpdateCoordinator
30+
from .utils import (
2931
get_alarm_unique_id,
3032
get_gateway_unique_id,
3133
migrate_entity_name,
3234
zone_open,
3335
zone_trouble,
3436
)
35-
from .const import ADTPULSE_DATA_ATTRIBUTION, ADTPULSE_DOMAIN
36-
from .coordinator import ADTPulseDataUpdateCoordinator
3737

3838
LOG = getLogger(__name__)
3939

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""ADT Pulse utility functions."""
2+
from __future__ import annotations
3+
4+
from homeassistant.core import HomeAssistant
5+
from homeassistant.helpers import entity_registry
6+
from pyadtpulse.const import STATE_OK, STATE_ONLINE
7+
from pyadtpulse.site import ADTPulseSite
8+
from pyadtpulse.zones import ADTPulseZoneData
9+
10+
from .const import ADTPULSE_DOMAIN
11+
12+
13+
def migrate_entity_name(
14+
hass: HomeAssistant, site: ADTPulseSite, platform_name: str, entity_uid: str
15+
) -> None:
16+
"""Migrate old entity names."""
17+
registry = entity_registry.async_get(hass)
18+
if registry is None:
19+
return
20+
# this seems backwards
21+
entity_id = registry.async_get_entity_id(
22+
platform_name,
23+
ADTPULSE_DOMAIN,
24+
entity_uid,
25+
)
26+
if entity_id is not None:
27+
# change has_entity_name to True and set name to None for devices
28+
registry.async_update_entity(entity_id, has_entity_name=True, name=None)
29+
# rename site name to site id for entities which have site name
30+
slugified_site_name = slugify(site.name)
31+
if slugified_site_name in entity_id:
32+
registry.async_update_entity(
33+
entity_id, new_entity_id=entity_id.replace(slugified_site_name, site.id)
34+
)
35+
36+
37+
def get_gateway_unique_id(site: ADTPulseSite) -> str:
38+
"""Get entity unique id for the gateway."""
39+
return f"adt_pulse_gateway_{site.id}"
40+
41+
42+
def get_alarm_unique_id(site: ADTPulseSite) -> str:
43+
"""Get entity unique ID for alarm."""
44+
return f"adt_pulse_alarm_{site.id}"
45+
46+
47+
def zone_open(zone: ADTPulseZoneData) -> bool:
48+
"""Determine if a zone is opened."""
49+
return not zone.state == STATE_OK
50+
51+
52+
def zone_trouble(zone: ADTPulseZoneData) -> bool:
53+
"""Determine if a zone is in trouble state."""
54+
return not zone.status == STATE_ONLINE
55+
56+
57+
def system_can_be_armed(site: ADTPulseSite) -> bool:
58+
"""Determine is the system is able to be armed without being forced."""
59+
zones = site.zones_as_dict
60+
if zones is None:
61+
return False
62+
for zone in zones:
63+
if zone_open(zone) or zone_trouble(zone):
64+
return False
65+
return True

0 commit comments

Comments
 (0)