Skip to content

Commit

Permalink
Added fallback and cached data request
Browse files Browse the repository at this point in the history
  • Loading branch information
sockless-coding committed Sep 24, 2024
1 parent 3b42c46 commit 1d853fa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
21 changes: 17 additions & 4 deletions custom_components/panasonic_cc/pcomfortcloud/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'''

import logging
import re
import aiohttp
import time
from datetime import datetime
Expand Down Expand Up @@ -43,6 +42,7 @@ def __init__(self,
self._groups = None
self._devices: list[PanasonicDeviceInfo] = None
self._unknown_devices: list[PanasonicDeviceInfo] = []
self._cache_devices = {}

self._device_indexer = {}
self._raw = raw
Expand Down Expand Up @@ -134,14 +134,27 @@ async def history(self, device_id, mode, date, time_zone=""):
}


async def _get_device_status(self, device_info: PanasonicDeviceInfo):
if (device_info.status_data_mode == constants.StatusDataMode.LIVE
or (device_info.id in self._cache_devices and self._cache_devices[device_info.id] <= 0)):
try:
json_response = await self.execute_get(self._get_device_status_url(device_info.guid), "get_status", 200)
device_info.status_data_mode = constants.StatusDataMode.LIVE
return json_response
except Exception as e:
_LOGGER.warning("Failed to get live status for device {} switching to cached data.".format(device_info.guid))
device_info.status_data_mode = constants.StatusDataMode.CACHED
self._cache_devices[device_info.id] = 10
json_response = await self.execute_get(self._get_device_status_now_url(device_info.guid), "get_status", 200)
self._cache_devices[device_info.id] -= 1
return json_response

async def get_device(self, device_info: PanasonicDeviceInfo) -> PanasonicDevice:
json_response = await self.execute_get(self._get_device_status_now_url(device_info.guid), "get_device", 200)
json_response = await self._get_device_status(device_info)
return PanasonicDevice(device_info, json_response)

async def try_update_device(self, device: PanasonicDevice) -> bool:
device_guid = device.info.guid
json_response = await self.execute_get(self._get_device_status_now_url(device_guid), "try_update", 200)
json_response = await self._get_device_status(device.info)
return device.load(json_response)

async def get_aquarea_device(self, device_info: PanasonicDeviceInfo):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def raw(self):
@property
def status_data_mode(self):
return self._status_data_mode
@status_data_mode.setter
def status_data_mode(self, value: constants.StatusDataMode):
self._status_data_mode = value





Expand Down
15 changes: 15 additions & 0 deletions custom_components/panasonic_cc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)

from .pcomfortcloud.panasonicdevice import PanasonicDevice, PanasonicDeviceEnergy, PanasonicDeviceZone
from .pcomfortcloud import constants

from .const import (
DOMAIN,
Expand Down Expand Up @@ -81,6 +82,19 @@ class PanasonicEnergySensorEntityDescription(SensorEntityDescription):
is_available=lambda device: True,
entity_registry_enabled_default=True,
)
DATA_MODE_DESCRIPTION = PanasonicSensorEntityDescription(
key="status_data_mode",
translation_key="status_data_mode",
name="Data Mode",
options=[opt.name for opt in constants.StatusDataMode],
device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=None,
native_unit_of_measurement=None,
get_state=lambda device: device.info.status_data_mode.name,
is_available=lambda device: True,
entity_registry_enabled_default=True,
)
DAILY_ENERGY_DESCRIPTION = PanasonicEnergySensorEntityDescription(
key="daily_energy_sensor",
translation_key="daily_energy_sensor",
Expand Down Expand Up @@ -165,6 +179,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
entities.append(PanasonicSensorEntity(coordinator, OUTSIDE_TEMPERATURE_DESCRIPTION))
entities.append(PanasonicSensorEntity(coordinator, LAST_UPDATE_TIME_DESCRIPTION))
entities.append(PanasonicSensorEntity(coordinator, DATA_AGE_DESCRIPTION))
entities.append(PanasonicSensorEntity(coordinator, DATA_MODE_DESCRIPTION))
if coordinator.device.has_zones:
for zone in coordinator.device.parameters.zones:
entities.append(PanasonicSensorEntity(
Expand Down

0 comments on commit 1d853fa

Please sign in to comment.