try to fix integration
This commit is contained in:
parent
ed44da8427
commit
6d3755e465
@ -1,7 +1,5 @@
|
|||||||
DOMAIN = "atc_mi_thermometer_gateway"
|
DOMAIN = "atc_mi_thermometer_gateway"
|
||||||
|
|
||||||
from .discover_gateways import start_discovery_client
|
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
hass.states.set("hello_state.world", "Paulus")
|
hass.states.set("hello_state.world", "Paulus")
|
||||||
|
|||||||
@ -14,6 +14,7 @@ class EntityState:
|
|||||||
battery_percent:int
|
battery_percent:int
|
||||||
battery_volt:float
|
battery_volt:float
|
||||||
rssi:int
|
rssi:int
|
||||||
|
mac:str
|
||||||
|
|
||||||
|
|
||||||
def test_api(gateway):
|
def test_api(gateway):
|
||||||
@ -25,8 +26,8 @@ def test_api(gateway):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_state(gateway, device) -> EntityState | None:
|
def get_state(gateway, entity_mac) -> EntityState | None:
|
||||||
request = f'http://{gateway}:8000/api/state/{device}'
|
request = f'http://{gateway}:8000/api/state/{entity_mac}'
|
||||||
response = api.get(request)
|
response = api.get(request)
|
||||||
if not response.ok: return None
|
if not response.ok: return None
|
||||||
response_json = json.loads(response.text)
|
response_json = json.loads(response.text)
|
||||||
@ -37,7 +38,8 @@ def get_state(gateway, device) -> EntityState | None:
|
|||||||
response_json['humidity'],
|
response_json['humidity'],
|
||||||
response_json['battery_percent'],
|
response_json['battery_percent'],
|
||||||
response_json['battery_volt'],
|
response_json['battery_volt'],
|
||||||
response_json['rssi'])
|
response_json['rssi'],
|
||||||
|
entity_mac)
|
||||||
|
|
||||||
|
|
||||||
def get_deices(gateway) -> list | None:
|
def get_deices(gateway) -> list | None:
|
||||||
@ -48,10 +50,10 @@ def get_deices(gateway) -> list | None:
|
|||||||
return response_json['info']['devices']
|
return response_json['info']['devices']
|
||||||
|
|
||||||
|
|
||||||
def get_device(gateway, device) -> str | None:
|
def get_device(gateway, entity_mac) -> str | None:
|
||||||
devices = get_deices(gateway)
|
devices = get_deices(gateway)
|
||||||
if device in devices:
|
if entity_mac in devices:
|
||||||
index = devices.index(device)
|
index = devices.index(entity_mac)
|
||||||
return devices[index]
|
return devices[index]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
12
home_assistant_integration/gateway.py
Normal file
12
home_assistant_integration/gateway.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from api import *
|
||||||
|
from sensor import MiThermometer
|
||||||
|
|
||||||
|
|
||||||
|
def find_all_devices(gateway) -> list:
|
||||||
|
found_devices = []
|
||||||
|
devices = get_deices(gateway=gateway)
|
||||||
|
for device in devices:
|
||||||
|
device_state = get_state(entity_mac=device, gateway=gateway)
|
||||||
|
found_devices.append(MiThermometer(state=device_state))
|
||||||
|
|
||||||
|
return found_devices
|
||||||
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from api import *
|
from api import *
|
||||||
|
from .gateway import find_all_devices
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -28,15 +29,14 @@ def setup_platform(
|
|||||||
add_entities: AddEntitiesCallback,
|
add_entities: AddEntitiesCallback,
|
||||||
discovery_info: DiscoveryInfoType | None = None):
|
discovery_info: DiscoveryInfoType | None = None):
|
||||||
|
|
||||||
devices = get_deices(gateway=config[CONF_HOST])
|
devices = find_all_devices(gateway=config[CONF_HOST])
|
||||||
for device in devices:
|
for device in devices:
|
||||||
device_state = get_state(gateway=config[CONF_HOST], device=device)
|
add_entities(device)
|
||||||
add_entities([MiThermometer(mac=device, state=device_state)])
|
|
||||||
|
|
||||||
|
|
||||||
class MiThermometer(SensorEntity):
|
class MiThermometer(SensorEntity):
|
||||||
def __init__(self, mac:str, state:EntityState):
|
def __init__(self, state:EntityState):
|
||||||
self._mac = mac
|
self._mac = state.mac
|
||||||
self._online = False
|
self._online = False
|
||||||
self._last_update = ""
|
self._last_update = ""
|
||||||
self._temperature = state.temperature
|
self._temperature = state.temperature
|
||||||
@ -79,4 +79,4 @@ class MiThermometer(SensorEntity):
|
|||||||
return self._rssi
|
return self._rssi
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
pass
|
state = get_state(gateway=config[CONF_HOST], entity_mac=self._mac)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user