sebr / bhyve-home-assistant

Orbit BHyve custom component for Home Assistant
MIT License
260 stars 42 forks source link

Error while setting up bhyve platform for switch - AttributeError: 'NoneType' object has no attribute 'isoformat #226

Closed AndrewEAUS closed 1 year ago

AndrewEAUS commented 1 year ago

Here is the log showing the error - Logger: homeassistant.components.switch Source: helpers/entity_platform.py:359 Integration: Switch (documentation, issues) First occurred: 10:35:17 PM (2 occurrences) Last logged: 10:37:15 PM

Error while setting up bhyve 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/bhyve/switch.py", line 183, in async_setup_entry BHyveZoneSwitch( File "/config/custom_components/bhyve/switch.py", line 388, in init super().init(hass, bhyve, device, name, icon, SwitchDeviceClass.SWITCH) File "/config/custom_components/bhyve/init.py", line 268, in init self._setup(device) File "/config/custom_components/bhyve/switch.py", line 420, in _setup ).isoformat() ^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'isoformat'

Device is - Bhyve watering system Orbit Irrigation Product HT31BT B-Hyve XD Bluetooth Hose Faucet Timer.

Here is the key part of switch.py code -

        next_start_time = orbit_time_to_local_time(
            status.get("next_start_time")
        ).isoformat()
        if next_start_time is not None:
            next_start_programs = status.get("next_start_programs")
            self._attrs.update(
                {
                    ATTR_NEXT_START_TIME: next_start_time,
                    ATTR_NEXT_START_PROGRAMS: next_start_programs,
                }
            )

SOLUTION IS

        next_start_time_raw = status.get("next_start_time")
        if next_start_time_raw is not None:
            next_start_time = orbit_time_to_local_time(
                next_start_time_raw
            ).isoformat()
            next_start_programs = status.get("next_start_programs")
            self._attrs.update(
                {
                    ATTR_NEXT_START_TIME: next_start_time,
                    ATTR_NEXT_START_PROGRAMS: next_start_programs,
                }
            )

In this updated code:

Retrieve next_start_time from status and store it in next_start_time_raw. Check if next_start_time_raw is not None before proceeding with the conversion to local time and the update of self._attrs. This way, you avoid calling isoformat() on a None value and the AttributeError should no longer occur.

sebr commented 1 year ago

Closed in https://github.com/sebr/bhyve-home-assistant/pull/219