forked from AlexxIT/PythonScriptsPro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.py
More file actions
67 lines (55 loc) · 1.89 KB
/
Copy pathsensor.py
File metadata and controls
67 lines (55 loc) · 1.89 KB
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
import logging
from homeassistant.const import (
CONF_DEVICE_CLASS,
CONF_ICON,
CONF_NAME,
CONF_UNIT_OF_MEASUREMENT,
CONF_UNIQUE_ID,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
try:
if "file" in config:
finename = hass.config.path(config["file"])
with open(finename, "rt", encoding="utf-8") as f:
source = f.read()
elif "source" in config:
source = config["source"]
else:
return
code = compile(source, "<string>", "exec")
async_add_entities([PythonSensor(code, config)], True)
except Exception as e:
_LOGGER.error("Error init python script sensor", exc_info=e)
class PythonSensor(Entity):
def __init__(self, code, config: dict):
self.code = code
self.attributes = {}
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
self._attr_icon = config.get(CONF_ICON)
self._attr_name = config.get(CONF_NAME)
self._attr_unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
self._attr_unique_id = config.get(CONF_UNIQUE_ID)
@property
def state(self):
return self._attr_state
@state.setter
def state(self, value):
self._attr_state = value
@property
def state_attributes(self):
return self.attributes
def update(self):
try:
exec(self.code)
except Exception as e:
_LOGGER.error(f"Error update {self.name}", exc_info=e)