Last active
February 16, 2020 08:46
-
-
Save AlexxIT/c4a70b633b35495074a60f11ed29a320 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
1. Place file here: /config/custom_components/sonofftest/__init__.py | |
2. Add to configuration.yaml: `sonofftest: 192.168.1.123` (IP of sonoff) | |
3. Block the Sonoff from accessing the Internet | |
4. Restart Sonoff | |
5. Wait Sonoff LED to start blinking (two blinks every 2 seconds) | |
6. Restart HA | |
""" | |
import asyncio | |
import logging | |
import websockets | |
_LOGGER = logging.getLogger(__name__) | |
DOMAIN = 'sonofftest' | |
async def async_setup(hass, hass_config): | |
host = hass_config[DOMAIN] | |
try: | |
_LOGGER.warning(f"connect to {host}") | |
coro = websockets.connect(f"ws://{host}:8081/", subprotocols=['chat']) | |
ws = await asyncio.wait_for(coro, 10) | |
_LOGGER.warning("connected") | |
await ws.send('ping') | |
_LOGGER.warning("end") | |
except Exception as e: | |
_LOGGER.error(f"Error: {type(e)}") | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment