Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for chargers that are not reachable as installations #20

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions custom_components/zaptec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ async def push_update_to_charger(t):
for circuits in ins.circuits:
for charger in circuits.chargers:
await charger.state()

for charger in acc.stand_alone_chargers:
await charger.state()


async_dispatcher_send(hass, EVENT_NEW_DATA)

async_track_time_interval(hass, push_update_to_charger, timedelta(seconds=60))
Expand Down Expand Up @@ -87,6 +92,7 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):

if unload_ok:
acc = hass.data[DOMAIN]["api"]
# no need to unload stand-alone chargers
await asyncio.gather(*[i.cancel_stream() for i in acc.installs])
hass.data.pop(DOMAIN)

Expand Down
31 changes: 20 additions & 11 deletions custom_components/zaptec/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def __init__(self, username, password, client=None):
self._token_info = {}
self._access_token = None
self.installs = []
self.stand_alone_chargers = []
# Map using the id t lookup
self.map = {}
self.obs = {}
Expand Down Expand Up @@ -444,8 +445,11 @@ async def build(self):

self.installs = cls_installs

return cls_installs

so_chargers = await self.chargers()
for charger in so_chargers:
if charger.id not in self.map:
self.map[charger.id] = charger
self.stand_alone_chargers = so_chargers

class Charger(ZapBase):
def __init__(self, data, account):
Expand Down Expand Up @@ -574,15 +578,16 @@ async def state(self):
# I couldn't find a way to see if it was up to date..
# maybe remove this later if it dont interest ppl.

firmware_info = await self._account.charger_firmware(self.installation_id)
for fm in firmware_info:
if fm["ChargerId"] == self.id:
fixed = {
"current_firmware_version": fm["CurrentVersion"],
"available_firmware_version": fm["AvailableVersion"],
"firmware_update_to_date": fm["IsUpToDate"],
}
self.set_attributes(fixed)
if self.installation_id in self._account.map:
firmware_info = await self._account.charger_firmware(self.installation_id)
for fm in firmware_info:
if fm["ChargerId"] == self.id:
fixed = {
"current_firmware_version": fm["CurrentVersion"],
"available_firmware_version": fm["AvailableVersion"],
"firmware_update_to_date": fm["IsUpToDate"],
}
self.set_attributes(fixed)

async def live(self):
# This don't seems to be documented but the portal uses it
Expand Down Expand Up @@ -622,4 +627,8 @@ async def cb(data):
print(data)
# await ins._stream(cb=cb)

for charger in acc.stand_alone_chargers:
data = await charger.state()
print(data)

asyncio.run(gogo())
14 changes: 11 additions & 3 deletions custom_components/zaptec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ async def cb(data):
sensors.append(chs)
sensors.append(InstallationSensor(ins))

for charger in acc.stand_alone_chargers:
# _LOGGER.debug("Building charger %s", charger)
# Force an update before its added.
await charger.state()
chs = ChargerSensor(charger, hass)
sensors.append(chs)


async_add_entities(sensors, False)

return True
Expand Down Expand Up @@ -86,7 +94,7 @@ def name(self) -> str:
return "zaptec_circute_%s" % self._api._attrs["id"]

@property
def device_state_attributes(self) -> dict:
def extra_state_attributes(self) -> dict:
return self._attrs

@property
Expand Down Expand Up @@ -121,7 +129,7 @@ def name(self) -> str:
return "zaptec_installation_%s" % self._attrs["id"]

@property
def device_state_attributes(self) -> dict:
def extra_state_attributes(self) -> dict:
return self._attrs

@property
Expand Down Expand Up @@ -193,7 +201,7 @@ def device_info(self):
}

@property
def device_state_attributes(self) -> dict:
def extra_state_attributes(self) -> dict:
return self._attrs

async def async_added_to_hass(self):
Expand Down
8 changes: 4 additions & 4 deletions custom_components/zaptec/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ async def async_setup_platform(
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
) -> bool: # pylint: disable=unused-argument
"""Setup switch platform."""
api = hass.data.get(DOMAIN, {}).get("api")
if api is None:
acc = hass.data[DOMAIN]["api"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use .get like I did before. This will raise and exception and not return None

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if acc is None:
_LOGGER.debug("Didn't setup switch the api wasnt ready")
return False

switches = []
chargers = await api.chargers()
chargers = acc.all_chargers

for c in chargers:
switches.append(Switch(c))
Expand Down Expand Up @@ -76,6 +76,6 @@ def is_on(self) -> bool:
return True if self._mode == "charging" else False

@property
def device_state_attributes(self) -> dict:
def extra_state_attributes(self) -> dict:
"""Return the state attributes."""
return self._attr