Skip to content

Commit

Permalink
Add capability check
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 28, 2023
1 parent c690069 commit b9d0872
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom_components/ohme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 8 additions & 2 deletions custom_components/ohme/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand Down
21 changes: 15 additions & 6 deletions custom_components/ohme/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b9d0872

Please sign in to comment.