Skip to content

Commit

Permalink
Fix compare and timeout (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Feb 20, 2022
1 parent 4cc8cd6 commit 6940bd1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions custom_components/healthchecksio/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,34 @@ async def async_setup_entry(hass, config_entry, async_add_devices):
class HealthchecksioBinarySensor(BinarySensorEntity):
"""Healthchecksio binary_sensor class."""

def __init__(self, hass, config, config_entry):
def __init__(self, hass, check_data, config_entry):
self.hass = hass
self.attr = {}
self.config_entry = config_entry
self._status = None
self.config = config
self.check_data = check_data
self.check = {}

async def async_update(self):
"""Update the binary_sensor."""
# Send update "signal" to the component
await self.hass.data[DOMAIN_DATA]["client"].update_data()

# Check the data and update the value.
for check in self.hass.data[DOMAIN_DATA]["data"]["checks"]:
for check in self.hass.data[DOMAIN_DATA].get("data", {}).get("checks", []):
if self.unique_id == check.get("ping_url").split("/")[-1]:
self.config = check
self.check = check
break
self._status = self.config.get("status") == "up"
self._status = self.check.get("status") == "up"

# Set/update attributes
self.attr["attribution"] = ATTRIBUTION
self.attr["last_ping"] = self.config.get("last_ping")
self.attr["last_ping"] = self.check.get("last_ping")

@property
def unique_id(self):
"""Return a unique ID to use for this binary_sensor."""
return self.config.get("ping_url").split("/")[-1]
return self.check_data.get("ping_url", "").split("/")[-1]

@property
def device_info(self):
Expand All @@ -67,7 +68,7 @@ def device_info(self):
@property
def name(self):
"""Return the name of the binary_sensor."""
return self.config.get("name")
return self.check.get("name")

@property
def device_class(self):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/healthchecksio/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async def _test_credentials(
verify_ssl = not self_hosted or site_root.startswith("https")
session = async_get_clientsession(self.hass, verify_ssl)
headers = {"X-Api-Key": api_key}
async with async_timeout.timeout(10, loop=asyncio.get_event_loop()):
async with async_timeout.timeout(10):
Logger("custom_components.healthchecksio").info("Checking API Key")
data = await session.get(f"{site_root}/api/v1/checks/", headers=headers)
self.hass.data[DOMAIN_DATA] = {"data": await data.json()}
Expand Down

0 comments on commit 6940bd1

Please sign in to comment.