Skip to content

Commit ad09c65

Browse files
authored
Merge pull request #40 from RobertD502/add_timezone_option
Add timezone option to config flow & improve BLE relay
2 parents 2f34a5d + 59b1bcb commit ad09c65

8 files changed

Lines changed: 701 additions & 13 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ ___
6565

6666
- **If you are running Home Assistant as a Docker container, the `TZ` environment variable must be set.**
6767

68+
6869
## Important - Please Read:
6970
### Note About PetKit Account & Family Sharing Feature:
7071

@@ -106,7 +107,7 @@ and place it inside your Home Assistant Core installation's `custom_components`
106107
## Setup
107108

108109
### Automatic Option
109-
Click on the button below to add the integration:
110+
Click on the button below to add the integration. Be sure to read #4 and #5 below.
110111

111112
[![Open your Home Assistant instance and start setting up a new integration.](https://my.home-assistant.io/badges/config_flow_start.svg)](https://my.home-assistant.io/redirect/config_flow_start/?domain=petkit)
112113

@@ -116,6 +117,7 @@ Click on the button below to add the integration:
116117
2. Click the `+ ADD INTEGRATION` button in the lower right-hand corner
117118
3. Search for `PetKit`
118119
4. Be sure to select the country associated with your account (Hong Kong users should select Hong Kong, not China).
120+
5. During setup, set the timezone option to `Set Automatically`. If the tzlocal library isn't able to fetch your timezone, please manually select your timezone.
119121

120122
**The current polling interval is set to 2 minutes (120 seconds). If you would like to set a different polling interval, change the polling interval option (via the UI). Keep in mind, setting the polling interval too short may result in your account getting rate limited/blocked.**
121123

custom_components/petkit/__init__.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
66
from homeassistant.core import HomeAssistant
77

8-
from .const import DOMAIN, LOGGER, PETKIT_COORDINATOR, PLATFORMS, POLLING_INTERVAL, REGION, UPDATE_LISTENER
8+
from .const import DOMAIN, LOGGER, PETKIT_COORDINATOR, PLATFORMS, POLLING_INTERVAL, REGION, TIMEZONE, UPDATE_LISTENER
99
from .coordinator import PetKitDataUpdateCoordinator
1010
from .util import async_validate_api, NoDevicesError
1111

@@ -44,7 +44,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
4444
password = entry.data[CONF_PASSWORD]
4545

4646
LOGGER.debug('Migrating PetKit config entry')
47-
entry.version = 4
47+
entry.version = 5
4848

4949
hass.config_entries.async_update_entry(
5050
entry,
@@ -54,6 +54,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5454
},
5555
options={
5656
REGION: None,
57+
TIMEZONE: "Set Automatically",
5758
POLLING_INTERVAL: 120,
5859
},
5960
)
@@ -65,7 +66,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
6566
polling_interval = entry.options[POLLING_INTERVAL]
6667

6768
LOGGER.debug('Migrating PetKit config entry')
68-
entry.version = 4
69+
entry.version = 5
6970

7071
hass.config_entries.async_update_entry(
7172
entry,
@@ -75,11 +76,34 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
7576
},
7677
options={
7778
REGION: None,
79+
TIMEZONE: "Set Automatically",
7880
POLLING_INTERVAL: polling_interval,
7981
},
8082
)
8183
LOGGER.error("PetKit API has changed. Please reauthenticate and select your country.")
8284

85+
if entry.version == 4:
86+
email = entry.data[CONF_EMAIL]
87+
password = entry.data[CONF_PASSWORD]
88+
region = entry.options[REGION]
89+
polling_interval = entry.options[POLLING_INTERVAL]
90+
91+
LOGGER.debug('Migrating PetKit config entry')
92+
entry.version = 5
93+
94+
hass.config_entries.async_update_entry(
95+
entry,
96+
data={
97+
CONF_EMAIL: email,
98+
CONF_PASSWORD: password,
99+
},
100+
options={
101+
REGION: region,
102+
TIMEZONE: "Set Automatically",
103+
POLLING_INTERVAL: polling_interval,
104+
},
105+
)
106+
83107
return True
84108

85109
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:

custom_components/petkit/config_flow.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from homeassistant.helpers import selector
1515
import homeassistant.helpers.config_validation as cv
1616

17-
from .const import DEFAULT_NAME, DOMAIN, POLLING_INTERVAL, REGION, REGIONS_LIST
17+
from .const import DEFAULT_NAME, DOMAIN, POLLING_INTERVAL, REGION, REGIONS_LIST, TIMEZONE
18+
from .timezones import TIMEZONES
1819
from .util import NoDevicesError, async_validate_api
1920

2021

@@ -24,6 +25,9 @@
2425
vol.Required(CONF_PASSWORD): cv.string,
2526
vol.Required(REGION): selector.SelectSelector(
2627
selector.SelectSelectorConfig(options=REGIONS_LIST),
28+
),
29+
vol.Required(TIMEZONE): selector.SelectSelector(
30+
selector.SelectSelectorConfig(options=TIMEZONES),
2731
)
2832

2933
}
@@ -33,7 +37,7 @@
3337
class PetKitConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
3438
"""Handle a config flow for PetKit integration."""
3539

36-
VERSION = 4
40+
VERSION = 5
3741

3842
entry: config_entries.ConfigEntry | None
3943

@@ -62,9 +66,10 @@ async def async_step_reauth_confirm(
6266
email = user_input[CONF_EMAIL]
6367
password = user_input[CONF_PASSWORD]
6468
region = user_input[REGION] if REGION else None
69+
timezone = user_input[TIMEZONE]
6570

6671
try:
67-
await async_validate_api(self.hass, email, password, region)
72+
await async_validate_api(self.hass, email, password, region, timezone)
6873
except RegionError:
6974
errors["base"] = "region_error"
7075
except TimezoneError:
@@ -91,6 +96,7 @@ async def async_step_reauth_confirm(
9196
},
9297
options={
9398
REGION: region,
99+
TIMEZONE: timezone,
94100
POLLING_INTERVAL: self.entry.options[POLLING_INTERVAL],
95101
}
96102
)
@@ -115,9 +121,10 @@ async def async_step_user(
115121
email = user_input[CONF_EMAIL]
116122
password = user_input[CONF_PASSWORD]
117123
region = user_input[REGION] if REGION else None
124+
timezone = user_input[TIMEZONE]
118125

119126
try:
120-
await async_validate_api(self.hass, email, password, region)
127+
await async_validate_api(self.hass, email, password, region, timezone)
121128
except RegionError:
122129
errors["base"] = "region_error"
123130
except TimezoneError:
@@ -144,6 +151,7 @@ async def async_step_user(
144151
},
145152
options={
146153
REGION: region,
154+
TIMEZONE: timezone,
147155
POLLING_INTERVAL: 120,
148156
}
149157
)
@@ -180,6 +188,14 @@ async def async_step_petkit_options(self, user_input=None):
180188
): selector.SelectSelector(
181189
selector.SelectSelectorConfig(options=REGIONS_LIST)
182190
),
191+
vol.Required(
192+
TIMEZONE,
193+
default=self.config_entry.options.get(
194+
TIMEZONE, "Set Automatically"
195+
),
196+
): selector.SelectSelector(
197+
selector.SelectSelectorConfig(options=TIMEZONES)
198+
),
183199
vol.Required(
184200
POLLING_INTERVAL,
185201
default=self.config_entry.options.get(

custom_components/petkit/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
PETKIT_COORDINATOR = "petkit_coordinator"
3030
POLLING_INTERVAL = "polling_interval"
3131
TIMEOUT = 20
32+
TIMEZONE = "timezone"
3233
UPDATE_LISTENER = "update_listener"
3334

3435
PETKIT_ERRORS = (

custom_components/petkit/coordinator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1616
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
1717

18-
from .const import DOMAIN, LOGGER, POLLING_INTERVAL, REGION, TIMEOUT
18+
from .const import DOMAIN, LOGGER, POLLING_INTERVAL, REGION, TIMEOUT, TIMEZONE
1919

2020

2121
class PetKitDataUpdateCoordinator(DataUpdateCoordinator):
@@ -26,12 +26,17 @@ class PetKitDataUpdateCoordinator(DataUpdateCoordinator):
2626
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
2727
"""Initialize the PetKit coordinator."""
2828

29+
if entry.options[TIMEZONE] == "Set Automatically":
30+
tz = None
31+
else:
32+
tz = entry.options[TIMEZONE]
2933
try:
3034
self.client = PetKitClient(
3135
entry.data[CONF_EMAIL],
3236
entry.data[CONF_PASSWORD],
3337
session=async_get_clientsession(hass),
3438
region=entry.options[REGION],
39+
timezone=tz,
3540
timeout=TIMEOUT,
3641
)
3742
super().__init__(

custom_components/petkit/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"iot_class": "cloud_polling",
1212
"issue_tracker": "https://github.com/RobertD502/home-assistant-petkit/issues",
1313
"requirements": [
14-
"petkitaio==0.1.9",
14+
"petkitaio==0.1.10",
1515
"tzlocal>=4.2"
1616
],
17-
"version": "0.1.9.1"
17+
"version": "0.1.10"
1818
}

0 commit comments

Comments
 (0)