Skip to content

Commit

Permalink
configurable refresh delay
Browse files Browse the repository at this point in the history
  • Loading branch information
ucpy7374 committed Apr 10, 2024
1 parent 50ccd9e commit 4fb39b2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
11 changes: 8 additions & 3 deletions custom_components/deltadore_tydom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.exceptions import ConfigEntryNotReady

from . import hub
from .const import DOMAIN, CONF_TYDOM_PASSWORD, CONF_ZONES_HOME, CONF_ZONES_AWAY
from .const import DOMAIN, CONF_TYDOM_PASSWORD, CONF_ZONES_HOME, CONF_ZONES_AWAY, CONF_REFRESH_INTERVAL

# List of platforms to support. There should be a matching .py file for each,
# eg <cover.py> and <sensor.py>
Expand Down Expand Up @@ -41,13 +41,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
pin = None
if CONF_PIN in entry.data:
pin = entry.data[CONF_PIN]

refresh_interval = "30"
if CONF_REFRESH_INTERVAL in entry.data:
refresh_interval = entry.data[CONF_REFRESH_INTERVAL]

tydom_hub = hub.Hub(
hass,
entry,
entry.data[CONF_HOST],
entry.data[CONF_MAC],
entry.data[CONF_TYDOM_PASSWORD],
refresh_interval,
zone_home,
zone_away,
pin,
Expand All @@ -69,7 +74,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
target=tydom_hub.refresh_data_1s(), hass=hass, name="Tydom refresh data 1s"
)
entry.async_create_background_task(
target=tydom_hub.refresh_data_5m(), hass=hass, name="Tydom refresh data 5m"
target=tydom_hub.refresh_data(), hass=hass, name="Tydom refresh data"
)

except Exception as err:
Expand All @@ -95,4 +100,4 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener."""
tydom_hub = hass.data[DOMAIN][entry.entry_id]
tydom_hub.update_config(entry.data[CONF_ZONES_HOME], entry.data[CONF_ZONES_AWAY])
tydom_hub.update_config(entry.data[CONF_REFRESH_INTERVAL], entry.data[CONF_ZONES_HOME], entry.data[CONF_ZONES_AWAY])
35 changes: 30 additions & 5 deletions custom_components/deltadore_tydom/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.components import dhcp

from .const import DOMAIN, LOGGER, CONF_TYDOM_PASSWORD, CONF_ZONES_HOME, CONF_ZONES_AWAY
from .const import DOMAIN, LOGGER, CONF_TYDOM_PASSWORD, CONF_ZONES_HOME, CONF_ZONES_AWAY, CONF_REFRESH_INTERVAL
from . import hub
from .tydom.tydom_client import (
TydomClientApiClientCommunicationError,
Expand All @@ -29,6 +29,7 @@
vol.Required(CONF_MAC): cv.string,
vol.Required(CONF_EMAIL): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_REFRESH_INTERVAL): cv.string,
vol.Optional(CONF_ZONES_HOME): cv.string,
vol.Optional(CONF_ZONES_AWAY): cv.string,
vol.Optional(CONF_PIN): str,
Expand Down Expand Up @@ -74,6 +75,11 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:

if len(data[CONF_PASSWORD]) < 3:
raise InvalidPassword

try:
int(data[CONF_REFRESH_INTERVAL])
except ValueError:
raise InvalidRefreshInterval

if CONF_ZONES_HOME in data and not zones_valid(data[CONF_ZONES_HOME]):
raise InvalidZoneHome
Expand Down Expand Up @@ -106,6 +112,7 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
CONF_MAC: data[CONF_MAC],
CONF_EMAIL: data[CONF_EMAIL],
CONF_PASSWORD: data[CONF_PASSWORD],
CONF_REFRESH_INTERVAL: data[CONF_REFRESH_INTERVAL],
CONF_TYDOM_PASSWORD: password,
CONF_ZONES_HOME: zone_home,
CONF_ZONES_AWAY: zone_away,
Expand Down Expand Up @@ -153,6 +160,7 @@ async def async_step_user(self, user_input=None) -> config_entries.FlowResult:
user_input[CONF_HOST],
user_input[CONF_MAC],
user_input[CONF_TYDOM_PASSWORD],
"-1",
None,
None,
None,
Expand All @@ -179,6 +187,8 @@ async def async_step_user(self, user_input=None) -> config_entries.FlowResult:
except InvalidPassword:
_errors[CONF_PASSWORD] = "invalid_password"
LOGGER.error("Invalid password")
except InvalidRefreshInterval:
_errors[CONF_REFRESH_INTERVAL] = "invalid_refresh_interval"
except InvalidZoneHome:
_errors[CONF_ZONES_HOME] = "invalid_zone_config"
default_zone_home = ""
Expand Down Expand Up @@ -228,6 +238,7 @@ async def async_step_user(self, user_input=None) -> config_entries.FlowResult:
vol.Required(
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD)
): cv.string,
vol.Required(CONF_REFRESH_INTERVAL, default="30"): cv.string,
vol.Optional(CONF_ZONES_HOME, default=default_zone_home): str,
vol.Optional(CONF_ZONES_AWAY, default=default_zone_away): str,
vol.Optional(CONF_PIN, default=user_input.get(CONF_PIN, "")): str,
Expand Down Expand Up @@ -284,6 +295,8 @@ async def async_step_discovery_confirm(self, user_input=None):
_errors[CONF_EMAIL] = "invalid_email"
except InvalidPassword:
_errors[CONF_PASSWORD] = "invalid_password"
except InvalidRefreshInterval:
_errors[CONF_REFRESH_INTERVAL] = "invalid_refresh_interval"
except InvalidZoneHome:
_errors[CONF_ZONES_HOME] = "invalid_zone_config"
except InvalidZoneAway:
Expand Down Expand Up @@ -324,6 +337,7 @@ async def async_step_discovery_confirm(self, user_input=None):
vol.Required(
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD)
): cv.string,
vol.Required(CONF_REFRESH_INTERVAL, default=user_input.get(CONF_REFRESH_INTERVAL, "30")): str,
vol.Optional(CONF_ZONES_HOME, default=user_input.get(CONF_ZONES_HOME, "")): str,
vol.Optional(CONF_ZONES_AWAY, default=user_input.get(CONF_ZONES_AWAY, "")): str,
vol.Optional(CONF_PIN, default=user_input.get(CONF_PIN, "")): str,
Expand Down Expand Up @@ -354,22 +368,32 @@ async def async_step_init(
_errors = {}
default_zone_home = ""
default_zone_away = ""
default_refresh_interval = "30"
if CONF_ZONES_HOME in self.config_entry.data:
default_zone_home = self.config_entry.data[CONF_ZONES_HOME]

if CONF_ZONES_AWAY in self.config_entry.data:
default_zone_away = self.config_entry.data[CONF_ZONES_AWAY]

if CONF_REFRESH_INTERVAL in self.config_entry.data:
default_refresh_interval = self.config_entry.data[CONF_REFRESH_INTERVAL]

if user_input is not None:
default_zone_home = user_input.get(CONF_ZONES_HOME, "")
default_zone_away = user_input.get(CONF_ZONES_AWAY, "")
default_refresh_interval = user_input.get(CONF_REFRESH_INTERVAL, "30")

try:
if CONF_ZONES_HOME in user_input and not zones_valid(user_input[CONF_ZONES_HOME]):
raise InvalidZoneHome

if CONF_ZONES_AWAY in user_input and not zones_valid(user_input[CONF_ZONES_AWAY]):
raise InvalidZoneAway

try:
int(user_input[CONF_REFRESH_INTERVAL])
except ValueError:
raise InvalidRefreshInterval

user_input[CONF_HOST] = self.config_entry.data[CONF_HOST]
user_input[CONF_MAC] = self.config_entry.data[CONF_MAC]
Expand All @@ -385,6 +409,8 @@ async def async_step_init(
)
return self.async_create_entry(title="", data={})

except InvalidRefreshInterval:
_errors[CONF_REFRESH_INTERVAL] = "invalid_refresh_interval"
except InvalidZoneHome:
_errors[CONF_ZONES_HOME] = "invalid_zone_config"
default_zone_home = ""
Expand All @@ -398,6 +424,7 @@ async def async_step_init(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(CONF_REFRESH_INTERVAL, description={"suggested_value": default_refresh_interval}): str,
vol.Optional(CONF_ZONES_HOME, description={"suggested_value": default_zone_home}): str,
vol.Optional(CONF_ZONES_AWAY, description={"suggested_value": default_zone_away}): str,
}
Expand All @@ -408,19 +435,15 @@ async def async_step_init(
class CannotConnect(exceptions.HomeAssistantError):
"""Error to indicate we cannot connect."""


class InvalidHost(exceptions.HomeAssistantError):
"""Error to indicate there is an invalid hostname."""


class InvalidMacAddress(exceptions.HomeAssistantError):
"""Error to indicate there is an invalid Mac address."""


class InvalidEmail(exceptions.HomeAssistantError):
"""Error to indicate there is an invalid Email."""


class InvalidPassword(exceptions.HomeAssistantError):
"""Error to indicate there is an invalid Password."""

Expand All @@ -430,3 +453,5 @@ class InvalidZoneHome(exceptions.HomeAssistantError):
class InvalidZoneAway(exceptions.HomeAssistantError):
"""Error to indicate the Zones Away config is not valid."""

class InvalidRefreshInterval(exceptions.HomeAssistantError):
"""Error to indicate the refresh interval is not valid."""
1 change: 1 addition & 0 deletions custom_components/deltadore_tydom/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
NAME = "Delta Dore TYDOM"

CONF_TYDOM_PASSWORD = "tydom_password"
CONF_REFRESH_INTERVAL= "refresh_interval"
CONF_ZONES_HOME = "zones_home"
CONF_ZONES_AWAY = "zones_away"
15 changes: 11 additions & 4 deletions custom_components/deltadore_tydom/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(
host: str,
mac: str,
password: str,
refresh_interval: str,
zone_home: str,
zone_away: str,
alarmpin: str,
Expand All @@ -60,6 +61,7 @@ def __init__(
self._host = host
self._mac = mac
self._pass = password
self._refresh_interval = int(refresh_interval)*60
self._zone_home = zone_home
self._zone_away = zone_away
self._pin = alarmpin
Expand Down Expand Up @@ -91,9 +93,10 @@ def __init__(

self.online = True

def update_config(self, zone_home, zone_away):
def update_config(self, refresh_interval, zone_home, zone_away):
"""Update zone configuration."""
self._tydom_client.update_config(zone_home, zone_away)
self._refresh_interval = int(refresh_interval)*60
self._zone_home = zone_home
self._zone_away = zone_away

Expand Down Expand Up @@ -307,8 +310,12 @@ async def refresh_data_1s(self) -> None:
await self._tydom_client.poll_devices_data_1s()
await asyncio.sleep(1)

async def refresh_data_5m(self) -> None:
async def refresh_data(self) -> None:
"""Periodically refresh data for devices which don't do push."""
while True:
await self._tydom_client.poll_devices_data_5m()
await asyncio.sleep(1800)
if(self._refresh_interval > 0):
await self._tydom_client.poll_devices_data_5m()
await asyncio.sleep(self._refresh_interval)
else:
await asyncio.sleep(60)

5 changes: 5 additions & 0 deletions custom_components/deltadore_tydom/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"mac": "MAC address",
"email": "Email",
"password": "Password",
"refresh_interval": "Refresh interval in min",
"zones_away": "Active zones in away alarm mode",
"zones_home": "Active zones in home alarm mode",
"pin": "Alarm PIN"
Expand All @@ -22,6 +23,7 @@
"mac": "MAC address",
"email": "Email",
"password": "Password",
"refresh_interval": "Refresh interval in min",
"zones_away": "Active zones in away alarm mode",
"zones_home": "Active zones in home alarm mode",
"pin": "Alarm PIN"
Expand All @@ -34,6 +36,7 @@
"invalid_macaddress": "MAC address is invalid",
"invalid_email": "Email is invalid",
"invalid_password": "Password is invalid",
"invalid_refresh_interval": "Refresh interval is not valid",
"communication_error": "Couldn't connect to Tydom Gateway",
"invalid_zone_config": "Zone configuration is invalid. syntax is : zone_id_1,zone_id_2... Example: 1,2,4"
},
Expand All @@ -52,12 +55,14 @@
"title": "Delta Dore Tydom Configuration",
"description": "If you need help with the configuration go to: https://github.com/CyrilP/hass-deltadore-tydom-component",
"data": {
"refresh_interval": "Refresh interval in min",
"zones_away": "Active zones in away alarm mode",
"zones_home": "Active zones in home alarm mode"
}
}
},
"error": {
"invalid_refresh_interval": "Refresh interval is not valid",
"invalid_zone_config": "Zone configuration is invalid. syntax is : zone_id_1,zone_id_2... Example: 1,2,4"
}
}
Expand Down

0 comments on commit 4fb39b2

Please sign in to comment.