Skip to content

Commit

Permalink
fix bugs and fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Apr 9, 2021
1 parent 831debb commit 6572b28
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 34 deletions.
2 changes: 0 additions & 2 deletions custom_components/miwifi/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from typing import Optional

from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant, callback
from homeassistant.components.binary_sensor import ENTITY_ID_FORMAT, BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
Expand All @@ -26,7 +25,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn

async_add_entities(sensors, True)


class MiWiFiBinarySensor(BinarySensorEntity):
def __init__(self, hass: HomeAssistant, luci: LuciData, code: str, data: dict) -> None:
self.hass = hass
Expand Down
3 changes: 1 addition & 2 deletions custom_components/miwifi/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging
import requests
import voluptuous as vol
import homeassistant.helpers.config_validation as cv

from homeassistant.core import HomeAssistant, callback
from homeassistant.core import callback
from homeassistant.config_entries import ConfigFlow, OptionsFlow, ConfigEntry
from homeassistant.const import (
CONF_IP_ADDRESS,
Expand Down
21 changes: 11 additions & 10 deletions custom_components/miwifi/core/luci.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ def set_state(self, state: bool) -> None:

async def login(self) -> dict:
nonce = self.generate_nonce()
response = {}
data = {}

try:
with async_timeout.timeout(self.timeout, loop = self._loop):
Expand Down Expand Up @@ -137,16 +135,18 @@ async def prepare_data(self):
async def set_device_data(self) -> None:
init_info = await self.init_info()
status = await self.status()
model = None

model = init_info["model"] if "model" in init_info else None
if not model and "hardware" in init_info:
if "model" in init_info:
model = init_info["model"]
elif "hardware" in init_info:
model = init_info["hardware"]

self._device_data = {
"mac": status["hardware"]["mac"],
"name": init_info["routername"] if "routername" in init_info else None,
"manufacturer": DEFAULT_MANUFACTURER,
"model": init_info["model"] if "model" in init_info else None,
"model": model,
"sw_version": init_info["romversion"] if "romversion" in init_info else None,
}

Expand All @@ -170,7 +170,7 @@ async def set_entity_data(self) -> None:
wifi_state = True
for state in wifi_status["status"]:
if state["up"] != 1:
self._state = False
wifi_state = False

uptime = wan_info["info"]["uptime"] if isinstance(wan_info["info"], dict) and "uptime" in wan_info["info"] else 0

Expand Down Expand Up @@ -313,7 +313,6 @@ async def get_entities_map(self) -> dict:
return entries_map

async def get(self, path: str):
data = {}
try:
with async_timeout.timeout(self.timeout, loop = self._loop):
response = await self._session.get(
Expand Down Expand Up @@ -356,18 +355,20 @@ async def reboot(self) -> dict:
return await self.get("xqsystem/reboot")

async def led(self, state: Optional[int] = None) -> dict:
return await self.get("misystem/led{}".format(f"?on={state}" if state != None else ""))
return await self.get("misystem/led{}".format(f"?on={state}" if state is not None else ""))

async def device_list(self) -> dict:
return await self.get("misystem/devicelist")

async def wifi_connect_devices(self) -> dict:
return await self.get("xqnetwork/wifi_connect_devices")

def sha1(self, key: str) -> str:
@staticmethod
def sha1(key: str) -> str:
return hashlib.sha1(key.encode()).hexdigest()

def get_mac_address(self) -> str:
@staticmethod
def get_mac_address() -> str:
as_hex = f"{uuid.getnode():012x}"

return ":".join(
Expand Down
12 changes: 4 additions & 8 deletions custom_components/miwifi/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities) -> None:
luci = hass.data[DOMAIN][config_entry.entry_id]
unsub_update = None

legacy_devices = await _get_legacy_devices(hass)

Expand Down Expand Up @@ -76,7 +75,7 @@ def update_devices() -> None:
if new_devices:
async_add_entities(new_devices)

unsub_update = async_dispatcher_connect(
async_dispatcher_connect(
hass, DEVICES_UPDATED, update_devices
)

Expand All @@ -103,7 +102,7 @@ async def _get_legacy_devices(hass: HomeAssistant) -> dict:

try:
devices = await hass.async_add_executor_job(load_yaml_config_file, hass.config.path(LEGACY_YAML_DEVICES))
except HomeAssistantError as err:
except HomeAssistantError:
return {}
except FileNotFoundError:
return {}
Expand All @@ -112,7 +111,7 @@ async def _get_legacy_devices(hass: HomeAssistant) -> dict:
try:
device = legacy_schema(device)
device["dev_id"] = cv.slugify(dev_id)
except vol.Invalid as e:
except vol.Invalid:
continue
else:
legacy_devices[device["mac"]] = dict(device)
Expand Down Expand Up @@ -300,16 +299,13 @@ async def _update_device(self, new_entry: ConfigEntry, remove_entry: ConfigEntry
if not device:
return

via_device_id = UNDEFINED
via = device_registry.async_get_device({(DOMAIN, self._device["router_mac"])})
if via:
via_device_id = via.id

device_registry._async_update_device(
device.id,
add_config_entry_id = new_entry.entry_id,
remove_config_entry_id = remove_entry.entry_id,
via_device_id = via.id,
via_device_id = via.id if via else UNDEFINED,
merge_connections = connections,
merge_identifiers = {(DOMAIN, self._mac)},
manufacturer = UNDEFINED,
Expand Down
5 changes: 0 additions & 5 deletions custom_components/miwifi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import homeassistant.helpers.device_registry as dr

from typing import Optional

from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant, callback
from homeassistant.components.light import ENTITY_ID_FORMAT, LightEntity
from homeassistant.config_entries import ConfigEntry
Expand All @@ -13,7 +10,6 @@

from .core.const import DATA_UPDATED, DOMAIN, LIGHTS
from .core.luci_data import LuciData
from .core import exceptions

_LOGGER = logging.getLogger(__name__)

Expand All @@ -26,7 +22,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn

async_add_entities(lights, True)


class MiWiFiLight(LightEntity):
def __init__(self, hass: HomeAssistant, luci: LuciData, code: str, data: dict) -> None:
self.hass = hass
Expand Down
2 changes: 1 addition & 1 deletion custom_components/miwifi/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "miwifi",
"name": "MiWiFi",
"version": "1.1.0",
"version": "1.1.1",
"documentation": "https://github.com/dmamontov/hass-miwifi",
"config_flow": true,
"requirements": [],
Expand Down
1 change: 0 additions & 1 deletion custom_components/miwifi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from typing import Optional

from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant, callback
from homeassistant.components.sensor import ENTITY_ID_FORMAT
from homeassistant.config_entries import ConfigEntry
Expand Down
5 changes: 0 additions & 5 deletions custom_components/miwifi/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import homeassistant.helpers.device_registry as dr

from typing import Optional

from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant, callback
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
from homeassistant.config_entries import ConfigEntry
Expand All @@ -13,7 +10,6 @@

from .core.const import DATA_UPDATED, DOMAIN, SWITCHS
from .core.luci_data import LuciData
from .core import exceptions

_LOGGER = logging.getLogger(__name__)

Expand All @@ -26,7 +22,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn

async_add_entities(switchs, True)


class MiWiFiSwitch(SwitchEntity):
def __init__(self, hass: HomeAssistant, luci: LuciData, code: str, data: dict) -> None:
self.hass = hass
Expand Down

0 comments on commit 6572b28

Please sign in to comment.