-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hass_myhomesmart.py
112 lines (97 loc) · 4.81 KB
/
hass_myhomesmart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# __ __ _ _ _____ _
# | \/ | | | | | / ____| | |
# | \ / |_ _| |__| | ___ _ __ ___ ___| (___ _ __ ___ __ _ _ __| |_
# | |\/| | | | | __ |/ _ \| '_ ` _ \ / _ \\___ \| '_ ` _ \ / _` | '__| __|
# | | | | |_| | | | | (_) | | | | | | __/____) | | | | | | (_| | | | |_
# |_| |_|\__, |_| |_|\___/|_| |_| |_|\___|_____/|_| |_| |_|\__,_|_| \__|
# __/ |
# |___/
#
# Idea: dadaloop82 - [email protected]
# Started: April 2022
# Github: https://github.com/dadaloop82/MyHomeSmart-HASS-AppDeamon
# License: GPLv2 (https://raw.githubusercontent.com/dadaloop82/MyHomeSmart-HASS-AppDeamon/main/LICENSE)
# Version 2.0α
#
# Today we can enjoy several devices called "smart"; devices that seem to possess reasoning, logic and
# deduction skills in performing seemingly spontaneous actions. In reality, this is not the case at all,
# each device is programmed to do actions under certain conditions, without any understanding of what it is
# doing and the possible consequence that such actions can do. For example, smart speakers simply read what
# they find on the web, but they don't know if what they read is right or they cause the user to do the
# wrong thing. A smart television is not smart, it just offers more opportunities for digital services.
# And so on.
# Giving intelligence to home automation means giving a logical and comprehensive reasoning to the action
# that the home automation system should do to reach a certain objective.
#
# Essential library for communicating with HASS
import hassapi as hass
# Constants
import module.constant as CONSTANT
# Utility
import module.utility as UTILITY
# Home Assistant functions
import module.hass as HASS
# Logging functions
import module.log as LOG
# Databaase functions
import module.database as DB
# filesystem "exists" from os
from os.path import exists
class main(hass.Hass):
# Global Class variable
lastNodeID = -1 # last Entity id Changed
lastEditableEntity = -1
def entityStateChanged(self, entityName: str, attribute: dict, old: any, new: any, kwargs: dict):
"""Support function to HASS.entityUpdate
Args:
entityName (str): The name of entity
attribute (dict): The attribute of entity (from appDeamon listen_state)
old (any): The previous state of this entity
new (any): The new state of this entity
kwargs (dict): Extra arguments
"""
_isEditable = False
if "editable" in kwargs['attrs']:
_isEditable = kwargs['attrs']['editable']
_entityID, _nodeID = HASS.entityUpdate(self, DB, entityName, new, old,
attribute, _isEditable, self.lastNodeID, self.lastEditableEntity, kwargs)
LOG.info(self, "[lastNodeID] %s: [lastEditableEntity] %s -> [_entityID] %s" %
(self.lastNodeID, self.lastEditableEntity, _entityID))
self.lastNodeID = _nodeID
if(_isEditable and _entityID != self.lastEditableEntity):
self.lastEditableEntity = _entityID
def initialize(self):
"""Default entrypoint for appDeamon
"""
try:
""" Check DB existence and connect them """
if not exists(CONSTANT.DB_EntityState):
DB.create(self, CONSTANT.DB_EntityState)
DB.connect(self, CONSTANT.DB_EntityState,
CONSTANT.DB_EntityState)
if not exists(CONSTANT.DB_CauseEffect):
DB.create(self, CONSTANT.DB_CauseEffect)
DB.connect(self, CONSTANT.DB_CauseEffect,
CONSTANT.DB_CauseEffectName)
""" Get usable entities """
_entities = HASS.get_HASSEntities(
self,
UTILITY.getConfigValue(self, "include_entities"),
UTILITY.getConfigValue(self, "exclude_entities")
)
""" Check if are any usable entities """
if not _entities:
LOG.error(self,
"There are no entities to control or monitor, check apps.yaml", True)
LOG.info(self, ("[ %s ] entities were found to be usable" %
len(_entities)))
""" Subscribe on all entities """
for _entityData in _entities.items():
_entityName = _entityData[0]
_entityAttrs = _entityData[1]
_entityObj = self.get_entity(_entityName)
_entityObj.listen_state(
self.entityStateChanged, attrs=_entityAttrs['attributes'])
except Exception as e:
""" There has been an error """
LOG.error(self, e, True)