Compare commits

...

2 Commits

Author SHA1 Message Date
Andrey
72cf516353 fix mqtt sensor 2021-01-28 12:17:38 +03:00
Andrey
3b6459468b fix mqtt sensor 2021-01-28 12:04:43 +03:00
4 changed files with 15 additions and 8 deletions

View File

@@ -160,7 +160,7 @@ async def async_remove_entry(hass, entry) -> None:
if hub is None: if hub is None:
return return
_LOGGER.debug(f'remove {id}') _LOGGER.debug(f'remove {id}')
_hubs.pop(entry.entry_id) _hubs.pop(id, None)
task: asyncio.Task = _POLL_TASKS.pop(id, None) task: asyncio.Task = _POLL_TASKS.pop(id, None)
if task is not None: if task is not None:
task.cancel() task.cancel()

View File

@@ -6,7 +6,7 @@ from homeassistant.const import CONF_NAME
from homeassistant.core import State from homeassistant.core import State
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.restore_state import RestoreEntity
from .hub import MegaD from . import hub as h
from .const import DOMAIN, CONF_CUSTOM, CONF_INVERT, EVENT_BINARY_SENSOR, LONG, \ from .const import DOMAIN, CONF_CUSTOM, CONF_INVERT, EVENT_BINARY_SENSOR, LONG, \
LONG_RELEASE, RELEASE, PRESS, SINGLE_CLICK, DOUBLE_CLICK, EVENT_BINARY LONG_RELEASE, RELEASE, PRESS, SINGLE_CLICK, DOUBLE_CLICK, EVENT_BINARY
@@ -19,6 +19,11 @@ async def _set_events_on():
_LOGGER.debug('events on') _LOGGER.debug('events on')
_events_on = True _events_on = True
def set_events_off():
global _events_on
_events_on = False
_task_set_ev_on = None _task_set_ev_on = None
@@ -30,7 +35,7 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity):
""" """
def __init__( def __init__(
self, self,
mega: MegaD, mega: 'h.MegaD',
port: int, port: int,
config_entry: ConfigEntry = None, config_entry: ConfigEntry = None,
id_suffix=None, id_suffix=None,

View File

@@ -15,6 +15,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import TEMP, HUM, PATT_SPLIT, DOMAIN, CONF_HTTP, EVENT_BINARY_SENSOR from .const import TEMP, HUM, PATT_SPLIT, DOMAIN, CONF_HTTP, EVENT_BINARY_SENSOR
from .entities import set_events_off
from .exceptions import CannotConnect from .exceptions import CannotConnect
from .tools import make_ints from .tools import make_ints
@@ -109,6 +110,7 @@ class MegaD:
async def start(self): async def start(self):
self.loop = asyncio.get_event_loop() self.loop = asyncio.get_event_loop()
if self.mqtt is not None: if self.mqtt is not None:
set_events_off()
self.subs = await self.mqtt.async_subscribe( self.subs = await self.mqtt.async_subscribe(
topic=f"{self.mqtt_id}/+", topic=f"{self.mqtt_id}/+",
msg_callback=self._process_msg, msg_callback=self._process_msg,
@@ -209,9 +211,9 @@ class MegaD:
raise NoPort() raise NoPort()
if ':' in ret: if ':' in ret:
ret = PATT_SPLIT.split(ret) ret = PATT_SPLIT.split(ret)
ret = dict([ ret = {'value': dict([
x.split(':') for x in ret if x.count(':') == 1 x.split(':') for x in ret if x.count(':') == 1
]) ])}
elif 'ON' in ret: elif 'ON' in ret:
ret = {'value': 'ON'} ret = {'value': 'ON'}
elif 'OFF' in ret: elif 'OFF' in ret:
@@ -232,7 +234,7 @@ class MegaD:
self.lg.debug('parsed: %s', ret) self.lg.debug('parsed: %s', ret)
if http_cmd == 'list' and isinstance(ret, dict) and 'value' in ret: if http_cmd == 'list' and isinstance(ret, dict) and 'value' in ret:
await asyncio.sleep(1) await asyncio.sleep(1)
ret = await self.request(pt=port, http_cmd=http_cmd) ret = await self.request(pt=port, cmd=http_cmd)
ret = self.parse_response(ret) ret = self.parse_response(ret)
self.values[port] = ret self.values[port] = ret
return ret return ret

View File

@@ -15,7 +15,7 @@ from homeassistant.const import (
CONF_DOMAIN, CONF_DOMAIN,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .entities import MegaD from . import hub as h
from .entities import MegaOutPort from .entities import MegaOutPort
from .const import CONF_DIMMER, CONF_SWITCH, DOMAIN, CONF_CUSTOM, CONF_SKIP from .const import CONF_DIMMER, CONF_SWITCH, DOMAIN, CONF_CUSTOM, CONF_SKIP
@@ -45,7 +45,7 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_devices): async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_devices):
mid = config_entry.data[CONF_ID] mid = config_entry.data[CONF_ID]
hub: MegaD = hass.data['mega'][mid] hub: 'h.MegaD' = hass.data['mega'][mid]
devices = [] devices = []
customize = hass.data.get(DOMAIN, {}).get(CONF_CUSTOM, {}) customize = hass.data.get(DOMAIN, {}).get(CONF_CUSTOM, {})