Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
asterix11 committed Apr 2, 2022
0 parents commit 7147b39
Show file tree
Hide file tree
Showing 15 changed files with 483 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: 🚀 Create release

on:
push:
branches:
- main
paths:
- custom_components/**
- pyproject.toml
- .github/**

jobs:
create_release:
runs-on: ubuntu-latest
steps:
- name: ⚙️ Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: 🔃 Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: ✅ Hassfest validation
uses: home-assistant/actions/hassfest@master

- name: ✅ HACS validation
uses: hacs/action@main
with:
category: integration

- name: 📢 Semantic Release
uses: relekang/python-semantic-release@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Pavel Roslovets

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Jablotron RS485 Home Assistant Integration

For connecting to a JA-121-T using a Modbus to Ethernet adapter.
16 changes: 16 additions & 0 deletions custom_components/jablotron_rs485/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import logging

PLATFORMS = ["switch"]

_LOGGER = logging.getLogger(__name__)

async def async_setup(hass, config):
"""Setup Jablotron RS485."""
_LOGGER.info("Setting up.")
return True

async def async_setup_entry(hass, config_entry):
"""Set up entry."""
_LOGGER.info("Initializing config entry.")
hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
return True
74 changes: 74 additions & 0 deletions custom_components/jablotron_rs485/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import asyncio
import logging, os
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
import json
import time
from telnetlib import Telnet

from .const import (
CONF_HOST,
CONF_PORT,
CONF_PIN,
CONF_PGCOUNT,
CMD_PGSTATE,
CMD_PGON,
CMD_PGOFF,
OBJ_PG
)

class JablotronRS485TerminalAPI:
def __init__(self, hass, config):
self._hass = hass
self._config = config
self._logger = logging.getLogger(__name__ + ":" + self.__class__.__name__)

async def _request(self, command, _object, index, wait):
try:
with Telnet(self._config.get(CONF_HOST), self._config.get(CONF_PORT)) as tn:
tn.write(bytes(self._config.get(CONF_PIN)+" "+command+" "+str(index)+"\n",'UTF-8'))
if not wait:
tn.close()
return None
arrived = [False, False]
result = False
while not arrived[0] or not arrived[1]:
line = tn.read_until(b"\n")
msg = line.decode("utf-8").replace("\n", "").strip()
if (msg.replace(":", "") == command):
arrived[0] = True
if (arrived[0]):
splitted = msg.split(" ")
if (splitted[0] == _object):
arrived[1] = True
if (splitted[2] == "ON"):
result = True
elif (splitted[2] == "OFF"):
result = False
tn.close()
return result
except Exception as e:
self._logger.error("Error while executing telnet query:")
self._logger.error(e)
return None

async def sleep(self, _time):
def _sleep():
time.sleep(_time)
await self._hass.async_add_executor_job(_sleep)

async def getDevices(self, _type):
devices = []
for i in range(1, self._config.get(CONF_PGCOUNT) + 1):
devices.append(i)

return devices

async def getPGState(self, index):
return (await self._request(CMD_PGSTATE, OBJ_PG, index, True))

async def setPGState(self, index, state):
if (state):
return (await self._request(CMD_PGON, OBJ_PG, index, False))
else:
return (await self._request(CMD_PGOFF, OBJ_PG, index, False))
30 changes: 30 additions & 0 deletions custom_components/jablotron_rs485/config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from homeassistant import config_entries
from .const import DOMAIN
import voluptuous as vol
import logging

_LOGGER = logging.getLogger(__name__)

class JablotronRS485ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Example config flow."""

data = None

async def async_step_user(self, info):
"""Config flow step user."""
if info is not None:
if not info.get("host") is None and not info.get("port") is None and not info.get("pin") is None and not info.get("pgcount") is None:
self.data = info
return await self.async_step_finish()

return self.async_show_form(
step_id="user", data_schema=vol.Schema({
vol.Required("host"): str,
vol.Required("port"): int,
vol.Required("pin"): str,
vol.Required("pgcount"): int,
})
)

async def async_step_finish(self, user_input=None):
return self.async_create_entry(title="Jablotron RS485", data=self.data)
16 changes: 16 additions & 0 deletions custom_components/jablotron_rs485/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Constants for the Jablotron integration."""

DOMAIN = "jablotron_rs485"

CONF_HOST = "host"
CONF_PORT = "port"
CONF_PIN = "pin"
CONF_PGCOUNT = "pgcount"

CMD_PGSTATE = "PGSTATE"
CMD_PGON = "PGON"
CMD_PGOFF = "PGOFF"

OBJ_PG = "PG"

ATTR_DEVICE_TYPE_SWITCH = "SWITCH"
17 changes: 17 additions & 0 deletions custom_components/jablotron_rs485/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"domain": "jablotron_rs485",
"name": "Jablotron",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/jablotron",
"ssdp": [],
"zeroconf": [],
"homekit": {},
"dependencies": [
"http"
],
"codeowners": [
"@asterix11"
],
"requirements": [],
"iot_class": "cloud_push"
}
22 changes: 22 additions & 0 deletions custom_components/jablotron_rs485/strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"config": {
"step": {
"user": {
"title": "Set up Jablotron",
"description": "Connect your Jablotron System using an RS485-Terminal-IP-Gateway.",
"data": {
"host": "Host/IP",
"port": "Port number",
"pin": "User PIN"
}
}
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]"
},
"create_entry": {
"default": "[%key:common::config_flow::create_entry::authenticated%]"
}
}
}
Loading

0 comments on commit 7147b39

Please sign in to comment.