Skip to content

Commit

Permalink
Update fyta_connector.py
Browse files Browse the repository at this point in the history
handle if no plant data in dict
  • Loading branch information
dontinelli authored Mar 26, 2024
1 parent 89d79f9 commit 47a90e7
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/fyta_cli/fyta_connector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Connector class to manage access to FYTA API."""

from datetime import datetime, timezone
from datetime import datetime
from typing import Any
from zoneinfo import ZoneInfo

Expand Down Expand Up @@ -29,7 +29,8 @@ def __init__(
access_token: str = "",
expiration: datetime | None = None,
tz: str = "",
):
) -> None:
"""Initialize connector class."""
self.email: str = email
self.password: str = password
self.client = Client(email, password, access_token, expiration)
Expand All @@ -38,7 +39,7 @@ def __init__(
self.plants: dict[int, dict[str, Any]] = {}
self.access_token: str = access_token
self.expiration: datetime | None = expiration
self.timezone: ZoneInfo = timezone.utc if tz == "" else ZoneInfo(tz)
self.timezone: ZoneInfo = datetime.UTC if tz == "" else ZoneInfo(tz)

async def test_connection(self) -> bool:
"""Test if connection to FYTA API works."""
Expand Down Expand Up @@ -69,7 +70,7 @@ async def update_all_plants(self) -> dict[int, dict[str, Any]]:

plant_list = await self.update_plant_list()

for plant in plant_list.keys():
for plant in plant_list:
current_plant = await self.update_plant_data(plant)
if current_plant != {}:
plants |= {plant: current_plant}
Expand All @@ -82,13 +83,13 @@ async def update_plant_data(self, plant_id: int) -> dict[str, Any]:
"""Get data of specific plant."""

p: dict = await self.client.get_plant_data(plant_id)
plant_data: dict = p["plant"]

current_plant = {}

if plant_data["sensor"] is None:
if ("plant" not in p) or (p["plant"]["sensor"] is None):
current_plant |= {"sensor_available": False}
else:
plant_data: dict = p["plant"]
current_plant |= {"online": True}
current_plant |= {"sensor_available": True}
current_plant |= {
Expand Down Expand Up @@ -129,10 +130,10 @@ async def update_plant_data(self, plant_id: int) -> dict[str, Any]:

@property
def fyta_id(self) -> str:
"""ID for FYTA object"""
"""ID for FYTA object."""
return self.email

@property
def data(self) -> dict:
"""ID for FYTA object"""
"""ID for FYTA object."""
return self.plants

0 comments on commit 47a90e7

Please sign in to comment.