smaller headers

This commit is contained in:
Andrey
2021-01-25 21:35:06 +03:00
parent 1548e8c364
commit c0b1247b9e
4 changed files with 34 additions and 17 deletions

View File

@@ -8,10 +8,10 @@ from aiohttp.web_request import Request
from aiohttp.web_response import Response
from homeassistant.helpers.template import Template
from .const import EVENT_BINARY_SENSOR, CONF_HTTP, DOMAIN, CONF_CUSTOM, CONF_RESPONSE_TEMPLATE
from .const import EVENT_BINARY_SENSOR, DOMAIN, CONF_RESPONSE_TEMPLATE
from homeassistant.components.http import HomeAssistantView
from homeassistant.core import callback, HomeAssistant
from . import hub
from homeassistant.core import HomeAssistant
from .tools import make_ints
_LOGGER = logging.getLogger(__name__).getChild('http')
@@ -61,6 +61,7 @@ class MegaView(HomeAssistantView):
make_ints(data)
port = data.get('pt')
data = data.copy()
data['mega_id'] = hub.id
ret = 'd'
if port is not None:
for cb in self.callbacks[hub.id][port]:
@@ -72,8 +73,7 @@ class MegaView(HomeAssistantView):
template.hass = hass
ret = template.async_render(data)
_LOGGER.debug('response %s', ret)
ret = Response(body=ret or 'd', content_type='text/plain', headers={})
ret.headers.clear()
ret = Response(body=ret or 'd', content_type='text/plain', headers={'Server': 's', 'Date': 'n'})
return ret
async def later_update(self, hub):
@@ -82,13 +82,3 @@ class MegaView(HomeAssistantView):
await hub.updater.async_refresh()
def make_ints(d: dict):
for x in d:
try:
d[x] = float(d[x])
except ValueError:
pass
if 'm' not in d:
d['m'] = 0
if 'click' not in d:
d['click'] = 0

View File

@@ -14,9 +14,10 @@ from homeassistant.const import DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_HUMIDITY
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
from .const import TEMP, HUM, PATT_SPLIT, DOMAIN, CONF_HTTP, EVENT_BINARY_SENSOR
from .exceptions import CannotConnect, MqttNotConfigured
from .http import MegaView
from .tools import make_ints
TEMP_PATT = re.compile(r'temp:([01234567890\.]+)')
HUM_PATT = re.compile(r'hum:([01234567890\.]+)')
@@ -290,9 +291,16 @@ class MegaD:
value = None
try:
value = json.loads(msg.payload)
value = make_ints(value)
self.values[port] = value
for cb in self._callbacks[port]:
cb(value)
value = value.copy()
value['mega_id'] = self.id
self.hass.bus.async_fire(
EVENT_BINARY_SENSOR,
value,
)
except Exception as exc:
self.lg.warning(f'could not parse json ({msg.payload}): {exc}')
return

View File

@@ -0,0 +1,10 @@
def make_ints(d: dict):
for x in d:
try:
d[x] = float(d[x])
except ValueError:
pass
if 'm' not in d:
d['m'] = 0
if 'click' not in d:
d['click'] = 0