Skip to content

Home Information - Your home, your data, organized.

Python
6.1 KB
Raw Blame
from dataclasses import dataclass, fields, MISSING
from datetime import datetime
from typing import Any, Dict, List, Tuple, Type

from hi.apps.common.utils import str_to_bool

from .enums import SimEntityType, SimStateType


@dataclass( frozen = True )
class SimEntityFields:
    """
    Base class that simulators should extend to add extra (editable) fields
    needed to define for a specific simulator entity.  Simulators should
    define one or more subclasses of this and make those known to the
    ServiceSimulatorManager through the sim_entity_definition_list() method of the
    ServiceSimulator class (using the SimEntityDefinition class).
    """
    name             : str
    
    @classmethod
    def class_id(cls) -> str:
        dotted_path = f'{cls.__module__}.{cls.__qualname__}'
        parts = dotted_path.split('.')
        return '.'.join( reversed( parts ))
    
    def to_json_dict(self) -> Dict[ str, Any ]:
        """
        Subclasses only need to override this if the field types are not
        directly JSON serializable or lack special cases defined in this
        method (e.g., datetime).
        """
        json_dict = dict()
        for field in fields(self):
            value = getattr( self, field.name )
            if isinstance( value, datetime ):
                value = value.isoformat()
            json_dict[field.name] = value
            continue
        return json_dict

    @classmethod
    def from_json_dict( cls, json_content : Dict[ str, Any ] ) -> 'SimEntityFields':
        """
        Subclasses only need to override this if the field types are not
        directly JSON deserializable or lack special cases defined in this
        method (e.g., datetime).
        """
        kwargs = dict()
        for field in fields( cls ):
            value = json_content.get( field.name, field.default )
            if ( field.type == datetime ) and isinstance( value, str ):
                value = datetime.fromisoformat( value )
            kwargs[field.name] = value
            continue
        return cls( **kwargs )

    def to_initial_form_values( self ) -> Dict[ str, Any ]:
        initial_values = dict()
        for field in fields(self):
            initial_values[field.name] = getattr( self, field.name )
            continue
        return initial_values

    @classmethod
    def from_form_data( cls, form_data : Dict[ str, Any ] ) -> 'SimEntityFields':
        kwargs = dict()
        for field in fields( cls ):
            value = form_data.get( field.name, field.default )
            if value is MISSING:
                value = None
            kwargs[field.name] = value
            continue
        return cls( **kwargs )


@dataclass
class SimState:
    """
    Base class that individual simulators should override to define the
    states for a simulator entity. A simulator entity will provide one or
    more SimState subclasses. SimState classes are provided via the
    SimEntityDefinition and will be initialized with the containing
    entities fields and values by the ServiceSimulatorManager.

    SimState definitions and instances are not persisted in the database.
    """

    # These fields are provided by ServiceSimulatorManager when creating instances.
    sim_entity_id      : int
    sim_entity_fields  : SimEntityFields

    # Subclasses must provide a default value for these.    
    sim_state_type     : SimStateType
    sim_state_id       : str  # Only needs to be unique among all SimEntity's SimState instances

    # Subclasses may provide a default value for this.    
    value              : Any              = None
    
    @property
    def name(self):
        return self.sim_entity_fields.name

    @property
    def is_on(self) -> bool:
        """Boolean coercion of ``value`` for templates that render
        a checkbox ``checked``-state. Default ``value``s like
        ``'off'`` are non-empty strings and would be truthy under
        Django's ``{% if value %}`` test, leaving the initial
        render out of sync with the logical state. Routing through
        ``str_to_bool`` keeps the visible toggle aligned with the
        actual ON/OFF interpretation across all string-stored
        boolean states (ON_OFF, MOVEMENT, OPEN_CLOSE, etc.)."""
        return str_to_bool( self.value )

    def set_value_from_string( self, value_str : str ):
        """ Subclasses should override this is the value is not a string and needs conversion. """
        if self.value == value_str:
            return
        self.value = value_str
        return
    
    @property
    def min_value(self) -> Any:
        """ Subclasses can override this to define the minimum valid value """
        return None
    
    @property
    def max_value(self) -> Any:
        """ Subclasses can override this to define the maximum valid value """
        return None
    
    @property
    def choices(self) -> List[ Tuple[ str, str ]]:
        """ Subclasses using SimStateType.DISCRETE should override this to provide the valid values. """
        return list()

    @property
    def display_unit(self) -> str:
        """Optional unit suffix shown next to the value/bounds in
        the simulator UI (e.g., ``°F`` next to a thermostat
        temperature). Subclasses with unit-bearing values should
        override; default is empty (no suffix)."""
        return ''

    def copy_value( self, other_sim_state : 'SimState' ):
        if not other_sim_state:
            return
        self.value = other_sim_state.value
        return
    

@dataclass
class SimEntityDefinition:
    """
    Used by ServiceSimulator instances to define a simulated entity that is
    available to be defined and added to the simulator configuration.  Each
    type of simulated entity should also provide one or more SimState
    subclasses that will be used to track the state/status of the simulation.
    """
    
    class_label              : str
    sim_entity_type          : SimEntityType
    sim_entity_fields_class  : Type[ SimEntityFields ]
    sim_state_class_list     : List[ Type[ SimState ]]
    
    @property
    def class_id(self) -> str:
        return f'{self.sim_entity_fields_class.class_id()}'