Skip to content

Commit

Permalink
Merge pull request #107 from Disane87/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Disane87 authored Apr 1, 2024
2 parents 2a31e03 + 23e1fbe commit 2a76638
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## [0.4.2](https://github.com/Disane87/spoolman-homeassistant/compare/v0.4.1...v0.4.2) (2024-03-18)

## [0.4.2-dev.2](https://github.com/Disane87/spoolman-homeassistant/compare/v0.4.2-dev.1...v0.4.2-dev.2) (2024-03-30)

### 🛠️ Fixes
* Fixes an issue when there is no active spool in klipper ([eda5f32](https://github.com/Disane87/spoolman-homeassistant/commit/eda5f329dd7708c2b2e997923acc7f966cd6504e)), closes [#105](https://github.com/Disane87/spoolman-homeassistant/issues/105)


## [0.4.2](https://github.com/Disane87/spoolman-homeassistant/compare/v0.4.1...v0.4.2) (2024-03-18)
### 🛠️ Fixes
* :bug: Fixes a bug where klipper url wasn't checked correctly which leads to an exception ([441e4e5](https://github.com/Disane87/spoolman-homeassistant/commit/441e4e58ec25143fe7f0b0989d61c08b5168864f)), closes [#100](https://github.com/Disane87/spoolman-homeassistant/issues/100)

## [0.4.2-dev.1](https://github.com/Disane87/spoolman-homeassistant/compare/v0.4.1...v0.4.2-dev.1) (2024-03-18)
Expand Down
9 changes: 7 additions & 2 deletions custom_components/spoolman/classes/klipper_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ def __init__(self, base_url):
"""Initialize the Klipper API."""
self.base_url = add_trailing_slash(base_url)

async def get_active_spool_id(self) -> int:
async def get_active_spool_id(self) -> int | None:
"""Get active spool from Klipper API."""
async with aiohttp.ClientSession() as session:
url = f"{self.base_url}server/spoolman/spool_id"
async with session.get(url) as response:
if response.status == 200:
data = await response.json()
return int(data.get('result').get("spool_id"))
# Extract the spool_id from the data dictionary
spool_id = data.get('result', {}).get("spool_id")

# Convert spool_id to an integer if it is not None; otherwise, return None
return int(spool_id) if spool_id is not None else None

else:
return 0
async def api_version(self) -> str | None:
Expand Down
13 changes: 7 additions & 6 deletions custom_components/spoolman/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ async def _async_update_data(self):

klipper_url = config.get(KLIPPER_URL, "")
if klipper_url is not None and klipper_url != "":
klipper_active_spool: int = await KlipperAPI(klipper_url).get_active_spool_id()
for spool in spools:
if spool["id"] == klipper_active_spool:
spool["klipper_active_spool"] = True
else:
spool["klipper_active_spool"] = False
klipper_active_spool: int | None = await KlipperAPI(klipper_url).get_active_spool_id()
if klipper_active_spool is not None:
for spool in spools:
if spool["id"] == klipper_active_spool:
spool["klipper_active_spool"] = True
else:
spool["klipper_active_spool"] = False

return spools

Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "Spoolman",
"homeassistant": "2023.9.0",
"render_readme": true,
"filename": "spoolman-homeassistant_0.4.2.zip",
"filename": "spoolman-homeassistant_0.4.2-dev.2.zip",
"zip_release": true
}

0 comments on commit 2a76638

Please # to comment.