Skip to content

Commit

Permalink
Updates to hub event processor for date/time
Browse files Browse the repository at this point in the history
  • Loading branch information
c503ghosh committed Nov 8, 2024
1 parent 0dc9f86 commit 588be48
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
10 changes: 2 additions & 8 deletions custom_components/dirigera_platform/base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ async def async_update(self):
async def async_set_percentage(self, percentage: int) -> None:
# Convert percent to speed
desired_speed = math.ceil(percentage * 50 / 100)
logger.debug(
"set_percentage got : {}, scaled to : {}".format(percentage, desired_speed)
)
logger.debug("set_percentage got : {}, scaled to : {}".format(percentage, desired_speed))
await self._hass.async_add_executor_job(self._json_data.set_motor_state, desired_speed)

async def async_set_status_light(self, status: bool) -> None:
Expand Down Expand Up @@ -575,11 +573,7 @@ async def async_set_preset_mode(self, preset_mode: str):
await self._hass.async_add_executor_job(self._json_data.set_fan_mode, mode_to_set)

async def async_turn_on(self, percentage=None, preset_mode=None) -> None:
logger.debug(
"Airpurifier call to turn_on with percentage: {}, preset_mode: {}".format(
percentage, preset_mode
)
)
logger.debug("Airpurifier call to turn_on with percentage: {}, preset_mode: {}".format(percentage, preset_mode))

if preset_mode is not None:
await self.async_set_preset_mode(preset_mode)
Expand Down
18 changes: 16 additions & 2 deletions custom_components/dirigera_platform/hub_event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

logger = logging.getLogger("custom_components.dirigera_platform")

DATE_TIME_FORMAT:str = "%Y-%m-%dT%H:%M:%S.%fZ"

process_events_from = {
"motionSensor" : ["isDetected","isOn","batteryPercentage"],
"outlet" : [ "isOn",
Expand Down Expand Up @@ -261,8 +263,20 @@ def on_message(self, ws:Any, ws_msg:str):
if key_attr == "is_on":
turn_on_off = True
logger.debug(f"setting {key_attr} to {attributes[key]}")
setattr(entity._json_data.attributes,key_attr, attributes[key])
logger.debug(entity._json_data)
logger.debug(f"Entity before setting: {entity._json_data}")

value_to_set = attributes[key]
#Need a hack for outlet with date/time entities
if key in ["timeOfLastEnergyReset","totalEnergyConsumedLastUpdated"]:
logger.debug(f"Got into date/time so will set the value accordingly...")
try :
value_to_set = datetime.datetime.strptime(attributes[key], DATE_TIME_FORMAT)
except:
#Ignore the exception
logger.warning(f"Failed to convert {attributes[key]} to date/time...")

setattr(entity._json_data.attributes,key_attr, value_to_set)
logger.debug(f"Entity after setting: {entity._json_data}")
except Exception as ex:
logger.warn(f"Failed to set attribute key: {key} converted to {key_attr} on device: {id}")
logger.warn(ex)
Expand Down
2 changes: 0 additions & 2 deletions custom_components/dirigera_platform/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ def available(self):

@property
def device_info(self) -> DeviceInfo:
logger.debug("device info called...")

return DeviceInfo(
identifiers={("dirigera_platform", self._json_data.id)},
Expand Down Expand Up @@ -372,7 +371,6 @@ def available(self):

@property
def device_info(self) -> DeviceInfo:
logger.debug("device info device_set called...")

# Register the device for updates
hub_event_listener.register(self.unique_id, self)
Expand Down

0 comments on commit 588be48

Please sign in to comment.