Skip to content

Commit

Permalink
Merge pull request #40 from RobertD502/add_timezone_option
Browse files Browse the repository at this point in the history
Add timezone option to config flow & improve BLE relay
  • Loading branch information
RobertD502 authored Sep 23, 2023
2 parents 2f34a5d + 59b1bcb commit ad09c65
Show file tree
Hide file tree
Showing 8 changed files with 701 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ___

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


## Important - Please Read:
### Note About PetKit Account & Family Sharing Feature:

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

### Automatic Option
Click on the button below to add the integration:
Click on the button below to add the integration. Be sure to read #4 and #5 below.

[![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)

Expand All @@ -116,6 +117,7 @@ Click on the button below to add the integration:
2. Click the `+ ADD INTEGRATION` button in the lower right-hand corner
3. Search for `PetKit`
4. Be sure to select the country associated with your account (Hong Kong users should select Hong Kong, not China).
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.

**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.**

Expand Down
30 changes: 27 additions & 3 deletions custom_components/petkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant

from .const import DOMAIN, LOGGER, PETKIT_COORDINATOR, PLATFORMS, POLLING_INTERVAL, REGION, UPDATE_LISTENER
from .const import DOMAIN, LOGGER, PETKIT_COORDINATOR, PLATFORMS, POLLING_INTERVAL, REGION, TIMEZONE, UPDATE_LISTENER
from .coordinator import PetKitDataUpdateCoordinator
from .util import async_validate_api, NoDevicesError

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

LOGGER.debug('Migrating PetKit config entry')
entry.version = 4
entry.version = 5

hass.config_entries.async_update_entry(
entry,
Expand All @@ -54,6 +54,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
},
options={
REGION: None,
TIMEZONE: "Set Automatically",
POLLING_INTERVAL: 120,
},
)
Expand All @@ -65,7 +66,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
polling_interval = entry.options[POLLING_INTERVAL]

LOGGER.debug('Migrating PetKit config entry')
entry.version = 4
entry.version = 5

hass.config_entries.async_update_entry(
entry,
Expand All @@ -75,11 +76,34 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
},
options={
REGION: None,
TIMEZONE: "Set Automatically",
POLLING_INTERVAL: polling_interval,
},
)
LOGGER.error("PetKit API has changed. Please reauthenticate and select your country.")

if entry.version == 4:
email = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]
region = entry.options[REGION]
polling_interval = entry.options[POLLING_INTERVAL]

LOGGER.debug('Migrating PetKit config entry')
entry.version = 5

hass.config_entries.async_update_entry(
entry,
data={
CONF_EMAIL: email,
CONF_PASSWORD: password,
},
options={
REGION: region,
TIMEZONE: "Set Automatically",
POLLING_INTERVAL: polling_interval,
},
)

return True

async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
Expand Down
24 changes: 20 additions & 4 deletions custom_components/petkit/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from homeassistant.helpers import selector
import homeassistant.helpers.config_validation as cv

from .const import DEFAULT_NAME, DOMAIN, POLLING_INTERVAL, REGION, REGIONS_LIST
from .const import DEFAULT_NAME, DOMAIN, POLLING_INTERVAL, REGION, REGIONS_LIST, TIMEZONE
from .timezones import TIMEZONES
from .util import NoDevicesError, async_validate_api


Expand All @@ -24,6 +25,9 @@
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(REGION): selector.SelectSelector(
selector.SelectSelectorConfig(options=REGIONS_LIST),
),
vol.Required(TIMEZONE): selector.SelectSelector(
selector.SelectSelectorConfig(options=TIMEZONES),
)

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

VERSION = 4
VERSION = 5

entry: config_entries.ConfigEntry | None

Expand Down Expand Up @@ -62,9 +66,10 @@ async def async_step_reauth_confirm(
email = user_input[CONF_EMAIL]
password = user_input[CONF_PASSWORD]
region = user_input[REGION] if REGION else None
timezone = user_input[TIMEZONE]

try:
await async_validate_api(self.hass, email, password, region)
await async_validate_api(self.hass, email, password, region, timezone)
except RegionError:
errors["base"] = "region_error"
except TimezoneError:
Expand All @@ -91,6 +96,7 @@ async def async_step_reauth_confirm(
},
options={
REGION: region,
TIMEZONE: timezone,
POLLING_INTERVAL: self.entry.options[POLLING_INTERVAL],
}
)
Expand All @@ -115,9 +121,10 @@ async def async_step_user(
email = user_input[CONF_EMAIL]
password = user_input[CONF_PASSWORD]
region = user_input[REGION] if REGION else None
timezone = user_input[TIMEZONE]

try:
await async_validate_api(self.hass, email, password, region)
await async_validate_api(self.hass, email, password, region, timezone)
except RegionError:
errors["base"] = "region_error"
except TimezoneError:
Expand All @@ -144,6 +151,7 @@ async def async_step_user(
},
options={
REGION: region,
TIMEZONE: timezone,
POLLING_INTERVAL: 120,
}
)
Expand Down Expand Up @@ -180,6 +188,14 @@ async def async_step_petkit_options(self, user_input=None):
): selector.SelectSelector(
selector.SelectSelectorConfig(options=REGIONS_LIST)
),
vol.Required(
TIMEZONE,
default=self.config_entry.options.get(
TIMEZONE, "Set Automatically"
),
): selector.SelectSelector(
selector.SelectSelectorConfig(options=TIMEZONES)
),
vol.Required(
POLLING_INTERVAL,
default=self.config_entry.options.get(
Expand Down
1 change: 1 addition & 0 deletions custom_components/petkit/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
PETKIT_COORDINATOR = "petkit_coordinator"
POLLING_INTERVAL = "polling_interval"
TIMEOUT = 20
TIMEZONE = "timezone"
UPDATE_LISTENER = "update_listener"

PETKIT_ERRORS = (
Expand Down
7 changes: 6 additions & 1 deletion custom_components/petkit/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN, LOGGER, POLLING_INTERVAL, REGION, TIMEOUT
from .const import DOMAIN, LOGGER, POLLING_INTERVAL, REGION, TIMEOUT, TIMEZONE


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

if entry.options[TIMEZONE] == "Set Automatically":
tz = None
else:
tz = entry.options[TIMEZONE]
try:
self.client = PetKitClient(
entry.data[CONF_EMAIL],
entry.data[CONF_PASSWORD],
session=async_get_clientsession(hass),
region=entry.options[REGION],
timezone=tz,
timeout=TIMEOUT,
)
super().__init__(
Expand Down
4 changes: 2 additions & 2 deletions custom_components/petkit/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/RobertD502/home-assistant-petkit/issues",
"requirements": [
"petkitaio==0.1.9",
"petkitaio==0.1.10",
"tzlocal>=4.2"
],
"version": "0.1.9.1"
"version": "0.1.10"
}
Loading

0 comments on commit ad09c65

Please sign in to comment.