MizterB / homeassistant-phyn

16 stars 12 forks source link

Integration does not support Phyn setups with Phyn Water Sensors #11

Open opq8 opened 1 year ago

opq8 commented 1 year ago

After working around the issue in #10, I now hit an issue where I don't get my switches from the Integration.

Would anyone be able to help me out as to why it doesn't like _device_state["sov_status"] or _device_state["flow"]?

Is anyone else testing against a Phyn Plus 1st gen (pp1)? Or does everyone here have 2nd gens (pp2)? Could this also be the same issue I reported to @rsocko in #5 where having water sensors (pw1)'s is interfering with logic since the structure is different?

Error while setting up phyn platform for switch

Logger: homeassistant.components.switch
Source: helpers/entity_platform.py:359
Integration: Switch (documentation, issues)
First occurred: 9:59:58 PM (1 occurrences)
Last logged: 9:59:58 PM

Error while setting up phyn platform for switch
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_setup_platform
    await asyncio.shield(task)
  File "/config/custom_components/phyn/switch.py", line 30, in async_setup_entry
    entities.append(PhynSwitch(device))
                    ^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/phyn/switch.py", line 40, in __init__
    self._state = self._device.valve_state == "Open"
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/phyn/device.py", line 116, in valve_state
    return self._device_state["sov_status"]["v"]
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'sov_status'

Error adding entities for domain sensor with platform phyn

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:507
Integration: Sensor ([documentation](https://www.home-assistant.io/integrations/sensor), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+sensor%22))
First occurred: 9:59:58 PM (1 occurrences)
Last logged: 9:59:58 PM
Error adding entities for domain sensor with platform phyn

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 507, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 752, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1057, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 779, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 879, in _async_write_ha_state
    state, attr = self._async_generate_attributes()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 820, in _async_generate_attributes
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 785, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 475, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/phyn/sensor.py", line 89, in native_value
    if self._device.current_flow_rate is None:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/phyn/device.py", line 86, in current_flow_rate
    return self._device_state["flow"]["mean"]
           ~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'flow'

Error while setting up phyn platform for sensor

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:367
Integration: Sensor (documentation, issues)
First occurred: 9:59:58 PM (1 occurrences)
Last logged: 9:59:58 PM

Error while setting up phyn platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 367, in _async_setup_platform
    await asyncio.gather(*pending)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 507, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 752, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1057, in add_to_platform_finish
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 779, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 879, in _async_write_ha_state
    state, attr = self._async_generate_attributes()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 820, in _async_generate_attributes
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 785, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 475, in state
    value = self.native_value
            ^^^^^^^^^^^^^^^^^
  File "/config/custom_components/phyn/sensor.py", line 89, in native_value
    if self._device.current_flow_rate is None:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/phyn/device.py", line 86, in current_flow_rate
    return self._device_state["flow"]["mean"]
           ~~~~~~~~~~~~~~~~~~^^^^^^^^
KeyError: 'flow'
opq8 commented 1 year ago

Also pasting what I wrote in #5:

I don't think this integration works at all with a Phyn setup where it's more than just the Phyn Plus, that is, when the setup also includes Phyn water sensors.

async_add_entities(entities) tries to add the same set of attributes in sensor.py and switch.py regardless of the device being a pp1/2 (the Phyn Plus) vs. a pw1 (water sensor).

And here are the attributes taken from my logs:

For a water sensor (PW1)

2023-10-12 22:28:13.312 DEBUG (MainThread) [custom_components.phyn] Phyn device state: {'battery_level': {'v': 100, 'ts': REDACTED}, 'signal_strength': 71, 'online_status': {'ts': REDACTED, 'v': 'online'}, 'sd_status': {'ts': REDACTED, 'v': 'G'}, 'hw_version': '0', 'device_id': 'REDACTED', 'product_code': 'PW1', 'network_name': 'REDACTED', 'temperature': {'v': REDACTED, 'ts': REDACTED}, 'name': 'REDACTED', 'auto_shutoff_eligible': 0, 'serial_number': 'REDACTED', 'fw_version': 'REDACTED', 'timezone': 'America/Los_Angeles', 'partner': 'phyn', 'humidity': {'v': REDACTED, 'ts': REDACTED}, 'users': ['REDACTED'], 'auto_shutoff_enable': False, 'created_ts': REDACTED}

For the Phyn Plus itself (pp1 or pp2, example taken from pp1)

2023-10-12 22:28:13.407 DEBUG (MainThread) [custom_components.phyn] Phyn device state: {'signal_strength': -65, 'online_status': {'v': 'online', 'sid': 'REDACTED', 'ts': REDACTED}, 'sd_status': {'v': 'G', 'r': 'watchdog', 'ts': REDACTED}, 'hw_version': '0', 'device_id': 'REDACTED', 'product_code': 'PP1', 'network_name': 'REDACTED', 'temperature': {'min': REDACTED, 'max': REDACTED, 'mean': REDACTED, 'ts': REDACTED}, 'auto_shutoff_eligible': 100, 'serial_number': 'REDACTED', 'sov_status': {'a': 'REDACTED', 'v': 'Open', 'ts': REDACTED}, 'flow': {'min': REDACTED, 'max': REDACTED, 'mean': REDACTED, 'ts': REDACTED}, 'pressure': {'min': REDACTED, 'median': REDACTED, 'max': REDACTED, 'mean': REDACTED, 'percentile95': REDACTED, 'pressure_threshold_95': REDACTED, 'percentile5': REDACTED, 'ts': REDACTED}, 'fw_version': 'REDACTED', 'timezone': 'America/Los_Angeles', 'partner': 'phyn', 'users': ['REDACTED'], 'auto_shutoff_enable': True, 'created_ts': REDACTED}

This probably means #5 and #11 (this issue) will need to be fixed together.

opq8 commented 1 year ago

Created https://github.com/MizterB/homeassistant-phyn/pull/12 to fix this. I'll stack further changes in future to add the Water Sensors (pw1) to HA.

opq8 commented 1 year ago

Not sure if this repo is still actively maintained so I created a fork https://github.com/opq8/homeassistant-phyn/tree/development with this fix. Happy to merge everything back into here as needed.

Hedda commented 10 months ago

FYI, it looks like jordanruthe has the most active fork(s) so might want to submit PR there to if wish to collaborate and consolidate:

https://github.com/jordanruthe/homeassistant-phyn -> https://github.com/jordanruthe/homeassistant-phyn/commits/dev/

https://github.com/jordanruthe/aiophyn -> https://github.com/jordanruthe/aiophyn/commits/dev/