spacemanspiff2007 / HABApp

Easy home automation with MQTT and/or openHAB
Apache License 2.0
54 stars 22 forks source link

Problem With Persistence #159

Closed NickWaterton closed 4 years ago

NickWaterton commented 4 years ago

I just upgraded to the latest version of HABApp, and I now get the following errors when trying to access persistence data:

2020-07-22 10:00:08,740[             HABApp.Worker]ERROR|  __format_traceback: Error in HEMCaculateCost.item_value_changed: maximum recursion depth exceeded
2020-07-22 10:00:08,741[             HABApp.Worker]ERROR|  __format_traceback: Stackprinter failed:
2020-07-22 10:00:08,743[             HABApp.Worker]ERROR|  __format_traceback:   File "/usr/local/lib/python3.6/dist-packages/stackprinter-0.2.3-py3.6.egg/stackprinter/formatting.py", line 168, in format_exc_info
2020-07-22 10:00:08,744[             HABApp.Worker]ERROR|  __format_traceback:     stack_msg = format_stack(frameinfos, style=style, reverse=reverse, **kwargs)
2020-07-22 10:00:08,745[             HABApp.Worker]ERROR|  __format_traceback: TypeError: format_stack() got an unexpected keyword argument 'line_wrap'
2020-07-22 10:00:08,746[             HABApp.Worker]ERROR|  __format_traceback:
2020-07-22 10:00:08,747[             HABApp.Worker]ERROR|  __format_traceback: So here is your original traceback at least:
2020-07-22 10:00:08,749[             HABApp.Worker]ERROR|  __format_traceback:
2020-07-22 10:00:08,751[             HABApp.Worker]ERROR|  __format_traceback: Traceback (most recent call last):
2020-07-22 10:00:08,753[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/core/wrappedfunction.py", line 91, in __run
2020-07-22 10:00:08,754[             HABApp.Worker]ERROR|  __format_traceback:     self._func(*args, **kwargs)
2020-07-22 10:00:08,755[             HABApp.Worker]ERROR|  __format_traceback:   File "/etc/openhab2/HABApp/rules/hem.py", line 144, in item_value_changed
2020-07-22 10:00:08,756[             HABApp.Worker]ERROR|  __format_traceback:     self.calc_energy_cost()
2020-07-22 10:00:08,759[             HABApp.Worker]ERROR|  __format_traceback:   File "/etc/openhab2/HABApp/rules/hem.py", line 226, in calc_energy_cost
2020-07-22 10:00:08,761[             HABApp.Worker]ERROR|  __format_traceback:     self.get_average_values()
2020-07-22 10:00:08,762[             HABApp.Worker]ERROR|  __format_traceback:   File "/etc/openhab2/HABApp/rules/hem.py", line 242, in get_average_values
2020-07-22 10:00:08,763[             HABApp.Worker]ERROR|  __format_traceback:     power_vals_24_hrs = self.power.get_persistence_data(start_time=prev_24_hours)
2020-07-22 10:00:08,765[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/items/base_item.py", line 38, in get_persistence_data
2020-07-22 10:00:08,765[             HABApp.Worker]ERROR|  __format_traceback:     self.name, persistence, start_time, end_time
2020-07-22 10:00:08,767[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/connection_handler/func_sync.py", line 219, in get_persistence_data
2020-07-22 10:00:08,768[             HABApp.Worker]ERROR|  __format_traceback:     item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
2020-07-22 10:00:08,769[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/connection_handler/func_sync.py", line 219, in get_persistence_data
2020-07-22 10:00:08,770[             HABApp.Worker]ERROR|  __format_traceback:     item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
2020-07-22 10:00:08,772[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/connection_handler/func_sync.py", line 219, in get_persistence_data
2020-07-22 10:00:08,773[             HABApp.Worker]ERROR|  __format_traceback:     item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
2020-07-22 10:00:08,775[             HABApp.Worker]ERROR|  __format_traceback:   [Previous line repeated 980 more times]
2020-07-22 10:00:08,776[             HABApp.Worker]ERROR|  __format_traceback: RecursionError: maximum recursion depth exceeded
2020-07-22 10:00:08,778[             HABApp.Worker]WARNI|               __run: Execution of HEMCaculateCost.item_value_changed took too long: 4.82s

and a similar error

2020-07-22 10:05:30,921[             HABApp.Worker]ERROR|  __format_traceback: Error in Weather.change_received: maximum recursion depth exceeded
2020-07-22 10:05:30,922[             HABApp.Worker]ERROR|  __format_traceback: Stackprinter failed:
2020-07-22 10:05:30,923[             HABApp.Worker]ERROR|  __format_traceback:   File "/usr/local/lib/python3.6/dist-packages/stackprinter-0.2.3-py3.6.egg/stackprinter/formatting.py", line 168, in format_exc_info
2020-07-22 10:05:30,923[             HABApp.Worker]ERROR|  __format_traceback:     stack_msg = format_stack(frameinfos, style=style, reverse=reverse, **kwargs)
2020-07-22 10:05:30,924[             HABApp.Worker]ERROR|  __format_traceback: TypeError: format_stack() got an unexpected keyword argument 'line_wrap'
2020-07-22 10:05:30,925[             HABApp.Worker]ERROR|  __format_traceback:
2020-07-22 10:05:30,926[             HABApp.Worker]ERROR|  __format_traceback: So here is your original traceback at least:
2020-07-22 10:05:30,927[             HABApp.Worker]ERROR|  __format_traceback:
2020-07-22 10:05:30,927[             HABApp.Worker]ERROR|  __format_traceback: Traceback (most recent call last):
2020-07-22 10:05:30,928[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/core/wrappedfunction.py", line 91, in __run
2020-07-22 10:05:30,929[             HABApp.Worker]ERROR|  __format_traceback:     self._func(*args, **kwargs)
2020-07-22 10:05:30,930[             HABApp.Worker]ERROR|  __format_traceback:   File "/etc/openhab2/HABApp/rules/weather.py", line 280, in change_received
2020-07-22 10:05:30,932[             HABApp.Worker]ERROR|  __format_traceback:     self.get_minmax_values()
2020-07-22 10:05:30,933[             HABApp.Worker]ERROR|  __format_traceback:   File "/etc/openhab2/HABApp/rules/weather.py", line 325, in get_minmax_values
2020-07-22 10:05:30,933[             HABApp.Worker]ERROR|  __format_traceback:     temps_since_midnight = self.item['Weather_Temperature'].get_persistence_data(start_time=since_midnight)
2020-07-22 10:05:30,934[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/items/base_item.py", line 38, in get_persistence_data
2020-07-22 10:05:30,935[             HABApp.Worker]ERROR|  __format_traceback:     self.name, persistence, start_time, end_time
2020-07-22 10:05:30,937[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/connection_handler/func_sync.py", line 219, in get_persistence_data
2020-07-22 10:05:30,937[             HABApp.Worker]ERROR|  __format_traceback:     item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
2020-07-22 10:05:30,938[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/connection_handler/func_sync.py", line 219, in get_persistence_data
2020-07-22 10:05:30,939[             HABApp.Worker]ERROR|  __format_traceback:     item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
2020-07-22 10:05:30,940[             HABApp.Worker]ERROR|  __format_traceback:   File "/home/nick/Scripts/HABApp/HABApp/openhab/connection_handler/func_sync.py", line 219, in get_persistence_data
2020-07-22 10:05:30,941[             HABApp.Worker]ERROR|  __format_traceback:     item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
2020-07-22 10:05:30,941[             HABApp.Worker]ERROR|  __format_traceback:   [Previous line repeated 981 more times]
2020-07-22 10:05:30,942[             HABApp.Worker]ERROR|  __format_traceback: RecursionError: maximum recursion depth exceeded
2020-07-22 10:05:30,943[             HABApp.Worker]WARNI|               __run: Execution of Weather.change_received took too long: 5.24s

Any suggestions?

NickWaterton commented 4 years ago

OK, poking around, the problem is in File "/home/nick/Scripts/HABApp/HABApp/openhab/connection_handler/func_sync.py", line 219 (as the error states):

def get_persistence_data(item_name: str, persistence: Optional[str],
                         start_time: Optional[datetime.datetime],
                         end_time: Optional[datetime.datetime]) -> OpenhabPersistenceData:
    """Query historical data from the OpenHAB persistence service

    :param item_name: name of the persistet item
    :param persistence: name of the persistence service (e.g. ``rrd4j``, ``mapdb``). If not set default will be used
    :param start_time: return only items which are newer than this
    :param end_time: return only items which are older than this
    """
    assert isinstance(item_name, str) and item_name, item_name
    assert isinstance(persistence, str) or persistence is None, persistence
    assert isinstance(start_time, datetime.datetime) or start_time is None, start_time
    assert isinstance(end_time, datetime.datetime) or end_time is None, end_time

    fut = asyncio.run_coroutine_threadsafe(
        get_persistence_data(
            item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
        ),
        loop
    )

    ret = fut.result()
    return OpenhabPersistenceData.from_dict(ret)

Which recursively calls itself.

The solution is to add async_get_persistence_data to the imports at the top of the file:

from .func_async import async_post_update, async_send_command, async_create_item, async_get_item, async_get_thing, \
    async_set_metadata, async_remove_metadata, async_get_channel_link, async_create_channel_link, \
    async_remove_channel_link, async_channel_link_exists, \
    async_remove_item, async_item_exists, async_get_persistence_data

and change line 217 to be:

    fut = asyncio.run_coroutine_threadsafe(
        async_get_persistence_data(
            item_name=item_name, persistence=persistence, start_time=start_time, end_time=end_time
        ),
        loop
    )

I assume this was a simple oversight.

spacemanspiff2007 commented 4 years ago

Thank you for catching and sorry for the convenience. I must have made a mistake while refactoring and it seems I have not test for persistence.