Skip to content

Commit

Permalink
Merge pull request #29 from lawtancool/deprecation_fixes
Browse files Browse the repository at this point in the history
Deprecation fixes
  • Loading branch information
lawtancool authored Sep 1, 2024
2 parents 9539dfc + 419889d commit ec50ba8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 254 deletions.
24 changes: 15 additions & 9 deletions custom_components/control4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

# Add Control4 controller to device registry
try:
controller_href = (await entry_data[CONF_ACCOUNT].getAccountControllers())["href"]
controller_href = (await entry_data[CONF_ACCOUNT].getAccountControllers())[
"href"
]
except (client_exceptions.ClientError, asyncio.TimeoutError) as exception:
raise ConfigEntryNotReady(exception) from exception

Expand Down Expand Up @@ -171,10 +173,13 @@ async def get_items_of_category(hass: HomeAssistant, entry: ConfigEntry, categor
return_list = await director.getAllItemsByCategory(category)
return json.loads(return_list)
except InvalidCategory as e:
_LOGGER.warning("Category %s does not exist on this Control4 system, \
entities from this domain will not be setup.", category, exc_info=True)
_LOGGER.warning(
"Category %s does not exist on this Control4 system, \
entities from this domain will not be setup.",
category,
exc_info=True,
)
return []



async def refresh_tokens(hass: HomeAssistant, entry: ConfigEntry):
Expand Down Expand Up @@ -236,8 +241,10 @@ async def refresh_tokens(hass: HomeAssistant, entry: ConfigEntry):
raise ConfigEntryNotReady(exception) from exception

# Schedule refresh 5mins before expiry, but no sooner than 5mins from now
delay = max(director_token_dict["validSeconds"]
- SCHEDULE_REFRESH_ADVANCE_SEC, SCHEDULE_REFRESH_ADVANCE_SEC)
delay = max(
director_token_dict["validSeconds"] - SCHEDULE_REFRESH_ADVANCE_SEC,
SCHEDULE_REFRESH_ADVANCE_SEC,
)

_LOGGER.debug(
"Registering next token refresh in %s seconds",
Expand Down Expand Up @@ -324,8 +331,7 @@ def _schedule_refresh_retry(self):
self.retries += 1
# exponential backoff with jitter
delay = random.uniform(0, min(2**self.retries, RETRY_BACKOFF_MAX_SEC))
_LOGGER.warning("Token refresh failed, trying again in %s seconds",
delay)
_LOGGER.warning("Token refresh failed, trying again in %s seconds", delay)
entry_data = self.hass.data[DOMAIN][self.entry.entry_id]
entry_data[CONF_CANCEL_TOKEN_REFRESH_CALLBACK] = async_call_later(
hass=self.hass,
Expand Down Expand Up @@ -416,7 +422,7 @@ async def _update_callback(self, device, message):
data = message["data"]
await self._data_to_extra_state_attributes(data)
_LOGGER.debug("Message for device %s", device)
self.schedule_update_ha_state()
self.async_write_ha_state()

async def _data_to_extra_state_attributes(self, data) -> None:
"""Load data from Websocket update into extra_state_attributes."""
Expand Down
160 changes: 9 additions & 151 deletions custom_components/control4/alarm_control_panel.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
"""Platform for Control4 Alarm Control Panel."""
from __future__ import annotations

from datetime import timedelta
import json
import logging

from pyControl4.alarm import C4SecurityPanel
from pyControl4.error_handling import C4Exception
import voluptuous

from homeassistant.components.alarm_control_panel import (
FORMAT_NUMBER,
SUPPORT_ALARM_ARM_AWAY,
SUPPORT_ALARM_ARM_HOME,
AlarmControlPanelEntity,
)
from homeassistant.components.alarm_control_panel.const import (
SUPPORT_ALARM_ARM_CUSTOM_BYPASS,
SUPPORT_ALARM_ARM_NIGHT,
SUPPORT_ALARM_ARM_VACATION,
AlarmControlPanelEntityFeature,
CodeFormat,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand All @@ -34,7 +26,6 @@
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from . import Control4Entity, get_items_of_category
from .const import (
Expand Down Expand Up @@ -86,106 +77,6 @@
}


# async def async_setup_entry(
# hass: HomeAssistant, entry: ConfigEntry, async_add_entities
# ):
# """Set up Control4 alarm control panels from a config entry."""
# entry_data = hass.data[DOMAIN][entry.entry_id]
# scan_interval = entry_data[CONF_SCAN_INTERVAL]
# _LOGGER.debug(
# "Scan interval = %s",
# scan_interval,
# )

# # Register alarm_control_panel specific service
# platform = entity_platform.current_platform.get()
# platform.async_register_entity_service(
# "send_alarm_keystrokes",
# {voluptuous.Required("keystrokes"): cv.string},
# "send_alarm_keystrokes",
# )

# async def async_update_data():
# """Fetch data from Control4 director for alarm control panels."""
# variables = ","
# variables = variables.join(
# [
# CONTROL4_ARMED_AWAY_VAR,
# CONTROL4_ARMED_HOME_VAR,
# CONTROL4_DISARMED_VAR,
# CONTROL4_ALARM_STATE_VAR,
# CONTROL4_DISPLAY_TEXT_VAR,
# CONTROL4_TROUBLE_TEXT_VAR,
# CONTROL4_PARTITION_STATE_VAR,
# CONTROL4_DELAY_TIME_REMAINING_VAR,
# CONTROL4_OPEN_ZONE_COUNT_VAR,
# CONTROL4_ALARM_TYPE_VAR,
# CONTROL4_ARMED_TYPE_VAR,
# CONTROL4_LAST_EMERGENCY_VAR,
# CONTROL4_LAST_ARM_FAILURE_VAR,
# ]
# )
# try:
# return await director_update_data_multi_variable(hass, entry, variables)
# except C4Exception as err:
# raise UpdateFailed(f"Error communicating with API: {err}") from err

# coordinator = DataUpdateCoordinator(
# hass,
# _LOGGER,
# name="alarm_control_panel",
# update_method=async_update_data,
# update_interval=timedelta(seconds=scan_interval),
# )

# # Fetch initial data so we have data when entities subscribe
# await coordinator.async_refresh()

# items_of_category = await get_items_of_category(hass, entry, CONTROL4_CATEGORY)
# director = entry_data[CONF_DIRECTOR]
# for item in items_of_category:
# if (
# item["type"] == CONTROL4_ENTITY_TYPE
# and item["control"] == CONTROL4_CATEGORY
# ):
# item_name = item["name"]
# item_id = item["id"]
# item_parent_id = item["parentId"]
# item_coordinator = coordinator

# item_setup_info = await director.getItemSetup(item_id)
# item_setup_info = json.loads(item_setup_info)
# item_enabled = item_setup_info["setup"]["enabled"]

# item_manufacturer = None
# item_device_name = None
# item_model = None

# for parent_item in items_of_category:
# if parent_item["id"] == item_parent_id:
# item_manufacturer = parent_item["manufacturer"]
# item_device_name = parent_item["name"]
# item_model = parent_item["model"]
# break
# async_add_entities(
# [
# Control4AlarmControlPanel(
# entry_data,
# entry,
# item_coordinator,
# item_name,
# item_id,
# item_device_name,
# item_manufacturer,
# item_model,
# item_parent_id,
# item_enabled,
# )
# ],
# True,
# )


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
):
Expand Down Expand Up @@ -328,7 +219,7 @@ async def _update_callback(self, device, message):
else:
await self._data_to_extra_state_attributes(data)
_LOGGER.debug("Message for device %s", device)
self.schedule_update_ha_state()
self.async_write_ha_state()

def create_api_object(self):
"""Create a pyControl4 device object.
Expand All @@ -345,25 +236,25 @@ def entity_registry_enabled_default(self) -> bool:
@property
def code_format(self):
"""Regex for code format or None if no code is required."""
return FORMAT_NUMBER
return CodeFormat.NUMBER

@property
def supported_features(self) -> int:
"""Flag supported features."""
flags = 0
if not self.entry_data[CONF_ALARM_AWAY_MODE] == DEFAULT_ALARM_AWAY_MODE:
flags |= SUPPORT_ALARM_ARM_AWAY
flags |= AlarmControlPanelEntityFeature.ARM_AWAY
if not self.entry_data[CONF_ALARM_HOME_MODE] == DEFAULT_ALARM_HOME_MODE:
flags |= SUPPORT_ALARM_ARM_HOME
flags |= AlarmControlPanelEntityFeature.ARM_HOME
if not self.entry_data[CONF_ALARM_NIGHT_MODE] == DEFAULT_ALARM_NIGHT_MODE:
flags |= SUPPORT_ALARM_ARM_NIGHT
flags |= AlarmControlPanelEntityFeature.ARM_NIGHT
if (
not self.entry_data[CONF_ALARM_CUSTOM_BYPASS_MODE]
== DEFAULT_ALARM_CUSTOM_BYPASS_MODE
):
flags |= SUPPORT_ALARM_ARM_CUSTOM_BYPASS
flags |= AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS
if not self.entry_data[CONF_ALARM_VACATION_MODE] == DEFAULT_ALARM_VACATION_MODE:
flags |= SUPPORT_ALARM_ARM_VACATION
flags |= AlarmControlPanelEntityFeature.ARM_VACATION
return flags

@property
Expand Down Expand Up @@ -396,39 +287,6 @@ def state(self):
if alarm_state:
return STATE_ALARM_TRIGGERED

# disarmed = self.extra_state_attributes[CONTROL4_DISARMED_VAR]
# armed_home = self.extra_state_attributes[CONTROL4_ARMED_HOME_VAR]
# armed_away = self.extra_state_attributes[CONTROL4_ARMED_AWAY_VAR]
# if disarmed == 1:
# return STATE_ALARM_DISARMED
# if armed_home == 1:
# return STATE_ALARM_ARMED_HOME
# if armed_away == 1:
# return STATE_ALARM_ARMED_AWAY

# @property
# def device_state_attributes(self):
# """Return the state attributes."""
# state_attr = {}
# all_vars = [
# CONTROL4_DISPLAY_TEXT_VAR,
# CONTROL4_TROUBLE_TEXT_VAR,
# CONTROL4_PARTITION_STATE_VAR,
# CONTROL4_DELAY_TIME_REMAINING_VAR,
# CONTROL4_OPEN_ZONE_COUNT_VAR,
# CONTROL4_ALARM_STATE_VAR,
# CONTROL4_ALARM_TYPE_VAR,
# CONTROL4_ARMED_TYPE_VAR,
# CONTROL4_LAST_EMERGENCY_VAR,
# CONTROL4_LAST_ARM_FAILURE_VAR,
# ]
# for var in all_vars:
# state_attr[var.lower()] = self.coordinator.data[self._idx][var]
# state_attr[CONTROL4_ALARM_STATE_VAR.lower()] = bool(
# self.coordinator.data[self._idx][CONTROL4_ALARM_STATE_VAR]
# )
# return state_attr

async def async_alarm_arm_away(self, code=None):
"""Send arm away command."""
c4_alarm = self.create_api_object()
Expand Down
Loading

0 comments on commit ec50ba8

Please sign in to comment.