Skip to content

Commit

Permalink
Fix code styling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jun 14, 2023
1 parent 2d7cf3b commit 3c2ebeb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
18 changes: 11 additions & 7 deletions custom_components/dataplicity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from . import utils

DOMAIN = 'dataplicity'
DOMAIN = "dataplicity"


async def async_setup(hass: HomeAssistant, hass_config: dict):
Expand All @@ -19,13 +19,14 @@ async def async_setup(hass: HomeAssistant, hass_config: dict):
real_install = package.install_package

def fake_install(*args, **kwargs):
kwargs.pop('constraints')
kwargs.pop("constraints")
return real_install(*args, **kwargs)

try:
package.install_package = fake_install
# latest dataplicity has bug with redirect_port
await async_process_requirements(hass, DOMAIN, ['dataplicity==0.4.40'])
await async_process_requirements(hass, DOMAIN, ["dataplicity==0.4.40"])

# fix Python 3.11 support
if not hasattr(inspect, "getargspec"):

Expand All @@ -45,18 +46,21 @@ def getargspec(*args):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
# fix: type object 'array.array' has no attribute 'tostring'
from dataplicity import iptool
iptool.get_all_interfaces = lambda: [('lo', '127.0.0.1')]

iptool.get_all_interfaces = lambda: [("lo", "127.0.0.1")]

# fix: module 'platform' has no attribute 'linux_distribution'
from dataplicity import device_meta

device_meta.get_os_version = lambda: "Linux"

from dataplicity.client import Client

hass.data[DOMAIN] = client = Client(serial=entry.data['serial'],
auth_token=entry.data['auth'])
hass.data[DOMAIN] = client = Client(
serial=entry.data["serial"], auth_token=entry.data["auth"]
)
# replace default 80 port to Hass port (usual 8123)
client.port_forward.add_service('web', hass.config.api.port)
client.port_forward.add_service("web", hass.config.api.port)
Thread(name=DOMAIN, target=client.run_forever).start()

async def hass_stop(event):
Expand Down
41 changes: 21 additions & 20 deletions custom_components/dataplicity/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,41 @@

_LOGGER = logging.getLogger(__name__)

RE_TOKEN = re.compile(r'https://www\.dataplicity\.com/([a-z0-9]+)\.py')
RE_TOKEN = re.compile(r"https://www\.dataplicity\.com/([a-z0-9]+)\.py")


class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
async def async_step_user(self, data=None, error=None):
if sys.platform == 'win32':
return self.async_abort(reason='win32')
if sys.platform == "win32":
return self.async_abort(reason="win32")

if self.hass.config.api.use_ssl:
return self.async_abort(reason='ssl')
return self.async_abort(reason="ssl")

if data is None:
return self.async_show_form(
step_id='user',
data_schema=vol.Schema({
vol.Required('token'): str,
}),
errors={'base': error} if error else None
step_id="user",
data_schema=vol.Schema(
{
vol.Required("token"): str,
}
),
errors={"base": error} if error else None,
)

m = RE_TOKEN.search(data['token'])
token = m[1] if m else data['token']
m = RE_TOKEN.search(data["token"])
token = m[1] if m else data["token"]

if not token.isalnum():
return await self.async_step_user(error='token')
return await self.async_step_user(error="token")

session = async_get_clientsession(self.hass)
resp = await utils.register_device(session, token)
if resp:
return self.async_create_entry(title="Dataplicity", data={
'auth': resp['auth'],
'serial': resp['serial']
}, description_placeholders={
'device_url': resp['device_url']
})

return await self.async_step_user(error='auth')
return self.async_create_entry(
title="Dataplicity",
data={"auth": resp["auth"], "serial": resp["serial"]},
description_placeholders={"device_url": resp["device_url"]},
)

return await self.async_step_user(error="auth")
15 changes: 8 additions & 7 deletions custom_components/dataplicity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

async def register_device(session: ClientSession, token: str):
try:
r = await session.post('https://www.dataplicity.com/install/', data={
'name': "Home Assistant", 'serial': 'None', 'token': token
})
r = await session.post(
"https://www.dataplicity.com/install/",
data={"name": "Home Assistant", "serial": "None", "token": token},
)
if r.status != 200:
_LOGGER.error(f"Can't register dataplicity device: {r.status}")
return None
Expand All @@ -34,14 +35,14 @@ async def fix_middleware(hass: HomeAssistantType):
- 127.0.0.1
"""
for f in hass.http.app.middlewares:
if f.__name__ != 'forwarded_middleware':
if f.__name__ != "forwarded_middleware":
continue
# https://til.hashrocket.com/posts/ykhyhplxjh-examining-the-closure
for i, var in enumerate(f.__code__.co_freevars):
cell = f.__closure__[i]
if var == 'use_x_forwarded_for':
if var == "use_x_forwarded_for":
if not cell.cell_contents:
cell.cell_contents = True
elif var == 'trusted_proxies':
elif var == "trusted_proxies":
if not cell.cell_contents:
cell.cell_contents = [IPv4Network('127.0.0.1/32')]
cell.cell_contents = [IPv4Network("127.0.0.1/32")]

0 comments on commit 3c2ebeb

Please sign in to comment.