Skip to content

Commit

Permalink
fix bundle handling (again), fix unique wifi switch name
Browse files Browse the repository at this point in the history
  • Loading branch information
myTselection committed Oct 7, 2024
1 parent 1fbf3d7 commit 11a6f4b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion custom_components/telenet_telemeter/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/myTselection/telenet_telemeter/issues",
"requirements": ["beautifulsoup4", "html5lib"],
"version": "1.8.0"
"version": "1.8.1"
}
19 changes: 11 additions & 8 deletions custom_components/telenet_telemeter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ async def dry_setup(hass, config_entry, async_add_devices):
await data_internet._forced_update()
assert data_internet._telemeter is not None
sensor = SensorInternet(data_internet, hass)
sensor.async_update()
sensors.append(sensor)
binarysensor = SensorPeak(data_internet, hass)
binarysensor.async_update()
sensors.append(binarysensor)
if mobile:
data_mobile = ComponentData(
Expand Down Expand Up @@ -133,12 +135,12 @@ async def async_remove_entry(hass, config_entry):
def get_desired_internet_product(products, desired_product_type):
# Try to find a product with productType = "bundle"
_LOGGER.debug(f'products: {products}, {desired_product_type}')
bundle_product = next((product for product in products if product['productType'].lower() == desired_product_type), None)
bundle_product = next((product for product in products if product.get('productType','').lower() == desired_product_type), None)
_LOGGER.debug(f'desired_product: {bundle_product}, {desired_product_type}')

# If no bundle is found, look for productType = "internet"
if not bundle_product:
return next((product for product in products if product['productType'].lower() == 'internet'), products[0])
return next((product for product in products if product.get('productType','').lower() == 'internet'), products[0])

_LOGGER.debug(f'return desired_product: {bundle_product}, {desired_product_type}')
return bundle_product
Expand Down Expand Up @@ -183,12 +185,13 @@ async def _forced_update(self):
desired_product = get_desired_internet_product(planInfo, 'bundle')
productIdentifier = desired_product.get('identifier')
_LOGGER.debug(f"productIdentifier internet: {productIdentifier}")
# if desired_product.get('productType','').lower() == "bundle":
# productIdentifier = next((product for product in desired_product.get('products') if product['productType'].lower() == 'internet'), desired_product.get('identifier'))
# _LOGGER.debug(f"productIdentifier bundle: {productIdentifier}")
# else:
# productIdentifier = desired_product.get('identifier')
# _LOGGER.debug(f"productIdentifier internet: {productIdentifier}")
if desired_product.get('productType','').lower() == "bundle":
product = next((product for product in desired_product.get('products') if product.get('productType','').lower() == 'internet'), desired_product.get('identifier'))
productIdentifier = product.get('identifier')
_LOGGER.debug(f"productIdentifier bundle: {productIdentifier}")
else:
productIdentifier = desired_product.get('identifier')
_LOGGER.debug(f"productIdentifier internet: {productIdentifier}")
billcycles = await self._hass.async_add_executor_job(lambda: self._session.billCycles("internet", productIdentifier))
startDate = billcycles.get('billCycles')[0].get("startDate")
endDate = billcycles.get('billCycles')[0].get("endDate")
Expand Down
24 changes: 16 additions & 8 deletions custom_components/telenet_telemeter/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
switches = []

data = ComponentSwitch(hass, config)

await data.force_update()
assert data._identifier is not None
wifiSwitch = WifiSwitch(data)
switches.append(wifiSwitch)
# wifreeSwitch = WifreeSwitch(data)
Expand All @@ -42,12 +45,12 @@ async def async_remove_entry(hass, config_entry):
# Function to get the desired product
def get_desired_internet_product(products, desired_product_type):
# Try to find a product with productType = "bundle"
bundle_product = next((product for product in products if product['productType'].lower() == desired_product_type), None)
bundle_product = next((product for product in products if product.get('productType').lower() == desired_product_type), None)
_LOGGER.debug(f'desired_product: {bundle_product}, {desired_product_type}')

# If no bundle is found, look for productType = "internet"
if not bundle_product:
return next((product for product in products if product['productType'].lower() == 'internet'), products[0])
return next((product for product in products if product.get('productType').lower() == 'internet'), products[0])

return bundle_product

Expand All @@ -57,9 +60,11 @@ def __init__(self, hass, config):
self._hass = hass
self._username = config.get('username')
self._password = config.get('password')
self._wifiState = False
self._wifiState = None
self._session = TelenetSession()
self._update_required = False
self._update_required = True
self._identifier = None


async def handle_switch_wireless(self, enableWifi):
"""Handle the service call."""
Expand All @@ -75,8 +80,9 @@ async def handle_switch_wireless(self, enableWifi):
# customerLocationId = customerDetails.get('customerLocations')[0].get('id')

internetProductDetails = await self._hass.async_add_executor_job(lambda: self._session.productSubscriptions("INTERNET"))
get_desired_internet_product(internetProductDetails, "internet")
internetProductIdentifier = get_desired_internet_product.get('identifier')
bundle = get_desired_internet_product(internetProductDetails, "internet")
internetProductIdentifier = bundle.get('identifier')
self._identifier = internetProductIdentifier

modemDetails = await self._hass.async_add_executor_job(lambda: self._session.modemdetails(internetProductIdentifier))
modemMac = modemDetails.get('mac')
Expand Down Expand Up @@ -143,7 +149,9 @@ async def force_update(self):
# customerLocationId = customerDetails.get('customerLocations')[0].get('id')

internetProductDetails = await self._hass.async_add_executor_job(lambda: self._session.productSubscriptions("INTERNET"))
internetProductIdentifier = internetProductDetails[0].get('identifier')
bundle = get_desired_internet_product(internetProductDetails, "internet")
internetProductIdentifier = bundle.get('identifier')
self._identifier = internetProductIdentifier

modemDetails = await self._hass.async_add_executor_job(lambda: self._session.modemdetails(internetProductIdentifier))
modemMac = modemDetails.get('mac')
Expand Down Expand Up @@ -208,7 +216,7 @@ def icon(self) -> str:
def unique_id(self) -> str:
"""Return the name of the sensor."""
return (
f"{NAME} Wifi"
f"{NAME} Wifi {self._data._identifier}"
)

@property
Expand Down

0 comments on commit 11a6f4b

Please sign in to comment.