From b9d087226d4e2e32fbb7ee40097c9be3e170ea6a Mon Sep 17 00:00:00 2001 From: Daniel Raper Date: Thu, 28 Dec 2023 13:19:51 +0000 Subject: [PATCH] Add capability check --- custom_components/ohme/__init__.py | 2 +- custom_components/ohme/api_client.py | 10 ++++++++-- custom_components/ohme/switch.py | 21 +++++++++++++++------ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/custom_components/ohme/__init__.py b/custom_components/ohme/__init__.py index 0a3d31f..23625e5 100644 --- a/custom_components/ohme/__init__.py +++ b/custom_components/ohme/__init__.py @@ -15,7 +15,7 @@ async def async_setup_dependencies(hass, config): hass.data[DOMAIN][DATA_CLIENT] = client await client.async_refresh_session() - await client.async_update_account_info() + await client.async_update_device_info() async def async_setup_entry(hass, entry): diff --git a/custom_components/ohme/api_client.py b/custom_components/ohme/api_client.py index 578b345..e0a98b6 100644 --- a/custom_components/ohme/api_client.py +++ b/custom_components/ohme/api_client.py @@ -19,6 +19,7 @@ def __init__(self, email, password): self._password = password self._device_info = None + self._capabilities = {} self._token = None self._user_id = "" self._serial = "" @@ -136,8 +137,8 @@ async def async_get_account_info(self): return False return resp - - async def async_update_account_info(self, is_retry=False): + + async def async_update_device_info(self, is_retry=False): """Update _device_info with our charger model.""" resp = await self.async_get_account_info() @@ -155,12 +156,17 @@ async def async_update_account_info(self, is_retry=False): serial_number=device['id'] ) + self._capabilities = device['modelCapabilities'] self._user_id = resp['user']['id'] self._serial = device['id'] self._device_info = info return True + def is_capable(self, capability): + """Return whether or not this model has a given capability.""" + return bool(self._capabilities[capability]) + def _last_second_of_month_timestamp(self): """Get the last second of this month.""" dt = datetime.today() diff --git a/custom_components/ohme/switch.py b/custom_components/ohme/switch.py index 765c0d6..3ecce3c 100644 --- a/custom_components/ohme/switch.py +++ b/custom_components/ohme/switch.py @@ -27,14 +27,23 @@ async def async_setup_entry( accountinfo_coordinator = hass.data[DOMAIN][DATA_ACCOUNTINFO_COORDINATOR] client = hass.data[DOMAIN][DATA_CLIENT] - buttons = [OhmePauseChargeSwitch(coordinator, hass, client), - OhmeMaxChargeSwitch(coordinator, hass, client), - OhmeConfigurationSwitch(accountinfo_coordinator, hass, client, "Lock Buttons", "lock", "buttonsLocked"), - OhmeConfigurationSwitch(accountinfo_coordinator, hass, client, "Require Approval", "check-decagram", "pluginsRequireApproval"), + switches = [OhmePauseChargeSwitch(coordinator, hass, client), + OhmeMaxChargeSwitch(coordinator, hass, client)] + + if client.is_capable("buttonsLockable"): + switches.append( + OhmeConfigurationSwitch(accountinfo_coordinator, hass, client, "Lock Buttons", "lock", "buttonsLocked") + ) + if client.is_capable("pluginsRequireApprovalMode"): + switches.append( + OhmeConfigurationSwitch(accountinfo_coordinator, hass, client, "Require Approval", "check-decagram", "pluginsRequireApproval") + ) + if client.is_capable("stealth"): + switches.append( OhmeConfigurationSwitch(accountinfo_coordinator, hass, client, "Sleep When Inactive", "power-sleep", "stealthEnabled") - ] + ) - async_add_entities(buttons, update_before_add=True) + async_add_entities(switches, update_before_add=True) class OhmePauseChargeSwitch(CoordinatorEntity[OhmeChargeSessionsCoordinator], SwitchEntity):