# Path: controller/actor.py
# class PoupoolModel(Machine):
# 
#     def __init__(self, *args, **kwargs):
#         kwargs.setdefault("before_state_change", []).extend(["do_cancel", self.__update_state_time])
#         super().__init__(
#             auto_transitions=False,
#             ignore_invalid_triggers=True,
#             *args,
#             **kwargs
#         )
#         self.__state_time = None
# 
#     def __update_state_time(self):
#         self.__state_time = datetime.now()
# 
#     def get_time_in_state(self):
#         return datetime.now() - self.__state_time
# 
# Path: controller/actor.py
# class PoupoolActor(pykka.ThreadingActor):
# 
#     def __init__(self):
#         super().__init__()
#         self._proxy = self.actor_ref.proxy()
#         self.__timer = None
# 
#     def on_failure(self, exception_type, exception_value, traceback):
#         # The actor is going to die
#         logger.fatal(exception_type, exception_value, traceback)
# 
#     def on_stop(self):
#         self.do_cancel()
# 
#     def get_actor(self, name):
#         fsm = pykka.ActorRegistry.get_by_class_name(name)
#         if fsm:
#             return fsm[0].proxy()
#         logger.critical("Actor %s not found!!!" % name)
#         return None
# 
#     def __do_cancel(self):
#         if self.__timer:
#             self.__timer.cancel()
#             self.__timer = None
# 
#     def do_cancel(self):
#         self.__do_cancel()
# 
#     def do_delay(self, delay, method, *args, **kwargs):
#         assert type(method) == str
#         assert delay >= 0
#         # Stop an already running timer
#         self.__do_cancel()
#         func = getattr(self._proxy, method)
#         if delay > 0:
#             self.__timer = Timer(delay, func.defer, *args, **kwargs)
#             self.__timer.start()
#         else:
#             func.defer(*args, **kwargs)
# 
# Path: controller/actor.py
# def do_repeat():
#     def wrap(func):
#         @functools.wraps(func)
#         def wrapped_func(self, *args, **kwargs):
#             try:
#                 func(self, *args, **kwargs)
#             except StopRepeatException:
#                 pass
#             else:
#                 method = re.sub("on_enter_", "do_repeat_", func.__name__)
#                 function = getattr(self._proxy, method)
#                 function.defer()
#         return wrapped_func
#     return wrap
# 
# Path: controller/util.py
# def round_timedelta(x):
#     return timedelta(seconds=round(x.total_seconds()))
# 
# class Timer(object):
# 
#     def __init__(self, name):
#         self.__duration = timedelta()
#         self.__name = name
#         self.__last = None
#         self.__last_print = datetime(2000, 1, 1)
#         self.__delay = timedelta()
# 
#     @property
#     def duration(self):
#         return self.__duration
# 
#     @duration.setter
#     def duration(self, value):
#         self.__duration = value
# 
#     @property
#     def remaining(self):
#         return max(timedelta(), self.__delay - self.__duration)
# 
#     @property
#     def delay(self):
#         return self.__delay
# 
#     @delay.setter
#     def delay(self, value):
#         self.__delay = value
#         self.reset()
# 
#     def clear(self):
#         self.__last = None
# 
#     def reset(self):
#         self.clear()
#         self.__duration = timedelta()
# 
#     def update(self, now, factor=1):
#         if self.__last is not None:
#             self.__duration += factor * (now - self.__last)
#             remaining = max(timedelta(), self.delay - self.__duration)
#             if self.__last_print + timedelta(seconds=20) <= now:
#                 logger.debug("(%s) Timer: %s Remaining: %s" %
#                              (self.__name, self.__duration, remaining))
#                 self.__last_print = now
#         self.__last = now
# 
#     def elapsed(self):
#         return self.__duration >= self.__delay
# 
# Path: controller/config.py
# class Config:
#     def __init__(self, config_files):
#     def __getitem__(self, pair):
# def as_list(value, type_cast=int):


import pykka
import time
import logging
from datetime import datetime, timedelta
from astral import geocoder, sun
from .actor import PoupoolModel
from .actor import PoupoolActor
from .actor import do_repeat
from .util import round_timedelta, Timer
from .config import config
        from transitions.extensions import HierarchicalGraphMachine
        valve = self.__devices.get_valve("tank")
        if self.__overflow_in_comfort:
            valve.on()
        else:
            valve.off()

    def do_repeat_comfort(self):
        self.__eco_mode.update(datetime.now(), 0.5)
        actor = self.get_actor("Heating")
        if not actor.is_forcing().get() and not actor.is_recovering().get():
            actor.force.defer()
        self.do_delay(self.STATE_REFRESH_DELAY, self.do_repeat_comfort.__name__)

    def on_exit_comfort(self):
        logger.info("Exiting comfort state")
        self.get_actor("Heating").wait.defer()

    def on_enter_overflow(self):
        logger.info("Entering overflow state")
        self.__devices.get_valve("gravity").off()
        self.__devices.get_valve("tank").on()

    def on_enter_overflow_boost(self):
        logger.info("Entering overflow boost state")
        self.__encoder.filtration_state("overflow_boost")
        self.__actor_halt("Disinfection")
        self.__devices.get_pump("variable").speed(3)
        self.__devices.get_pump("boost").on()
        self.do_delay(self.__boost_duration.total_seconds(), "overflow")


# Path: controller/actor.py
# class PoupoolModel(Machine):
# 
#     def __init__(self, *args, **kwargs):
#         kwargs.setdefault("before_state_change", []).extend(["do_cancel", self.__update_state_time])
#         super().__init__(
#             auto_transitions=False,
#             ignore_invalid_triggers=True,
#             *args,
#             **kwargs
#         )
#         self.__state_time = None
# 
#     def __update_state_time(self):
#         self.__state_time = datetime.now()
# 
#     def get_time_in_state(self):
#         return datetime.now() - self.__state_time
# 
# Path: controller/actor.py
# class PoupoolActor(pykka.ThreadingActor):
# 
#     def __init__(self):
#         super().__init__()
#         self._proxy = self.actor_ref.proxy()
#         self.__timer = None
# 
#     def on_failure(self, exception_type, exception_value, traceback):
#         # The actor is going to die
#         logger.fatal(exception_type, exception_value, traceback)
# 
#     def on_stop(self):
#         self.do_cancel()
# 
#     def get_actor(self, name):
#         fsm = pykka.ActorRegistry.get_by_class_name(name)
#         if fsm:
#             return fsm[0].proxy()
#         logger.critical("Actor %s not found!!!" % name)
#         return None
# 
#     def __do_cancel(self):
#         if self.__timer:
#             self.__timer.cancel()
#             self.__timer = None
# 
#     def do_cancel(self):
#         self.__do_cancel()
# 
#     def do_delay(self, delay, method, *args, **kwargs):
#         assert type(method) == str
#         assert delay >= 0
#         # Stop an already running timer
#         self.__do_cancel()
#         func = getattr(self._proxy, method)
#         if delay > 0:
#             self.__timer = Timer(delay, func.defer, *args, **kwargs)
#             self.__timer.start()
#         else:
#             func.defer(*args, **kwargs)
# 
# Path: controller/actor.py
# def do_repeat():
#     def wrap(func):
#         @functools.wraps(func)
#         def wrapped_func(self, *args, **kwargs):
#             try:
#                 func(self, *args, **kwargs)
#             except StopRepeatException:
#                 pass
#             else:
#                 method = re.sub("on_enter_", "do_repeat_", func.__name__)
#                 function = getattr(self._proxy, method)
#                 function.defer()
#         return wrapped_func
#     return wrap
# 
# Path: controller/util.py
# def round_timedelta(x):
#     return timedelta(seconds=round(x.total_seconds()))
# 
# class Timer(object):
# 
#     def __init__(self, name):
#         self.__duration = timedelta()
#         self.__name = name
#         self.__last = None
#         self.__last_print = datetime(2000, 1, 1)
#         self.__delay = timedelta()
# 
#     @property
#     def duration(self):
#         return self.__duration
# 
#     @duration.setter
#     def duration(self, value):
#         self.__duration = value
# 
#     @property
#     def remaining(self):
#         return max(timedelta(), self.__delay - self.__duration)
# 
#     @property
#     def delay(self):
#         return self.__delay
# 
#     @delay.setter
#     def delay(self, value):
#         self.__delay = value
#         self.reset()
# 
#     def clear(self):
#         self.__last = None
# 
#     def reset(self):
#         self.clear()
#         self.__duration = timedelta()
# 
#     def update(self, now, factor=1):
#         if self.__last is not None:
#             self.__duration += factor * (now - self.__last)
#             remaining = max(timedelta(), self.delay - self.__duration)
#             if self.__last_print + timedelta(seconds=20) <= now:
#                 logger.debug("(%s) Timer: %s Remaining: %s" %
#                              (self.__name, self.__duration, remaining))
#                 self.__last_print = now
#         self.__last = now
# 
#     def elapsed(self):
#         return self.__duration >= self.__delay
# 
# Path: controller/config.py
# class Config:
#     def __init__(self, config_files):
#     def __getitem__(self, pair):
# def as_list(value, type_cast=int):


# Path: controller/filtration.py
import pykka
import time
import logging
from datetime import datetime, timedelta
from astral import geocoder, sun
from .actor import PoupoolModel
from .actor import PoupoolActor
from .actor import do_repeat
from .util import round_timedelta, Timer
from .config import config
        from transitions.extensions import HierarchicalGraphMachine

        valve = self.__devices.get_valve("tank")
        if self.__overflow_in_comfort:
            valve.on()
        else:
            valve.off()

    def do_repeat_comfort(self):
        self.__eco_mode.update(datetime.now(), 0.5)
        actor = self.get_actor("Heating")
        if not actor.is_forcing().get() and not actor.is_recovering().get():
            actor.force.defer()
        self.do_delay(self.STATE_REFRESH_DELAY, self.do_repeat_comfort.__name__)

    def on_exit_comfort(self):
        logger.info("Exiting comfort state")
        self.get_actor("Heating").wait.defer()

    def on_enter_overflow(self):
        logger.info("Entering overflow state")
        self.__devices.get_valve("gravity").off()
        self.__devices.get_valve("tank").on()

    def on_enter_overflow_boost(self):
        logger.info("Entering overflow boost state")
        self.__encoder.filtration_state("overflow_boost")
        self.__actor_halt("Disinfection")
        self.__devices.get_pump("variable").speed(3)
        self.__devices.get_pump("boost").on()
        self.do_delay(self.__boost_duration.total_seconds(), "overflow")


    @do_repeat()