Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vaughan-zeng committed Aug 5, 2021
1 parent 35c073f commit bd2c3e7
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 120 deletions.
2 changes: 1 addition & 1 deletion custom_components/uvfive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Support for uvFive MIoT device."""
"""Support for Five MIoT device."""
from datetime import timedelta
import logging

Expand Down
55 changes: 32 additions & 23 deletions custom_components/uvfive/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Config flow to configure uvFive MIoT device."""
"""Config flow to configure Five MIoT device."""
import logging
from re import search

Expand All @@ -18,21 +18,25 @@
DOMAIN,
UVFIVE_MODELS,
)
from .device import ConnectFiveDevice

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = "uvFive MIoT device"
DEFAULT_NAME = "Five MIoT device"
DEFAULT_LAMP_NAME = 'Five Sterilization Lamp'
DEFAULT_RACK_NAME = 'Five Sterilization Rack'

DEVICE_CONFIG = vol.Schema({
vol.Required(CONF_HOST): str,
vol.Required(CONF_TOKEN): vol.All(str, vol.Length(min=32, max=32)),
vol.Optional(CONF_NAME, default = DEFAULT_NAME): str,
# vol.Optional(CONF_NAME, default = DEFAULT_NAME): str,
vol.Optional(CONF_NAME): str,
# vol.Optional(CONF_MODEL, default = ''): vol.In(UVFIVE_MODELS),
})


class uvFiveMiotFlowHandler(config_entries.ConfigFlow, domain = DOMAIN):
"""Handle a uvFive MIoT config flow."""
"""Handle a Five MIoT config flow."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
Expand Down Expand Up @@ -85,45 +89,46 @@ async def async_step_zeroconf(self, discovery_info):

# Discovered device is not yet supported
_LOGGER.debug(
"Not yet supported uvFive MIoT device '%s' discovered with host %s",
"Not yet supported Five MIoT device '%s' discovered with host %s",
name,
self.host,
)
return self.async_abort(reason="not_uvfive_miot")

async def async_step_device(self, user_input=None):
"""Handle a flow initialized by the user to configure a uvfive miot device."""
"""Handle a flow initialized by the user to configure a Five MIoT device."""
errors = {}
if user_input is not None:
self.host = user_input[CONF_HOST]
token = user_input[CONF_TOKEN]
name = user_input[CONF_NAME]
if user_input.get(CONF_MODEL):
model = user_input.get(CONF_MODEL)
else:
model = None

try:
miot_device = Device(self.host, token)
device_info = miot_device.info()
if model is None and device_info is not None:
model = device_info.model

_LOGGER.info(
"%s %s %s detected",
model,
device_info.firmware_version,
device_info.hardware_version,
)
except DeviceException as ex:
raise PlatformNotReady from ex
# Try to connect to a Five MIoT Device.
connect_device_class = ConnectFiveDevice(self.hass)
await connect_device_class.async_connect_device(self.host, token)
device_info = connect_device_class.device_info

if model is None and device_info is not None:
model = device_info.model

if model is not None:
if self.mac is None and device_info is not None:
self.mac = format_mac(device_info.mac_address)

# Setup uvFive MIoT device
# name = user_input.get(CONF_NAME, model)
# Setup Five MIoT device
if user_input.get(CONF_NAME):
name = user_input.get(CONF_NAME)
else:
if model == 'uvfive.s_lamp.slmap2':
name = DEFAULT_LAMP_NAME
elif model == 'uvfive.steriliser.tiger':
name = DEFAULT_RACK_NAME
else:
name = DEFAULT_NAME

for device_model in UVFIVE_MODELS:
if model.startswith(device_model):
unique_id = f"{model}-{device_info.mac_address}"
Expand All @@ -142,6 +147,10 @@ async def async_step_device(self, user_input=None):
},
)

errors["base"] = "unknown_device"
else:
errors["base"] = "cannot_connect"

schema = DEVICE_CONFIG

return self.async_show_form(step_id="device", data_schema=schema, errors=errors)
2 changes: 1 addition & 1 deletion custom_components/uvfive/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Constants for the uvFive MIoT device component."""
"""Constants for the Five MIoT device component."""
DOMAIN = "uvfive"

CONF_FLOW_TYPE = "config_flow_device"
Expand Down
91 changes: 91 additions & 0 deletions custom_components/uvfive/device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Code to handle a Five MIoT Device."""
import logging

from miio import Device, DeviceException

from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import Entity

from .const import CONF_MAC, CONF_MODEL, DOMAIN

_LOGGER = logging.getLogger(__name__)


class ConnectFiveDevice:
"""Class to async connect to a Five MIoT Device."""

def __init__(self, hass):
"""Initialize the entity."""
self._hass = hass
self._device = None
self._device_info = None

@property
def device(self):
"""Return the class containing all connections to the device."""
return self._device

@property
def device_info(self):
"""Return the class containing device info."""
return self._device_info

async def async_connect_device(self, host, token):
"""Connect to the Five MIoT Device."""
_LOGGER.debug("Initializing Five MIoT device with host %s (token %s...)", host, token[:5])
try:
self._device = Device(host, token)
# get the device info
self._device_info = await self._hass.async_add_executor_job(
self._device.info
)
except DeviceException:
_LOGGER.error(
"DeviceException during setup of Five MIoT device with host %s", host
)
return False
_LOGGER.debug(
"%s %s %s detected",
self._device_info.model,
self._device_info.firmware_version,
self._device_info.hardware_version,
)
return True


class FiveMIoTEntity(Entity):
"""Representation of a base Five MIoT Entity."""

def __init__(self, name, device, entry, unique_id):
"""Initialize the Five MIoT Device."""
self._device = device
self._model = entry.data[CONF_MODEL]
self._mac = entry.data[CONF_MAC]
self._device_id = entry.unique_id
self._unique_id = unique_id
self._name = name

@property
def unique_id(self):
"""Return an unique ID."""
return self._unique_id

@property
def name(self):
"""Return the name of this entity, if any."""
return self._name

@property
def device_info(self):
"""Return the device info."""
device_info = {
"identifiers": {(DOMAIN, self._device_id)},
"manufacturer": "Five",
"name": self._name,
"model": self._model,
}

if self._mac is not None:
device_info["connections"] = {(dr.CONNECTION_NETWORK_MAC, self._mac)}

return device_info
Loading

0 comments on commit bd2c3e7

Please sign in to comment.