mirror of
https://github.com/home-assistant/core.git
synced 2025-11-13 13:00:11 +00:00
Refactor bayesian observations using dataclass (#79590)
* refactor * remove some changes * remove typehint * improve codestyle * move docstring to comment * < 88 chars * avoid short var names * more readable * fix rename * Update homeassistant/components/bayesian/helpers.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/bayesian/binary_sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/bayesian/binary_sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * no intermediate * comment why set before list Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
69
homeassistant/components/bayesian/helpers.py
Normal file
69
homeassistant/components/bayesian/helpers.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""Helpers to deal with bayesian observations."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
import uuid
|
||||
|
||||
from homeassistant.const import (
|
||||
CONF_ABOVE,
|
||||
CONF_BELOW,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_PLATFORM,
|
||||
CONF_VALUE_TEMPLATE,
|
||||
)
|
||||
from homeassistant.helpers.template import Template
|
||||
|
||||
from .const import CONF_P_GIVEN_F, CONF_P_GIVEN_T, CONF_TO_STATE
|
||||
|
||||
|
||||
@dataclass
|
||||
class Observation:
|
||||
"""Representation of a sensor or template observation."""
|
||||
|
||||
entity_id: str | None
|
||||
platform: str
|
||||
prob_given_true: float
|
||||
prob_given_false: float
|
||||
to_state: str | None
|
||||
above: float | None
|
||||
below: float | None
|
||||
value_template: Template | None
|
||||
observed: bool | None = None
|
||||
id: str = field(default_factory=lambda: str(uuid.uuid4()))
|
||||
|
||||
def to_dict(self) -> dict[str, str | float | bool | None]:
|
||||
"""Represent Class as a Dict for easier serialization."""
|
||||
|
||||
# Needed because dataclasses asdict() can't serialize Templates and ignores Properties.
|
||||
dic = {
|
||||
CONF_PLATFORM: self.platform,
|
||||
CONF_ENTITY_ID: self.entity_id,
|
||||
CONF_VALUE_TEMPLATE: self.template,
|
||||
CONF_TO_STATE: self.to_state,
|
||||
CONF_ABOVE: self.above,
|
||||
CONF_BELOW: self.below,
|
||||
CONF_P_GIVEN_T: self.prob_given_true,
|
||||
CONF_P_GIVEN_F: self.prob_given_false,
|
||||
"observed": self.observed,
|
||||
}
|
||||
|
||||
for key, value in dic.copy().items():
|
||||
if value is None:
|
||||
del dic[key]
|
||||
|
||||
return dic
|
||||
|
||||
def is_mirror(self, other: Observation) -> bool:
|
||||
"""Dectects whether given observation is a mirror of this one."""
|
||||
return (
|
||||
self.platform == other.platform
|
||||
and round(self.prob_given_true + other.prob_given_true, 1) == 1
|
||||
and round(self.prob_given_false + other.prob_given_false, 1) == 1
|
||||
)
|
||||
|
||||
@property
|
||||
def template(self) -> str | None:
|
||||
"""Not all observations have templates and we want to get template strings."""
|
||||
if self.value_template is not None:
|
||||
return self.value_template.template
|
||||
return None
|
||||
Reference in New Issue
Block a user