Skip to content

Commit

Permalink
$org_location
Browse files Browse the repository at this point in the history
  • Loading branch information
zachi40 committed Jun 15, 2024
1 parent d15b40d commit 29c9d18
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion custom_components/mycodo_app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"version": "1.0.1",
"issue_tracker": "https://github.com/zachi40/home-assistant-mycodo/issues",
"loggers": ["mycodo"]
}
}
15 changes: 9 additions & 6 deletions custom_components/mycodo_app/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
try:
# Attempt to extract necessary measurement details
device_measurements = details["device measurements"][0]
# unit = device_measurements["unit"]
# if unit == "C":
# unit = TEMP_CELSIUS
unit = device_measurements.get("unit", "C")
unit = device_measurements.get("unit", "")
device_class = device_measurements["measurement"]

data = await hass.async_add_executor_job(
mycodo_client.get_sensor_data, sensor["unique_id"]
mycodo_client.get_sensor_data, sensor["unique_id"], unit
)
if data:
state = data.get("value")
Expand Down Expand Up @@ -82,6 +79,7 @@ def __init__(
self._name = f"Mycodo {name}"
self._unique_id = unique_id
self._unit_of_measurement = unit_of_measurement
self._unit = unit_of_measurement
self._device_class = device_class

self._state = state
Expand Down Expand Up @@ -113,6 +111,11 @@ def unit_of_measurement(self):
return TEMP_KELVIN
return unit

@property
def unit(self):
"""Return the unit of the sensor."""
return self._unit

@property
def device_class(self):
"""Return the device class of the sensor."""
Expand All @@ -121,7 +124,7 @@ def device_class(self):
async def async_update(self):
"""Fetch new state data for the sensor."""
data = await self.hass.async_add_executor_job(
self.mycodo_client.get_sensor_data, self._unique_id
self.mycodo_client.get_sensor_data, self._unique_id, self._unit
)
if data:
self._state = data.get("value")
Expand Down
11 changes: 6 additions & 5 deletions custom_components/mycodo_app/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import logging
import random

_LOGGER = logging.getLogger(__name__)
# Disable SSL warnings (for self-signed certificates)
Expand Down Expand Up @@ -52,15 +53,15 @@ def get_sensor_details(self, sensor_id):
"""Get detailed information for a specific sensor from Mycodo."""
return self.make_request(f"api/inputs/{sensor_id}")

def get_sensor_data(self, sensor_id):
"""Get the latest data for a specific sensor from Mycodo."""
def get_sensor_data(self, sensor_id, unit):
NOT_value = True
respose = self.make_request(f"api/measurements/last/{sensor_id}/C/0/30")
"""Get the latest data for a specific sensor from Mycodo."""
respose = self.make_request(f"api/measurements/last/{sensor_id}/{unit}/0/30")
while NOT_value:
if not respose.get("value"):
if respose.get("value") == None:
random_number = random.randint(10, 60)
respose = self.make_request(
f"api/measurements/last/{sensor_id}/C/0/{random_number}"
f"api/measurements/last/{sensor_id}/{unit}/0/{random_number}"
)
else:
return respose
Expand Down

0 comments on commit 29c9d18

Please sign in to comment.