def decode_characteristics(self, json: dict) -> list[Characteristic]: """ Parses the json from homebridge-mqtt into a list of characteristic objects :param json: The json to parse :return: A list with the characteristics parsed from the json """ parsed_json = self._parse_gadget_json(json) gadget_characteristics = [] for characteristic_name in parsed_json: try: characteristic_ident = HomebridgeCharacteristicTranslator.str_to_type( characteristic_name) min_val = parsed_json[characteristic_name]["minValue"] max_val = parsed_json[characteristic_name]["maxValue"] steps = (max_val - min_val ) // parsed_json[characteristic_name]["minStep"] current_val = parsed_json[characteristic_name]["value"] characteristic = Characteristic(characteristic_ident, min_val, max_val, steps) characteristic.set_true_value(current_val) gadget_characteristics.append(characteristic) except CharacteristicParsingError as err: self._logger.error(err.args[0]) return gadget_characteristics
def gadget(): gadget = FanWestinghouseIR( GADGET_NAME, CLIENT_NAME, Characteristic(CharacteristicIdentifier.status, 0, 1), Characteristic(CharacteristicIdentifier.fan_speed, 0, 100, 4)) yield gadget gadget.__del__()
def encode_characteristic_update(characteristic: Characteristic) -> dict: """ Serializes a characteristic update information according to api specification :param characteristic: The characteristic to serialize :return: The serialized version of the the changeable characteristic information as dict """ return {"type": int(characteristic.get_type()), "step_value": characteristic.get_step_value()}
def encode_characteristic(characteristic: Characteristic) -> dict: """ Serializes a characteristic according to api specification :param characteristic: The characteristic to serialize :return: The serialized version of the characteristic as dict """ return {"type": int(characteristic.get_type()), "min": characteristic.get_min(), "max": characteristic.get_max(), "steps": characteristic.get_steps(), "step_value": characteristic.get_step_value()}
def lamp(characteristic_status): lamp = LampNeopixelBasic( L_NAME, TEST_CLIENT, characteristic_status, Characteristic(c_type=CharacteristicIdentifier.brightness, min_val=L_CHARACTERISTIC_MIN, max_val=L_BRIGHTNESS_MAX), Characteristic(c_type=CharacteristicIdentifier.hue, min_val=L_CHARACTERISTIC_MIN, max_val=L_HUE_MAX), Characteristic(c_type=CharacteristicIdentifier.saturation, min_val=L_CHARACTERISTIC_MIN, max_val=L_SATURATION_MAX)) yield lamp lamp.__del__()
def test_api_handle_gadget_update(api: ApiManager, network: DummyNetworkConnector, delegate: DummyApiDelegate): delegate.add_gadget( Gadget(GADGET_NAME, "spongolopolus", [Characteristic(CharacteristicIdentifier.status, 0, 1, 1, 1)])) network.mock_receive(ApiURIs.update_gadget.uri, REQ_SENDER, {"gadget": { "yolo": "blub" }}) assert delegate.get_last_gadget_update() is None network.mock_receive(ApiURIs.update_gadget.uri, REQ_SENDER, GADGET_UPDATE_ERR) assert delegate.get_last_gadget_update() is None network.mock_receive(ApiURIs.update_gadget.uri, REQ_SENDER, GADGET_UPDATE_ERR_UNKNOWN) assert delegate.get_last_gadget_update() is None network.mock_receive(ApiURIs.update_gadget.uri, REQ_SENDER, GADGET_UPDATE_OK) assert delegate.get_last_gadget_update() is not None assert delegate.get_last_gadget_update().get_characteristic_types( )[0] == GADGET_CHARACTERISTIC_TYPE
def _add_dummy_data(bridge: Bridge): gadget = FanWestinghouseIR( "dummy_fan", "bridge", Characteristic(CharacteristicIdentifier.status, 0, 1, 1), Characteristic(CharacteristicIdentifier.fan_speed, 0, 100, 4)) gadget.set_event_mapping([GadgetEventMapping("ab09d8_", [(1, 1)])]) bridge.get_gadget_manager().receive_gadget(gadget) date = datetime.utcnow() client = Client(name="dummy_client", runtime_id=18298931, flash_date=date, software_commit="2938479384", software_branch="spongo", port_mapping={}, boot_mode=1, api_version=SoftwareVersion(0, 0, 1)) bridge.get_client_manager().add_client(client)
def fan(characteristic_status): fan = FanWestinghouseIR( FAN_NAME, TEST_CLIENT, characteristic_status, Characteristic(c_type=CharacteristicIdentifier.fan_speed, min_val=FAN_SPEED_MIN, max_val=FAN_SPEED_MAX, steps=FAN_SPEED_STEPS)) yield fan fan.__del__()
def decode_characteristic(self, characteristic_data: dict) -> Characteristic: """ Parses a characteristic from the given data :param characteristic_data: Data to parse the characteristic from :return: The parsed Characteristic :raises CharacteristicDecodeError: If anything goes wrong parsing the characteristic """ try: identifier = CharacteristicIdentifier(characteristic_data["type"]) min_val = characteristic_data["min"] max_val = characteristic_data["max"] steps = characteristic_data["steps"] value = characteristic_data["step_value"] return Characteristic(identifier, min_val, max_val, steps, value) except KeyError: self._logger.error("Missing key in data for characteristic") except ValueError: self._logger.error(f"Cannot create CharacteristicIdentifier out of '{characteristic_data['type']}'") raise CharacteristicDecodeError
def f_characteristic_status(): characteristic = Characteristic(c_type=CharacteristicIdentifier.status, min_val=0, max_val=1) yield characteristic
def status_characteristic(): status = Characteristic(CharacteristicIdentifier.status, 0, 1, 1, 1) yield status
def f_characteristic_hue(): characteristic = Characteristic(c_type=CharacteristicIdentifier.hue, min_val=0, max_val=0xFF) yield characteristic
def f_characteristic_brightness(): characteristic = Characteristic(c_type=CharacteristicIdentifier.brightness, min_val=0, max_val=100) yield characteristic
def f_characteristic_fan_speed(): characteristic = Characteristic(c_type=CharacteristicIdentifier.fan_speed, min_val=0, max_val=100, steps=4) yield characteristic
def characteristic_status(): status = Characteristic(c_type=CharacteristicIdentifier.status, min_val=L_CHARACTERISTIC_MIN, max_val=L_STATUS_MAX) yield status
def f_characteristic_saturation(): characteristic = Characteristic(c_type=CharacteristicIdentifier.saturation, min_val=0, max_val=100) yield characteristic
def fan_speed_characteristic(): fan_speed = Characteristic(CharacteristicIdentifier.fan_speed, 0, 100, 3, 1) yield fan_speed