mirror of
https://github.com/andvikt/mega_hacs.git
synced 2025-12-11 00:54:28 +05:00
add new events, fix binsensor
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
import asyncio
|
||||
from bs4 import BeautifulSoup
|
||||
import aiohttp
|
||||
|
||||
host = '192.168.88.14/sec'
|
||||
|
||||
|
||||
|
||||
|
||||
# page = '''
|
||||
# <html><head><style>input,select{margin:1px}</style></head><body><a href="/sec/?cf=3">Back</a><br>P7/ON<br><a href="/sec/?pt=7&cmd=7:1">ON</a> <a href="/sec/?pt=7&cmd=7:0">OFF</a><br><form action="/sec/"><input type="hidden" name="pn" value="7">Type <select name="pty"><option value="255">NC</option><option value="0">In</option><option value="1" selected="">Out</option><option value="3">DSen</option><option value="4">I2C</option></select><br>Default: <select name="d"><option value="0" selected="">0</option><option value="1">1</option></select><br>Mode <select name="m"><option value="0" selected="">SW</option><option value="3">SW LINK</option><option value="2">DS2413</option></select><br>Group <input name="grp" size="2" value=""><br><input type="submit" value="Save"></form></body></html>
|
||||
# <head><style>input,select{margin:1px}</style></head>
|
||||
# <body><a href="/sec/?cf=3">Back</a><br>P7/ON<br><a href="/sec/?pt=7&cmd=7:1">ON</a> <a href="/sec/?pt=7&cmd=7:0">OFF</a><br><form action="/sec/"><input type="hidden" name="pn" value="7">Type <select name="pty"><option value="255">NC</option><option value="0">In</option><option value="1" selected="">Out</option><option value="3">DSen</option><option value="4">I2C</option></select><br>Default: <select name="d"><option value="0" selected="">0</option><option value="1">1</option></select><br>Mode <select name="m"><option value="0" selected="">SW</option><option value="3">SW LINK</option><option value="2">DS2413</option></select><br>Group <input name="grp" size="2" value=""><br><input type="submit" value="Save"></form></body>
|
||||
# <a href="/sec/?cf=3">Back</a>
|
||||
# <br>
|
||||
# P7/ON
|
||||
# <br>
|
||||
# <a href="/sec/?pt=7&cmd=7:1">ON</a>
|
||||
# <a href="/sec/?pt=7&cmd=7:0">OFF</a>
|
||||
# <br>
|
||||
# <form action="/sec/"><input type="hidden" name="pn" value="7">Type <select name="pty"><option value="255">NC</option><option value="0">In</option><option value="1" selected="">Out</option><option value="3">DSen</option><option value="4">I2C</option></select><br>Default: <select name="d"><option value="0" selected="">0</option><option value="1">1</option></select><br>Mode <select name="m"><option value="0" selected="">SW</option><option value="3">SW LINK</option><option value="2">DS2413</option></select><br>Group <input name="grp" size="2" value=""><br><input type="submit" value="Save"></form>
|
||||
# <body><a href="/sec/?cf=3">Back</a><br>P7/ON<br><a href="/sec/?pt=7&cmd=7:1">ON</a> <a href="/sec/?pt=7&cmd=7:0">OFF</a><br><form action="/sec/"><input type="hidden" name="pn" value="7">Type <select name="pty"><option value="255">NC</option><option value="0">In</option><option value="1" selected="">Out</option><option value="3">DSen</option><option value="4">I2C</option></select><br>Default: <select name="d"><option value="0" selected="">0</option><option value="1">1</option></select><br>Mode <select name="m"><option value="0" selected="">SW</option><option value="3">SW LINK</option><option value="2">DS2413</option></select><br>Group <input name="grp" size="2" value=""><br><input type="submit" value="Save"></form></body>
|
||||
# <html><head><style>input,select{margin:1px}</style></head><body><a href="/sec/?cf=3">Back</a><br>P7/ON<br><a href="/sec/?pt=7&cmd=7:1">ON</a> <a href="/sec/?pt=7&cmd=7:0">OFF</a><br><form action="/sec/"><input type="hidden" name="pn" value="7">Type <select name="pty"><option value="255">NC</option><option value="0">In</option><option value="1" selected="">Out</option><option value="3">DSen</option><option value="4">I2C</option></select><br>Default: <select name="d"><option value="0" selected="">0</option><option value="1">1</option></select><br>Mode <select name="m"><option value="0" selected="">SW</option><option value="3">SW LINK</option><option value="2">DS2413</option></select><br>Group <input name="grp" size="2" value=""><br><input type="submit" value="Save"></form></body></html>
|
||||
# '''
|
||||
# tree = BeautifulSoup(page, features="lxml")
|
||||
# pty = tree.find('select', attrs={'name': 'pty'}).find(selected=True)['value']
|
||||
# m = tree.find('select', attrs={'name': 'm'})
|
||||
# if m:
|
||||
# m = m.find(selected=True)['value']
|
||||
#
|
||||
# print(pty, m)
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.get_event_loop().run_until_complete(
|
||||
scan_port(0)
|
||||
)
|
||||
@@ -78,7 +78,7 @@ class MegaBinarySensor(BinarySensorEntity, MegaPushEntity):
|
||||
if val is None and self._state is not None:
|
||||
return self._state == 'ON'
|
||||
elif val is not None:
|
||||
return val == 'ON' or val == 1
|
||||
return val == 'ON' or val != 1
|
||||
|
||||
def _update(self, payload: dict):
|
||||
self.mega.values[self.port] = payload
|
||||
|
||||
@@ -31,4 +31,13 @@ PLATFORMS = [
|
||||
"sensor",
|
||||
]
|
||||
EVENT_BINARY_SENSOR = f'{DOMAIN}.sensor'
|
||||
PATT_SPLIT = re.compile('[;/]')
|
||||
EVENT_BINARY = f'{DOMAIN}.binary'
|
||||
|
||||
PATT_SPLIT = re.compile('[;/]')
|
||||
|
||||
LONG = 'long'
|
||||
RELEASE = 'release'
|
||||
LONG_RELEASE = 'long_release'
|
||||
PRESS = 'press'
|
||||
SINGLE_CLICK = 'single'
|
||||
DOUBLE_CLICK = 'double'
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import logging
|
||||
import asyncio
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import State
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from .hub import MegaD
|
||||
from .const import DOMAIN, CONF_CUSTOM, CONF_INVERT
|
||||
from .const import DOMAIN, CONF_CUSTOM, CONF_INVERT, EVENT_BINARY_SENSOR, LONG, \
|
||||
LONG_RELEASE, RELEASE, PRESS, SINGLE_CLICK, DOUBLE_CLICK
|
||||
|
||||
|
||||
class BaseMegaEntity(CoordinatorEntity, RestoreEntity):
|
||||
@@ -111,7 +113,51 @@ class MegaPushEntity(BaseMegaEntity):
|
||||
self._update(value)
|
||||
self.async_write_ha_state()
|
||||
self.lg.debug(f'state after update %s', self.state)
|
||||
self.is_first_update = False
|
||||
if not self.entity_id.startswith('binary_sensor'):
|
||||
return
|
||||
ll: bool = self.mega.last_long.get(self.port, False)
|
||||
if safe_int(value.get('click', 0)) == 1:
|
||||
self.hass.bus.async_fire(
|
||||
event_type=EVENT_BINARY_SENSOR,
|
||||
event_data={
|
||||
'entity_id': self.entity_id,
|
||||
'type': SINGLE_CLICK
|
||||
}
|
||||
)
|
||||
elif safe_int(value.get('click', 0)) == 2:
|
||||
self.hass.bus.async_fire(
|
||||
event_type=EVENT_BINARY_SENSOR,
|
||||
event_data={
|
||||
'entity_id': self.entity_id,
|
||||
'type': DOUBLE_CLICK
|
||||
}
|
||||
)
|
||||
elif safe_int(value.get('m', 0)) == 2:
|
||||
self.mega.last_long[self.port] = True
|
||||
self.hass.bus.async_fire(
|
||||
event_type=EVENT_BINARY_SENSOR,
|
||||
event_data={
|
||||
'entity_id': self.entity_id,
|
||||
'type': LONG
|
||||
}
|
||||
)
|
||||
elif safe_int(value.get('m', 0)) == 1:
|
||||
self.hass.bus.async_fire(
|
||||
event_type=EVENT_BINARY_SENSOR,
|
||||
event_data={
|
||||
'entity_id': self.entity_id,
|
||||
'type': LONG_RELEASE if ll else RELEASE,
|
||||
}
|
||||
)
|
||||
elif safe_int(value.get('m', None)) == 0:
|
||||
self.hass.bus.async_fire(
|
||||
event_type=EVENT_BINARY_SENSOR,
|
||||
event_data={
|
||||
'entity_id': self.entity_id,
|
||||
'type': PRESS,
|
||||
}
|
||||
)
|
||||
self.mega.last_long[self.port] = False
|
||||
return
|
||||
|
||||
def _update(self, payload: dict):
|
||||
|
||||
@@ -12,7 +12,7 @@ from .const import EVENT_BINARY_SENSOR, DOMAIN, CONF_RESPONSE_TEMPLATE
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.core import HomeAssistant
|
||||
from .tools import make_ints
|
||||
|
||||
from . import hub as h
|
||||
_LOGGER = logging.getLogger(__name__).getChild('http')
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ class MegaView(HomeAssistantView):
|
||||
_LOGGER.debug('templates: %s', self.templates)
|
||||
|
||||
async def get(self, request: Request) -> Response:
|
||||
|
||||
auth = False
|
||||
for x in self.allowed_hosts:
|
||||
if request.remote.startswith(x):
|
||||
@@ -47,7 +48,7 @@ class MegaView(HomeAssistantView):
|
||||
return Response(status=401)
|
||||
|
||||
hass: HomeAssistant = request.app['hass']
|
||||
hub: 'hub.MegaD' = hass.data.get(DOMAIN).get(request.remote) # TODO: проверить какой remote
|
||||
hub: 'h.MegaD' = hass.data.get(DOMAIN).get(request.remote) # TODO: проверить какой remote
|
||||
if hub is None and request.remote == '::1':
|
||||
hub = hass.data.get(DOMAIN).get('__def')
|
||||
if hub is None:
|
||||
@@ -64,6 +65,7 @@ class MegaView(HomeAssistantView):
|
||||
data['mega_id'] = hub.id
|
||||
ret = 'd'
|
||||
if port is not None:
|
||||
hub.values[port] = data
|
||||
for cb in self.callbacks[hub.id][port]:
|
||||
cb(data)
|
||||
template: Template = self.templates.get(hub.id, {}).get(port)
|
||||
@@ -81,4 +83,3 @@ class MegaView(HomeAssistantView):
|
||||
await asyncio.sleep(1)
|
||||
await hub.updater.async_refresh()
|
||||
|
||||
|
||||
|
||||
@@ -15,8 +15,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
from .const import TEMP, HUM, PATT_SPLIT, DOMAIN, CONF_HTTP, EVENT_BINARY_SENSOR
|
||||
from .exceptions import CannotConnect, MqttNotConfigured
|
||||
from .http import MegaView
|
||||
from .exceptions import CannotConnect
|
||||
from .tools import make_ints
|
||||
|
||||
TEMP_PATT = re.compile(r'temp:([01234567890\.]+)')
|
||||
@@ -76,6 +75,7 @@ class MegaD:
|
||||
self.mqtt = mqtt
|
||||
self.id = id
|
||||
self.lck = asyncio.Lock()
|
||||
self.last_long = {}
|
||||
self._http_lck = asyncio.Lock()
|
||||
self._notif_lck = asyncio.Lock()
|
||||
self.cnd = asyncio.Condition()
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
_params = ['m', 'click', 'cnt', 'pt']
|
||||
|
||||
def make_ints(d: dict):
|
||||
for x in d:
|
||||
for x in _params:
|
||||
try:
|
||||
d[x] = float(d[x])
|
||||
d[x] = int(d.get(x, 0))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
if 'm' not in d:
|
||||
|
||||
Reference in New Issue
Block a user