Compare commits
	
		
			3 Commits
		
	
	
		
			9c434bf22c
			...
			1812642773
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1812642773 | |||
| 85190aa9dd | |||
| 
						 | 
					fd3c2db642 | 
@ -6,6 +6,7 @@ from homeassistant import config_entries
 | 
				
			|||||||
from homeassistant.helpers.event import async_track_point_in_utc_time
 | 
					from homeassistant.helpers.event import async_track_point_in_utc_time
 | 
				
			||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
 | 
					from homeassistant.const import EVENT_HOMEASSISTANT_STOP
 | 
				
			||||||
import homeassistant.util.dt as dt_util
 | 
					import homeassistant.util.dt as dt_util
 | 
				
			||||||
 | 
					from homeassistant.helpers import device_registry as dr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from . import pyplejd
 | 
					from . import pyplejd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -28,7 +29,21 @@ async def async_setup(hass, config):
 | 
				
			|||||||
async def async_setup_entry(hass, config_entry):
 | 
					async def async_setup_entry(hass, config_entry):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    plejdManager = pyplejd.PlejdManager(config_entry.data)
 | 
					    plejdManager = pyplejd.PlejdManager(config_entry.data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    devices = await plejdManager.get_devices()
 | 
					    devices = await plejdManager.get_devices()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if sum(d.type in ["light", "switch"] for d in devices.values()) == 0:
 | 
				
			||||||
 | 
					        site_data = await plejdManager.get_site_data()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        device_registry = dr.async_get(hass)
 | 
				
			||||||
 | 
					        device_registry.async_get_or_create(
 | 
				
			||||||
 | 
					            config_entry_id=config_entry.entry_id,
 | 
				
			||||||
 | 
					            identifiers={(DOMAIN, config_entry.data["siteId"])},
 | 
				
			||||||
 | 
					            manufacturer="Plejd",
 | 
				
			||||||
 | 
					            name=site_data.get("site", {}).get("title", "Unknown site"),
 | 
				
			||||||
 | 
					            entry_type=dr.DeviceEntryType.SERVICE,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for dev in devices.values():
 | 
					    for dev in devices.values():
 | 
				
			||||||
        ble_device = bluetooth.async_ble_device_from_address(
 | 
					        ble_device = bluetooth.async_ble_device_from_address(
 | 
				
			||||||
            hass, dev.BLE_address, True
 | 
					            hass, dev.BLE_address, True
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										6
									
								
								custom_components/plejd/diagnostics.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								custom_components/plejd/diagnostics.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					from . import pyplejd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async def async_get_config_entry_diagnostics(hass, config_entry):
 | 
				
			||||||
 | 
					    plejdManager = pyplejd.PlejdManager(config_entry.data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return await plejdManager.get_site_data()
 | 
				
			||||||
@ -48,10 +48,10 @@ class PlejdLight(LightEntity, CoordinatorEntity):
 | 
				
			|||||||
            "identifiers": {(DOMAIN, self.device.BLE_address)},
 | 
					            "identifiers": {(DOMAIN, self.device.BLE_address)},
 | 
				
			||||||
            "name": self.device.name,
 | 
					            "name": self.device.name,
 | 
				
			||||||
            "manufacturer": "Plejd",
 | 
					            "manufacturer": "Plejd",
 | 
				
			||||||
            "model": f"{self.device.model} ({self.device.hardwareId})",
 | 
					            "model": {self.device.model},
 | 
				
			||||||
            #"connections": ???,
 | 
					            #"connections": ???,
 | 
				
			||||||
            "suggested_area": self.device.room,
 | 
					            "suggested_area": self.device.room,
 | 
				
			||||||
            "sw_version": self.device.firmware,
 | 
					            "sw_version": f"{self.device.firmware} ({self.device.hardwareId})",
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
 | 
				
			|||||||
@ -4,7 +4,7 @@ from datetime import timedelta
 | 
				
			|||||||
from bleak_retry_connector import close_stale_connections
 | 
					from bleak_retry_connector import close_stale_connections
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .mesh import PlejdMesh
 | 
					from .mesh import PlejdMesh
 | 
				
			||||||
from .api import get_cryptokey, get_devices
 | 
					from .api import get_cryptokey, get_devices, get_site_data
 | 
				
			||||||
from .plejd_device import PlejdDevice
 | 
					from .plejd_device import PlejdDevice
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .const import PLEJD_SERVICE
 | 
					from .const import PLEJD_SERVICE
 | 
				
			||||||
@ -32,6 +32,9 @@ class PlejdManager:
 | 
				
			|||||||
    def connected(self):
 | 
					    def connected(self):
 | 
				
			||||||
        return self.mesh is not None and self.mesh.connected
 | 
					        return self.mesh is not None and self.mesh.connected
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async def get_site_data(self):
 | 
				
			||||||
 | 
					        return await get_site_data(**self.credentials)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async def get_devices(self):
 | 
					    async def get_devices(self):
 | 
				
			||||||
        devices = await get_devices(**self.credentials)
 | 
					        devices = await get_devices(**self.credentials)
 | 
				
			||||||
        self.devices = {k: PlejdDevice(self, **v) for (k,v) in devices.items()}
 | 
					        self.devices = {k: PlejdDevice(self, **v) for (k,v) in devices.items()}
 | 
				
			||||||
 | 
				
			|||||||
@ -43,13 +43,17 @@ async def _get_site_details(session, siteId):
 | 
				
			|||||||
        #     fp.write(json.dumps(data))
 | 
					        #     fp.write(json.dumps(data))
 | 
				
			||||||
        return data
 | 
					        return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					site_data = None
 | 
				
			||||||
async def get_site_data(username, password, siteId):
 | 
					async def get_site_data(username, password, siteId):
 | 
				
			||||||
    # TODO: Memoize this somehow?
 | 
					    global site_data
 | 
				
			||||||
 | 
					    if site_data is not None:
 | 
				
			||||||
 | 
					        return site_data
 | 
				
			||||||
    async with ClientSession(base_url=API_BASE_URL, headers=headers) as session:
 | 
					    async with ClientSession(base_url=API_BASE_URL, headers=headers) as session:
 | 
				
			||||||
        session_token = await _login(session, username, password)
 | 
					        session_token = await _login(session, username, password)
 | 
				
			||||||
        _LOGGER.debug("Session token: %s", session_token)
 | 
					        _LOGGER.debug("Session token: %s", session_token)
 | 
				
			||||||
        session.headers["X-Parse-Session-Token"] = session_token
 | 
					        session.headers["X-Parse-Session-Token"] = session_token
 | 
				
			||||||
        details = await _get_site_details(session, siteId)
 | 
					        details = await _get_site_details(session, siteId)
 | 
				
			||||||
 | 
					        site_data = details
 | 
				
			||||||
        return details
 | 
					        return details
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async def get_sites(username, password):
 | 
					async def get_sites(username, password):
 | 
				
			||||||
 | 
				
			|||||||
@ -74,7 +74,7 @@ class PlejdMesh():
 | 
				
			|||||||
            return False
 | 
					            return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.client = client
 | 
					        self.client = client
 | 
				
			||||||
        self.connected_node = binascii.a2b_hex(address.replace(":", ""))[::-1]
 | 
					        self.connected_node = binascii.a2b_hex(address.replace(":", "").replace("-", ""))[::-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await asyncio.sleep(2)
 | 
					        await asyncio.sleep(2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -149,7 +149,7 @@ class PlejdMesh():
 | 
				
			|||||||
            return False
 | 
					            return False
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            _LOGGER.debug("Authenticating")
 | 
					            _LOGGER.debug("Authenticating")
 | 
				
			||||||
            await self.client.write_gatt_char(PLEJD_AUTH, [0], response=True)
 | 
					            await self.client.write_gatt_char(PLEJD_AUTH, b"\0x00", response=True)
 | 
				
			||||||
            challenge = await self.client.read_gatt_char(PLEJD_AUTH)
 | 
					            challenge = await self.client.read_gatt_char(PLEJD_AUTH)
 | 
				
			||||||
            response = auth_response(self.crypto_key, challenge)
 | 
					            response = auth_response(self.crypto_key, challenge)
 | 
				
			||||||
            await self.client.write_gatt_char(PLEJD_AUTH, response, response=True)
 | 
					            await self.client.write_gatt_char(PLEJD_AUTH, response, response=True)
 | 
				
			||||||
 | 
				
			|||||||
@ -7,10 +7,10 @@ Device = namedtuple("Device", ["model", "type", "dimmable"])
 | 
				
			|||||||
LIGHT = "light"
 | 
					LIGHT = "light"
 | 
				
			||||||
SENSOR = "sensor"
 | 
					SENSOR = "sensor"
 | 
				
			||||||
SWITCH = "switch"
 | 
					SWITCH = "switch"
 | 
				
			||||||
 | 
					UNKNOWN = "unknown"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
HARDWARE_TYPES = {
 | 
					HARDWARE_TYPES = {
 | 
				
			||||||
    "0": Device("-unknown-", LIGHT, False),
 | 
					    "0": Device("-unknown-", UNKNOWN, False),
 | 
				
			||||||
    "1": Device("DIM-01", LIGHT, True),
 | 
					    "1": Device("DIM-01", LIGHT, True),
 | 
				
			||||||
    "2": Device("DIM-02", LIGHT, True),
 | 
					    "2": Device("DIM-02", LIGHT, True),
 | 
				
			||||||
    "3": Device("CTR-01", LIGHT, False),
 | 
					    "3": Device("CTR-01", LIGHT, False),
 | 
				
			||||||
@ -19,18 +19,15 @@ HARDWARE_TYPES = {
 | 
				
			|||||||
    "6": Device("WPH-01", SWITCH, False),
 | 
					    "6": Device("WPH-01", SWITCH, False),
 | 
				
			||||||
    "7": Device("REL-01", SWITCH, False),
 | 
					    "7": Device("REL-01", SWITCH, False),
 | 
				
			||||||
    "8": Device("SPR-01", SWITCH, False),
 | 
					    "8": Device("SPR-01", SWITCH, False),
 | 
				
			||||||
    "9": Device("-unknown-", LIGHT, False),
 | 
					 | 
				
			||||||
    "10": Device("WRT-01", SWITCH, False),
 | 
					    "10": Device("WRT-01", SWITCH, False),
 | 
				
			||||||
    "11": Device("DIM-01", LIGHT, True),
 | 
					    "11": Device("DIM-01", LIGHT, True),
 | 
				
			||||||
    "12": Device("-unknown-", LIGHT, False),
 | 
					 | 
				
			||||||
    "13": Device("Generic", LIGHT, False),
 | 
					    "13": Device("Generic", LIGHT, False),
 | 
				
			||||||
    "14": Device("DIM-01", LIGHT, True),
 | 
					    "14": Device("DIM-01", LIGHT, True),
 | 
				
			||||||
    "15": Device("-unknown-", LIGHT, False),
 | 
					    "15": Device("DIM-02", LIGHT, True),
 | 
				
			||||||
    "16": Device("-unknown-", LIGHT, False),
 | 
					    "17": Device("REL-01-2P", SWITCH, False),
 | 
				
			||||||
    "17": Device("REL-01", SWITCH, False),
 | 
					 | 
				
			||||||
    "18": Device("REL-02", SWITCH, False),
 | 
					    "18": Device("REL-02", SWITCH, False),
 | 
				
			||||||
    "19": Device("-unknown-", LIGHT, False),
 | 
					 | 
				
			||||||
    "20": Device("SPR-01", SWITCH, False),
 | 
					    "20": Device("SPR-01", SWITCH, False),
 | 
				
			||||||
 | 
					    "36": Device("LED-75", LIGHT, True),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PlejdDevice:
 | 
					class PlejdDevice:
 | 
				
			||||||
 | 
				
			|||||||
@ -19,8 +19,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
 | 
				
			|||||||
            async def updateCallback(data):
 | 
					            async def updateCallback(data):
 | 
				
			||||||
                coordinator.async_set_updated_data(data)
 | 
					                coordinator.async_set_updated_data(data)
 | 
				
			||||||
            dev.updateCallback = updateCallback
 | 
					            dev.updateCallback = updateCallback
 | 
				
			||||||
            light = PlejdSwitch(coordinator, dev)
 | 
					            switch = PlejdSwitch(coordinator, dev)
 | 
				
			||||||
            entities.append(light)
 | 
					            entities.append(switch)
 | 
				
			||||||
    async_add_entities(entities, False)
 | 
					    async_add_entities(entities, False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Coordinator(DataUpdateCoordinator):
 | 
					class Coordinator(DataUpdateCoordinator):
 | 
				
			||||||
@ -48,10 +48,10 @@ class PlejdSwitch(SwitchEntity, CoordinatorEntity):
 | 
				
			|||||||
            "identifiers": {(DOMAIN, self.device.BLE_address)},
 | 
					            "identifiers": {(DOMAIN, self.device.BLE_address)},
 | 
				
			||||||
            "name": self.device.name,
 | 
					            "name": self.device.name,
 | 
				
			||||||
            "manufacturer": "Plejd",
 | 
					            "manufacturer": "Plejd",
 | 
				
			||||||
            "model": f"{self.device.model} ({self.device.hardwareId})",
 | 
					            "model": self.device.model,
 | 
				
			||||||
            #"connections": ???,
 | 
					            #"connections": ???,
 | 
				
			||||||
            "suggested_area": self.device.room,
 | 
					            "suggested_area": self.device.room,
 | 
				
			||||||
            "sw_version": self.device.firmware,
 | 
					            "sw_version": f"{self.device.firmware} ({self.device.hardwareId})",
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user