This commit is contained in:
monster
2018-12-30 14:13:57 +03:00
parent e311c31b88
commit 2033fe79e4
225 changed files with 6370 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
import appdaemon.plugins.hass.hassapi as hass
import globals
#
# Person
# Script that creates persons with their properties.
#
# Args:
#
# None
#
# Release Notes
#
# Version 1.0:
# Initial Version
class Person(hass.Hass):
view_name = 'group.persons'
view_friendly_name = 'Люди'
tags = []
def initialize(self):
self.log('args: {}'.format(self.args))
self.listen_event_handle_list = []
self.add_person()
# self.listen_event_handle_list.append(self.listen_event(self.event_detected, "click"))
def event_detected(self, event_name, data, kwargs):
self.log("ButtonClicked: {}".format(data["entity_id"]))
#self.check_batteries({"force": 1})
def add_person(self):
view = self.get_state(self.view_name, attribute = 'all')
self.log(view)
if (view == None):
self.set_state(self.view_name, state="on",attributes={"view": False, "hidden": False, "assumed_state": "on", "friendly_name": self.view_friendly_name,"entity_id": []})
# attributes = {}
# attributes['unit_of_measurement'] = sensor['unit']
# attributes['description'] = 'Created and updated from appdaemon (narodmon)'
# if 'friendly_name' in self.args:
# attributes['friendly_name'] = self.args['friendly_name']
# self.set_state(self.args['entity'], state = sensor['value'], attributes = attributes)
def terminate(self):
if self.listen_event_handle_list != None:
for listen_event_handle in self.listen_event_handle_list:
self.cancel_listen_event(listen_event_handle)
+107
View File
@@ -0,0 +1,107 @@
import globals
from automation import Base
from globals import CONF_PEOPLE
from typing import Any, Tuple, Union
#
# Person
# Script that creates persons with their properties.
#
# Args:
#
# None
#
# Release Notes
#
# Version 1.0:
# Initial Version
class Person(Base):
"""Define a class to represent a person."""
def initialize(self) -> None:
"""Initialize."""
super().initialize()
self._add_person_to_globals()
# Listen for changes to the device trackers (to initiate re-rendering
# if needed):
for tracker in self.device_trackers:
self.listen_state(self._device_tracker_changed_cb, tracker)
@property
def first_name(self) -> str:
"""Return the person's name."""
return self.name.title()
@property
def device_trackers(self) -> list:
"""Return the device trackers associated with the person."""
return self.properties['device_trackers']
@property
def notifiers(self) -> list:
"""Return the notifiers associated with the person."""
return self.properties['notifiers']
@property
def presence_sensor(self) -> str:
"""Return the entity ID of the generated presence status sensor."""
return 'sensor.{0}_presence_status'.format(self.name)
@property
def presence_input_select(self) -> str:
"""Return the input select related to the person's presence."""
return self.properties['presence_input_select']
@property
def location(self) -> str:
"""Get the current location from combined device trackers."""
raw_location = globals.most_common([
self.get_tracker_state(tracker_entity)
for tracker_entity in self.properties['device_trackers']
])
if raw_location not in ('home', 'not_home'):
return raw_location
return self.get_state(self.presence_input_select)
#return raw_location
def _device_tracker_changed_cb( # pylint: disable=too-many-arguments
self, entity: Union[str, dict], attribute: str, old: str, new: str,
kwargs: dict) -> None:
"""Respond when a device tracker changes state."""
if new == old:
return
self._render_presence_status_sensor()
def _add_person_to_globals(self):
self.global_vars.setdefault(CONF_PEOPLE, [])
existing_element = None
for element in self.global_vars[CONF_PEOPLE]:
if element.first_name == self.first_name:
existing_element = element
if existing_element == None:
self.global_vars[CONF_PEOPLE].append(self)
else:
index = self.global_vars[CONF_PEOPLE].index(existing_element)
self.global_vars[CONF_PEOPLE][index] = self
def _render_presence_status_sensor(self):
"""Update the presence status sensor."""
if self.location in ('Home', 'Just Arrived'):
picture_state = 'home'
else:
picture_state = 'away'
self.log('Setting presence sensor {} state to {}.'.format(self.presence_sensor, self.location))
self.set_state(
self.presence_sensor,
state=self.location,
attributes={
'friendly_name':
self.first_name,
'entity_picture':
'/local/{0}-{1}.png'.format(self.name, picture_state),
})
@@ -0,0 +1,49 @@
danil:
module: person
class: Person
priority: 10
properties:
at_home_sensor: binary_sensor.danil_home
device_trackers:
- sensor.tag_monster_nut
notifiers:
- telegram_monster
presence_input_select: input_select.danil_presence_status
dependencies:
- automation
global_dependencies:
- globals
sveta:
module: person
class: Person
priority: 10
properties:
at_home_sensor: binary_sensor.sveta_home
device_trackers:
- sensor.tag_nut_guest
notifiers:
- telegram_sveta
presence_input_select: input_select.sveta_presence_status
dependencies:
- automation
global_dependencies:
- globals
mama:
module: person
class: Person
priority: 10
properties:
at_home_sensor: binary_sensor.mama_home
device_trackers:
- sensor.tag_mamik_band
- sensor.tag_nut_blue
notifiers:
- telegram_mama
presence_input_select: input_select.mama_presence_status
dependencies:
- automation
global_dependencies:
- globals